Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

Commit 0014b05

Browse files
committed
cli:refactoring
- use go convetion of `cmd/…` with having main package in `cmd/docker-app` - less `init()` function, should make the cli testable later on with few additions - some style changes Signed-off-by: Vincent Demeester <[email protected]>
1 parent 6914ba5 commit 0014b05

File tree

19 files changed

+336
-358
lines changed

19 files changed

+336
-358
lines changed

cmd/docker-app/build.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,17 @@
11
package main
22

33
import (
4-
"github.com/docker/lunchbox/internal"
54
log "github.com/sirupsen/logrus"
65
"github.com/spf13/cobra"
76
)
87

98
// buildCmd represents the build command
10-
var buildCmd = &cobra.Command{
11-
Use: "build <app-name>",
12-
Short: "Compile an app package from locally available data",
13-
Run: func(cmd *cobra.Command, args []string) {
14-
log.Info("build called")
15-
},
16-
}
17-
18-
func init() {
19-
if internal.Experimental == "on" {
20-
rootCmd.AddCommand(buildCmd)
9+
func buildCmd() *cobra.Command {
10+
return &cobra.Command{
11+
Use: "build <app-name>",
12+
Short: "Compile an app package from locally available data",
13+
Run: func(cmd *cobra.Command, args []string) {
14+
log.Info("build called")
15+
},
2116
}
2217
}

cmd/docker-app/deploy.go

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,46 @@ import (
1010
"github.com/spf13/cobra"
1111
)
1212

13-
// deployCmd represents the deploy command
14-
var deployCmd = &cobra.Command{
15-
Use: "deploy [<app-name>]",
16-
Short: "Deploy or update an application",
17-
Long: `Deploy the application on either Swarm or Kubernetes.`,
18-
Args: cli.RequiresMaxArgs(1),
19-
RunE: func(cmd *cobra.Command, args []string) error {
20-
if do, ok := os.LookupEnv("DOCKER_ORCHESTRATOR"); ok {
21-
deployOrchestrator = do
22-
}
23-
if deployOrchestrator != "swarm" && deployOrchestrator != "kubernetes" {
24-
return fmt.Errorf("orchestrator must be either 'swarm' or 'kubernetes'")
25-
}
26-
d, err := parseSettings(deployEnv)
27-
if err != nil {
28-
return err
29-
}
30-
return renderer.Deploy(firstOrEmpty(args), deployComposeFiles, deploySettingsFiles, d, deployStackName, deployOrchestrator, deployKubeConfig, deployNamespace)
31-
},
32-
}
13+
var (
14+
deployComposeFiles []string
15+
deploySettingsFiles []string
16+
deployEnv []string
17+
deployOrchestrator string
18+
deployKubeConfig string
19+
deployNamespace string
20+
deployStackName string
21+
)
3322

34-
var deployComposeFiles []string
35-
var deploySettingsFiles []string
36-
var deployEnv []string
37-
var deployOrchestrator string
38-
var deployKubeConfig string
39-
var deployNamespace string
40-
var deployStackName string
23+
// deployCmd represents the deploy command
24+
func deployCmd() *cobra.Command {
25+
cmd := &cobra.Command{
26+
Use: "deploy [<app-name>]",
27+
Short: "Deploy or update an application",
28+
Long: `Deploy the application on either Swarm or Kubernetes.`,
29+
Args: cli.RequiresMaxArgs(1),
30+
RunE: func(cmd *cobra.Command, args []string) error {
31+
if do, ok := os.LookupEnv("DOCKER_ORCHESTRATOR"); ok {
32+
deployOrchestrator = do
33+
}
34+
if deployOrchestrator != "swarm" && deployOrchestrator != "kubernetes" {
35+
return fmt.Errorf("orchestrator must be either 'swarm' or 'kubernetes'")
36+
}
37+
d, err := parseSettings(deployEnv)
38+
if err != nil {
39+
return err
40+
}
41+
return renderer.Deploy(firstOrEmpty(args), deployComposeFiles, deploySettingsFiles, d, deployStackName, deployOrchestrator, deployKubeConfig, deployNamespace)
42+
},
43+
}
4144

42-
func init() {
43-
rootCmd.AddCommand(deployCmd)
44-
deployCmd.Flags().StringArrayVarP(&deploySettingsFiles, "settings-files", "f", []string{}, "Override settings files")
45-
deployCmd.Flags().StringArrayVarP(&deployEnv, "set", "s", []string{}, "Override settings values")
46-
deployCmd.Flags().StringVarP(&deployOrchestrator, "orchestrator", "o", "swarm", "Orchestrator to deploy on (swarm, kubernetes)")
47-
deployCmd.Flags().StringVarP(&deployKubeConfig, "kubeconfig", "k", "", "kubeconfig file to use")
48-
deployCmd.Flags().StringVarP(&deployNamespace, "namespace", "n", "default", "namespace to deploy into")
49-
deployCmd.Flags().StringVarP(&deployStackName, "name", "d", "", "stack name (default: app name)")
45+
cmd.Flags().StringArrayVarP(&deploySettingsFiles, "settings-files", "f", []string{}, "Override settings files")
46+
cmd.Flags().StringArrayVarP(&deployEnv, "set", "s", []string{}, "Override settings values")
47+
cmd.Flags().StringVarP(&deployOrchestrator, "orchestrator", "o", "swarm", "Orchestrator to deploy on (swarm, kubernetes)")
48+
cmd.Flags().StringVarP(&deployKubeConfig, "kubeconfig", "k", "", "kubeconfig file to use")
49+
cmd.Flags().StringVarP(&deployNamespace, "namespace", "n", "default", "namespace to deploy into")
50+
cmd.Flags().StringVarP(&deployStackName, "name", "d", "", "stack name (default: app name)")
5051
if internal.Experimental == "on" {
51-
deployCmd.Flags().StringArrayVarP(&deployComposeFiles, "compose-files", "c", []string{}, "Override Compose files")
52+
cmd.Flags().StringArrayVarP(&deployComposeFiles, "compose-files", "c", []string{}, "Override Compose files")
5253
}
54+
return cmd
5355
}

cmd/docker-app/helm.go

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,35 @@ import (
77
"github.com/spf13/cobra"
88
)
99

10-
var helmCmd = &cobra.Command{
11-
Use: "helm [<app-name>] [-s key=value...] [-f settings-file...]",
12-
Short: "Generate a Helm chart",
13-
Long: `Generate a Helm chart for the application.`,
14-
Args: cli.RequiresMaxArgs(1),
15-
RunE: func(cmd *cobra.Command, args []string) error {
16-
d, err := parseSettings(helmEnv)
17-
if err != nil {
18-
return err
19-
}
20-
return renderer.Helm(firstOrEmpty(args), helmComposeFiles, helmSettingsFile, d, helmRender)
21-
},
22-
}
23-
24-
var helmComposeFiles []string
25-
var helmSettingsFile []string
26-
var helmEnv []string
27-
var helmRender bool
10+
var (
11+
helmComposeFiles []string
12+
helmSettingsFile []string
13+
helmEnv []string
14+
helmRender bool
15+
)
2816

29-
func init() {
30-
rootCmd.AddCommand(helmCmd)
17+
func helmCmd() *cobra.Command {
18+
cmd := &cobra.Command{
19+
Use: "helm [<app-name>] [-s key=value...] [-f settings-file...]",
20+
Short: "Generate a Helm chart",
21+
Long: `Generate a Helm chart for the application.`,
22+
Args: cli.RequiresMaxArgs(1),
23+
RunE: func(cmd *cobra.Command, args []string) error {
24+
d, err := parseSettings(helmEnv)
25+
if err != nil {
26+
return err
27+
}
28+
return renderer.Helm(firstOrEmpty(args), helmComposeFiles, helmSettingsFile, d, helmRender)
29+
},
30+
}
3131
if internal.Experimental == "on" {
32-
helmCmd.Flags().StringArrayVarP(&helmComposeFiles, "compose-files", "c", []string{}, "Override Compose files")
33-
helmCmd.Use += " [-c <compose-files>...]"
34-
helmCmd.Flags().BoolVarP(&helmRender, "render", "r", false, "Render the template instead of exporting it")
35-
helmCmd.Long += ` If the --render option is used, the docker-compose.yml will
32+
cmd.Flags().StringArrayVarP(&helmComposeFiles, "compose-files", "c", []string{}, "Override Compose files")
33+
cmd.Use += " [-c <compose-files>...]"
34+
cmd.Flags().BoolVarP(&helmRender, "render", "r", false, "Render the template instead of exporting it")
35+
cmd.Long += ` If the --render option is used, the docker-compose.yml will
3636
be rendered instead of exported as a template.`
3737
}
38-
helmCmd.Flags().StringArrayVarP(&helmSettingsFile, "settings-files", "f", []string{}, "Override settings files")
39-
helmCmd.Flags().StringArrayVarP(&helmEnv, "set", "s", []string{}, "Override settings values")
38+
cmd.Flags().StringArrayVarP(&helmSettingsFile, "settings-files", "f", []string{}, "Override settings files")
39+
cmd.Flags().StringArrayVarP(&helmEnv, "set", "s", []string{}, "Override settings values")
40+
return cmd
4041
}

cmd/docker-app/image-add.go

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,36 @@ import (
99
"github.com/spf13/cobra"
1010
)
1111

12-
var imageAddCmd = &cobra.Command{
13-
Use: "image-add <app-name> [services...]",
14-
Short: "Add images for given services (default: all) to the app package",
15-
Long: `This command renders the app's docker-compose.yml file, looks for the
12+
var (
13+
imageAddComposeFiles []string
14+
imageAddSettingsFile []string
15+
imageAddEnv []string
16+
)
17+
18+
func imageAddCmd() *cobra.Command {
19+
cmd := &cobra.Command{
20+
Use: "image-add <app-name> [services...]",
21+
Short: "Add images for given services (default: all) to the app package",
22+
Long: `This command renders the app's docker-compose.yml file, looks for the
1623
images it uses, and saves them from the local docker daemon to the images/
1724
subdirectory.`,
18-
Args: cobra.MinimumNArgs(1),
19-
RunE: func(cmd *cobra.Command, args []string) error {
20-
d := make(map[string]string)
21-
for _, v := range imageAddEnv {
22-
kv := strings.SplitN(v, "=", 2)
23-
if len(kv) != 2 {
24-
return fmt.Errorf("Malformed env input: '%s'", v)
25+
Args: cobra.MinimumNArgs(1),
26+
RunE: func(cmd *cobra.Command, args []string) error {
27+
d := make(map[string]string)
28+
for _, v := range imageAddEnv {
29+
kv := strings.SplitN(v, "=", 2)
30+
if len(kv) != 2 {
31+
return fmt.Errorf("Malformed env input: '%s'", v)
32+
}
33+
d[kv[0]] = kv[1]
2534
}
26-
d[kv[0]] = kv[1]
27-
}
28-
return image.Add(args[0], args[1:], imageAddComposeFiles, imageAddSettingsFile, d)
29-
},
30-
}
31-
var imageAddComposeFiles []string
32-
var imageAddSettingsFile []string
33-
var imageAddEnv []string
34-
35-
func init() {
35+
return image.Add(args[0], args[1:], imageAddComposeFiles, imageAddSettingsFile, d)
36+
},
37+
}
3638
if internal.Experimental == "on" {
37-
rootCmd.AddCommand(imageAddCmd)
38-
imageAddCmd.Flags().StringArrayVarP(&imageAddComposeFiles, "compose-files", "c", []string{}, "Override Compose files")
39-
imageAddCmd.Flags().StringArrayVarP(&imageAddSettingsFile, "settings-files", "s", []string{}, "Override settings files")
40-
imageAddCmd.Flags().StringArrayVarP(&imageAddEnv, "env", "e", []string{}, "Override environment values")
39+
cmd.Flags().StringArrayVarP(&imageAddComposeFiles, "compose-files", "c", []string{}, "Override Compose files")
40+
cmd.Flags().StringArrayVarP(&imageAddSettingsFile, "settings-files", "s", []string{}, "Override settings files")
41+
cmd.Flags().StringArrayVarP(&imageAddEnv, "env", "e", []string{}, "Override environment values")
4142
}
43+
return cmd
4244
}

cmd/docker-app/image-load.go

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,16 @@ package main
22

33
import (
44
"github.com/docker/lunchbox/image"
5-
"github.com/docker/lunchbox/internal"
65
"github.com/spf13/cobra"
76
)
87

9-
var imageLoadCmd = &cobra.Command{
10-
Use: "image-load <app-name> [services...]",
11-
Short: "Load stored images for given services (default: all) to the local docker daemon",
12-
Args: cobra.MinimumNArgs(1),
13-
RunE: func(cmd *cobra.Command, args []string) error {
14-
return image.Load(args[0], args[1:])
15-
},
16-
}
17-
18-
func init() {
19-
if internal.Experimental == "on" {
20-
rootCmd.AddCommand(imageLoadCmd)
8+
func imageLoadCmd() *cobra.Command {
9+
return &cobra.Command{
10+
Use: "image-load <app-name> [services...]",
11+
Short: "Load stored images for given services (default: all) to the local docker daemon",
12+
Args: cobra.MinimumNArgs(1),
13+
RunE: func(cmd *cobra.Command, args []string) error {
14+
return image.Load(args[0], args[1:])
15+
},
2116
}
2217
}

cmd/docker-app/init.go

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,27 @@ import (
66
"github.com/spf13/cobra"
77
)
88

9-
// initCmd represents the init command
10-
var initCmd = &cobra.Command{
11-
Use: "init <app-name> [-c <compose-file>] [-d <description>] [-m name:email ...]",
12-
Short: "Start building a Docker application",
13-
Long: `Start building a Docker application. Will automatically detect a docker-compose.yml file in the current directory.`,
14-
Args: cli.ExactArgs(1),
15-
RunE: func(cmd *cobra.Command, args []string) error {
16-
return packager.Init(args[0], initComposeFile, initDescription, initMaintainers, initSingleFile)
17-
},
18-
}
19-
20-
var initComposeFile string
21-
var initDescription string
22-
var initMaintainers []string
23-
var initSingleFile bool
9+
var (
10+
initComposeFile string
11+
initDescription string
12+
initMaintainers []string
13+
initSingleFile bool
14+
)
2415

25-
func init() {
26-
rootCmd.AddCommand(initCmd)
27-
initCmd.Flags().StringVarP(&initComposeFile, "compose-file", "c", "", "Initial Compose file (optional)")
28-
initCmd.Flags().StringVarP(&initDescription, "description", "d", "", "Initial description (optional)")
29-
initCmd.Flags().StringArrayVarP(&initMaintainers, "maintainer", "m", []string{}, "Maintainer (name:email) (optional)")
30-
initCmd.Flags().BoolVarP(&initSingleFile, "single-file", "s", false, "Create a single-file application")
16+
// initCmd represents the init command
17+
func initCmd() *cobra.Command {
18+
cmd := &cobra.Command{
19+
Use: "init <app-name> [-c <compose-file>] [-d <description>] [-m name:email ...]",
20+
Short: "Start building a Docker application",
21+
Long: `Start building a Docker application. Will automatically detect a docker-compose.yml file in the current directory.`,
22+
Args: cli.ExactArgs(1),
23+
RunE: func(cmd *cobra.Command, args []string) error {
24+
return packager.Init(args[0], initComposeFile, initDescription, initMaintainers, initSingleFile)
25+
},
26+
}
27+
cmd.Flags().StringVarP(&initComposeFile, "compose-file", "c", "", "Initial Compose file (optional)")
28+
cmd.Flags().StringVarP(&initDescription, "description", "d", "", "Initial description (optional)")
29+
cmd.Flags().StringArrayVarP(&initMaintainers, "maintainer", "m", []string{}, "Maintainer (name:email) (optional)")
30+
cmd.Flags().BoolVarP(&initSingleFile, "single-file", "s", false, "Create a single-file application")
31+
return cmd
3132
}

cmd/docker-app/inspect.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ import (
77
)
88

99
// inspectCmd represents the inspect command
10-
var inspectCmd = &cobra.Command{
11-
Use: "inspect [<app-name>]",
12-
Short: "Shows metadata and settings for a given application",
13-
Args: cli.RequiresMaxArgs(1),
14-
RunE: func(cmd *cobra.Command, args []string) error {
15-
return renderer.Inspect(firstOrEmpty(args))
16-
},
17-
}
18-
19-
func init() {
20-
rootCmd.AddCommand(inspectCmd)
10+
func inspectCmd() *cobra.Command {
11+
return &cobra.Command{
12+
Use: "inspect [<app-name>]",
13+
Short: "Shows metadata and settings for a given application",
14+
Args: cli.RequiresMaxArgs(1),
15+
RunE: func(cmd *cobra.Command, args []string) error {
16+
return renderer.Inspect(firstOrEmpty(args))
17+
},
18+
}
2119
}

cmd/docker-app/load.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ import (
66
"github.com/spf13/cobra"
77
)
88

9-
var loadCmd = &cobra.Command{
10-
Use: "load <repotag>",
11-
Short: "Load an application from a docker image",
12-
Args: cli.RequiresMaxArgs(1),
13-
RunE: func(cmd *cobra.Command, args []string) error {
14-
return packager.Load(firstOrEmpty(args), ".")
15-
},
16-
}
17-
18-
func init() {
19-
rootCmd.AddCommand(loadCmd)
9+
func loadCmd() *cobra.Command {
10+
return &cobra.Command{
11+
Use: "load <repotag>",
12+
Short: "Load an application from a docker image",
13+
Args: cli.RequiresMaxArgs(1),
14+
RunE: func(cmd *cobra.Command, args []string) error {
15+
return packager.Load(firstOrEmpty(args), ".")
16+
},
17+
}
2018
}

cmd/docker-app/main.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
package main
22

3+
import (
4+
"fmt"
5+
"os"
6+
)
7+
38
func main() {
4-
Execute()
9+
cmd := newRootCmd()
10+
if err := cmd.Execute(); err != nil {
11+
fmt.Fprintln(os.Stderr, err)
12+
os.Exit(1)
13+
}
514
}

0 commit comments

Comments
 (0)