Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New syntax #8

Merged
merged 3 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .docker-env/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ compose_file_override: docker-compose.override.yml
compose_default_profile: app
compose_sidecar_profile: sidecar

# Git options
git_default_branch: master

# Debug options
show_executed_commands: true

Expand Down
40 changes: 27 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ USAGE:
docker-env [global options] command [command options]

VERSION:
1.0.0
2.0.0

DESCRIPTION:
All commands must run in the git repository directory of the project.
Expand All @@ -63,7 +63,7 @@ COMMANDS:
restart, r, reboot Restart docker containers
remove, rm, delete Remove docker containers
ls, list, l, ll List projects, 'll' to show containers.
cleanup Removes all projects
reset, cleanup Removes all projects
build, b Build docker images
info, config, show Show configuration
terminal, term, shell, ssh Run terminal
Expand Down Expand Up @@ -92,23 +92,28 @@ USAGE:

DESCRIPTION:
Start docker containers.
If project name is not specified, current branch name is used.
If project name is not specified, master branch is used.
If project does not exist it will be created.

OPTIONS:
--project value, -p value set a project name
--service value, -s value start a single service
--recreate, -r recreate the containers (default: false)
--update, -u update the images and recreate the containers (default: false)
--help, -h show help
--project value, -p value set a project name
--branch, -b use current git branch as project name (default: false)
--service value, -s value start a single service
--recreate, -r recreate the containers (default: false)
--update, -u update the images and recreate the containers (default: false)
--no-hooks, --without-hooks do not run pre/post start hooks (default: false)
--help, -h show help
```

## Sample commands

```
# Create new environment based on branch name
# Create new environment with default git branch (master)
docker-env start

# Create branch based environment
docker-env start -b

# Create new environment with custom name
docker-env start -p db-fix

Expand All @@ -118,6 +123,9 @@ docker-env restart -p db-fix
# Restart a single container
docker-env restart -p db-fix -s app

# Restart a single container from active environment
docker-env restart -s postgresql

# Recreate all containers
docker-env start -r

Expand All @@ -131,7 +139,7 @@ docker-env start -u
docker-env start -s app -u

# Cleanup environments and images
docker-env cleanup --with-images
docker-env reset --hard

# Run shell
docker-env shell
Expand Down Expand Up @@ -202,6 +210,9 @@ compose_file_override: docker-compose.override.yml
compose_default_profile: app
compose_sidecar_profile: sidecar

# Git options
git_default_branch: master

# Debug options
show_executed_commands: true

Expand Down Expand Up @@ -234,13 +245,16 @@ vscode_binary: code

# Scripts to run before and after
pre_start_hooks:
- .docker-env/pre-start.sh
- .docker-env/pre-start.d/10-add-ssl-certificate-linux.sh
- .docker-env/pre-start.d/10-add-ssl-certificate-macos.sh
- .docker-env/pre-start.d/20-ports.sh
- .docker-env/pre-start.d/30-ssh-agent.sh

post_start_hooks:
- .docker-env/post-start.sh
- .docker-env/post-start.d/10-show-message.sh

post_stop_hooks:
- .docker-env/post-stop.sh
- .docker-env/post-stop.d/10-node-modules.sh

```

Expand Down
60 changes: 60 additions & 0 deletions app/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package app

import (
"github.com/marcinhlybin/docker-env/config"
"github.com/marcinhlybin/docker-env/logger"
"github.com/marcinhlybin/docker-env/registry"
"github.com/urfave/cli/v2"
)

type AppContext struct {
ProjectName string
ServiceName string
IsBranch bool
IsProjectNameSet bool
Config *config.Config
Registry *registry.DockerProjectRegistry
}

func NewAppContext(c *cli.Context) (*AppContext, error) {
cfg, err := initializeConfig(c)
if err != nil {
return nil, err
}

reg, err := initializeRegistry(cfg)
if err != nil {
return nil, err
}

// Set default project name
projectName := c.String("project")
if projectName == "" {
projectName = cfg.GitDefaultBranch
}

return &AppContext{
ProjectName: projectName,
ServiceName: c.String("service"),
IsProjectNameSet: c.IsSet("project"),
IsBranch: c.Bool("branch"),
Config: cfg,
Registry: reg,
}, nil
}

func initializeConfig(c *cli.Context) (*config.Config, error) {
cfg := config.NewConfig()
if err := cfg.LoadConfig(c.String("config")); err != nil {
return nil, err
}

// Show executed commands
logger.ShowExecutedCommands(cfg.ShowExecutedCommands)

return cfg, nil
}

func initializeRegistry(cfg *config.Config) (*registry.DockerProjectRegistry, error) {
return registry.NewDockerProjectRegistry(cfg), nil
}
43 changes: 43 additions & 0 deletions app/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package app

import (
"github.com/marcinhlybin/docker-env/logger"
"github.com/marcinhlybin/docker-env/project"
)

func (ctx *AppContext) CreateProject() (*project.Project, error) {
p, err := project.NewProject(ctx.ProjectName, ctx.ServiceName)
if err != nil {
return nil, err
}

// Set project name from git branch
if ctx.IsBranch {
if err := p.SetProjectNameFromGitBranch(); err != nil {
return nil, err
}
}

return p, nil
}

func (ctx *AppContext) ActiveProject() (*project.Project, error) {
if ctx.IsProjectNameSet {
return ctx.CreateProject()
}

// Fetch active project
reg := ctx.Registry
p, err := reg.ActiveProject()
if err != nil {
return nil, err
}

if p == nil {
logger.Warning("No active project found")
return nil, nil
}

p.SetServiceName(ctx.ServiceName)
return p, nil
}
99 changes: 0 additions & 99 deletions cmd/app.go

This file was deleted.

39 changes: 34 additions & 5 deletions cmd/build.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package cmd

import (
"fmt"

"github.com/marcinhlybin/docker-env/app"
"github.com/marcinhlybin/docker-env/project"
"github.com/urfave/cli/v2"
)

Expand All @@ -9,13 +13,18 @@ var BuildCommand = cli.Command{
Aliases: []string{"b"},
Usage: "Build docker images",
Description: `Build docker images.
If environment name is not specified current branch name is used.`,
If project name is not specified, master branch is used.`,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "project",
Aliases: []string{"p"},
Usage: "set a project name",
},
&cli.BoolFlag{
Name: "branch",
Aliases: []string{"b"},
Usage: "use current git branch as project name",
},
&cli.StringFlag{
Name: "service",
Aliases: []string{"s"},
Expand All @@ -32,14 +41,34 @@ If environment name is not specified current branch name is used.`,
func buildAction(c *cli.Context) error {
ExitWithErrorOnArgs(c)

app, err := NewApp(c)
// Mutually exclusive flags -p and -b
if c.IsSet("project") && c.IsSet("branch") {
return fmt.Errorf("flags -p and -b are mutually exclusive")
}

ctx, err := app.NewAppContext(c)
if err != nil {
return err
}

p, reg := app.Project, app.Registry
var p *project.Project

noCache := c.Bool("no-cache")
// Use active project name if no project or branch is specified
if !c.IsSet("project") && !c.IsSet("branch") {
p, err = ctx.ActiveProject()
if err != nil {
return err
}
}

return reg.BuildProject(p, noCache)
// No active project found
if p == nil {
p, err = ctx.CreateProject()
if err != nil {
return err
}
}

noCache := c.Bool("no-cache")
return ctx.Registry.BuildProject(p, noCache)
}
Loading