Skip to content

Commit bdf724d

Browse files
authored
New syntax (#8)
New docker-env syntax * now commands without arguments use default git branch (master) * added -b option for branched environments * hooks refactor * added checking aws binary * set go version to 1.22
1 parent 74f440f commit bdf724d

27 files changed

+402
-201
lines changed

.docker-env/config.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ compose_file_override: docker-compose.override.yml
1616
compose_default_profile: app
1717
compose_sidecar_profile: sidecar
1818

19+
# Git options
20+
git_default_branch: master
21+
1922
# Debug options
2023
show_executed_commands: true
2124

README.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ USAGE:
5151
docker-env [global options] command [command options]
5252
5353
VERSION:
54-
1.0.0
54+
2.0.0
5555
5656
DESCRIPTION:
5757
All commands must run in the git repository directory of the project.
@@ -63,7 +63,7 @@ COMMANDS:
6363
restart, r, reboot Restart docker containers
6464
remove, rm, delete Remove docker containers
6565
ls, list, l, ll List projects, 'll' to show containers.
66-
cleanup Removes all projects
66+
reset, cleanup Removes all projects
6767
build, b Build docker images
6868
info, config, show Show configuration
6969
terminal, term, shell, ssh Run terminal
@@ -92,23 +92,28 @@ USAGE:
9292
9393
DESCRIPTION:
9494
Start docker containers.
95-
If project name is not specified, current branch name is used.
95+
If project name is not specified, master branch is used.
9696
If project does not exist it will be created.
9797
9898
OPTIONS:
99-
--project value, -p value set a project name
100-
--service value, -s value start a single service
101-
--recreate, -r recreate the containers (default: false)
102-
--update, -u update the images and recreate the containers (default: false)
103-
--help, -h show help
99+
--project value, -p value set a project name
100+
--branch, -b use current git branch as project name (default: false)
101+
--service value, -s value start a single service
102+
--recreate, -r recreate the containers (default: false)
103+
--update, -u update the images and recreate the containers (default: false)
104+
--no-hooks, --without-hooks do not run pre/post start hooks (default: false)
105+
--help, -h show help
104106
```
105107

106108
## Sample commands
107109

108110
```
109-
# Create new environment based on branch name
111+
# Create new environment with default git branch (master)
110112
docker-env start
111113
114+
# Create branch based environment
115+
docker-env start -b
116+
112117
# Create new environment with custom name
113118
docker-env start -p db-fix
114119
@@ -118,6 +123,9 @@ docker-env restart -p db-fix
118123
# Restart a single container
119124
docker-env restart -p db-fix -s app
120125
126+
# Restart a single container from active environment
127+
docker-env restart -s postgresql
128+
121129
# Recreate all containers
122130
docker-env start -r
123131
@@ -131,7 +139,7 @@ docker-env start -u
131139
docker-env start -s app -u
132140
133141
# Cleanup environments and images
134-
docker-env cleanup --with-images
142+
docker-env reset --hard
135143
136144
# Run shell
137145
docker-env shell
@@ -202,6 +210,9 @@ compose_file_override: docker-compose.override.yml
202210
compose_default_profile: app
203211
compose_sidecar_profile: sidecar
204212
213+
# Git options
214+
git_default_branch: master
215+
205216
# Debug options
206217
show_executed_commands: true
207218
@@ -234,13 +245,16 @@ vscode_binary: code
234245
235246
# Scripts to run before and after
236247
pre_start_hooks:
237-
- .docker-env/pre-start.sh
248+
- .docker-env/pre-start.d/10-add-ssl-certificate-linux.sh
249+
- .docker-env/pre-start.d/10-add-ssl-certificate-macos.sh
250+
- .docker-env/pre-start.d/20-ports.sh
251+
- .docker-env/pre-start.d/30-ssh-agent.sh
238252
239253
post_start_hooks:
240-
- .docker-env/post-start.sh
254+
- .docker-env/post-start.d/10-show-message.sh
241255
242256
post_stop_hooks:
243-
- .docker-env/post-stop.sh
257+
- .docker-env/post-stop.d/10-node-modules.sh
244258
245259
```
246260

app/context.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package app
2+
3+
import (
4+
"github.com/marcinhlybin/docker-env/config"
5+
"github.com/marcinhlybin/docker-env/logger"
6+
"github.com/marcinhlybin/docker-env/registry"
7+
"github.com/urfave/cli/v2"
8+
)
9+
10+
type AppContext struct {
11+
ProjectName string
12+
ServiceName string
13+
IsBranch bool
14+
IsProjectNameSet bool
15+
Config *config.Config
16+
Registry *registry.DockerProjectRegistry
17+
}
18+
19+
func NewAppContext(c *cli.Context) (*AppContext, error) {
20+
cfg, err := initializeConfig(c)
21+
if err != nil {
22+
return nil, err
23+
}
24+
25+
reg, err := initializeRegistry(cfg)
26+
if err != nil {
27+
return nil, err
28+
}
29+
30+
// Set default project name
31+
projectName := c.String("project")
32+
if projectName == "" {
33+
projectName = cfg.GitDefaultBranch
34+
}
35+
36+
return &AppContext{
37+
ProjectName: projectName,
38+
ServiceName: c.String("service"),
39+
IsProjectNameSet: c.IsSet("project"),
40+
IsBranch: c.Bool("branch"),
41+
Config: cfg,
42+
Registry: reg,
43+
}, nil
44+
}
45+
46+
func initializeConfig(c *cli.Context) (*config.Config, error) {
47+
cfg := config.NewConfig()
48+
if err := cfg.LoadConfig(c.String("config")); err != nil {
49+
return nil, err
50+
}
51+
52+
// Show executed commands
53+
logger.ShowExecutedCommands(cfg.ShowExecutedCommands)
54+
55+
return cfg, nil
56+
}
57+
58+
func initializeRegistry(cfg *config.Config) (*registry.DockerProjectRegistry, error) {
59+
return registry.NewDockerProjectRegistry(cfg), nil
60+
}

app/project.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package app
2+
3+
import (
4+
"github.com/marcinhlybin/docker-env/logger"
5+
"github.com/marcinhlybin/docker-env/project"
6+
)
7+
8+
func (ctx *AppContext) CreateProject() (*project.Project, error) {
9+
p, err := project.NewProject(ctx.ProjectName, ctx.ServiceName)
10+
if err != nil {
11+
return nil, err
12+
}
13+
14+
// Set project name from git branch
15+
if ctx.IsBranch {
16+
if err := p.SetProjectNameFromGitBranch(); err != nil {
17+
return nil, err
18+
}
19+
}
20+
21+
return p, nil
22+
}
23+
24+
func (ctx *AppContext) ActiveProject() (*project.Project, error) {
25+
if ctx.IsProjectNameSet {
26+
return ctx.CreateProject()
27+
}
28+
29+
// Fetch active project
30+
reg := ctx.Registry
31+
p, err := reg.ActiveProject()
32+
if err != nil {
33+
return nil, err
34+
}
35+
36+
if p == nil {
37+
logger.Warning("No active project found")
38+
return nil, nil
39+
}
40+
41+
p.SetServiceName(ctx.ServiceName)
42+
return p, nil
43+
}

cmd/app.go

Lines changed: 0 additions & 99 deletions
This file was deleted.

cmd/build.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package cmd
22

33
import (
4+
"fmt"
5+
6+
"github.com/marcinhlybin/docker-env/app"
7+
"github.com/marcinhlybin/docker-env/project"
48
"github.com/urfave/cli/v2"
59
)
610

@@ -9,13 +13,18 @@ var BuildCommand = cli.Command{
913
Aliases: []string{"b"},
1014
Usage: "Build docker images",
1115
Description: `Build docker images.
12-
If environment name is not specified current branch name is used.`,
16+
If project name is not specified, master branch is used.`,
1317
Flags: []cli.Flag{
1418
&cli.StringFlag{
1519
Name: "project",
1620
Aliases: []string{"p"},
1721
Usage: "set a project name",
1822
},
23+
&cli.BoolFlag{
24+
Name: "branch",
25+
Aliases: []string{"b"},
26+
Usage: "use current git branch as project name",
27+
},
1928
&cli.StringFlag{
2029
Name: "service",
2130
Aliases: []string{"s"},
@@ -32,14 +41,34 @@ If environment name is not specified current branch name is used.`,
3241
func buildAction(c *cli.Context) error {
3342
ExitWithErrorOnArgs(c)
3443

35-
app, err := NewApp(c)
44+
// Mutually exclusive flags -p and -b
45+
if c.IsSet("project") && c.IsSet("branch") {
46+
return fmt.Errorf("flags -p and -b are mutually exclusive")
47+
}
48+
49+
ctx, err := app.NewAppContext(c)
3650
if err != nil {
3751
return err
3852
}
3953

40-
p, reg := app.Project, app.Registry
54+
var p *project.Project
4155

42-
noCache := c.Bool("no-cache")
56+
// Use active project name if no project or branch is specified
57+
if !c.IsSet("project") && !c.IsSet("branch") {
58+
p, err = ctx.ActiveProject()
59+
if err != nil {
60+
return err
61+
}
62+
}
4363

44-
return reg.BuildProject(p, noCache)
64+
// No active project found
65+
if p == nil {
66+
p, err = ctx.CreateProject()
67+
if err != nil {
68+
return err
69+
}
70+
}
71+
72+
noCache := c.Bool("no-cache")
73+
return ctx.Registry.BuildProject(p, noCache)
4574
}

0 commit comments

Comments
 (0)