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

Commit e8a6c21

Browse files
Merge pull request #145 from vdemeester/cli-refactoring
cli: refactoring
2 parents 6d0c156 + 0014b05 commit e8a6c21

40 files changed

+593
-616
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ check_go_env:
5656

5757
bin: check_go_env
5858
@echo "Building _build/$(BIN_NAME)$(EXEC_EXT)..."
59-
$(GO_BUILD) -ldflags=$(LDFLAGS) -o _build/$(BIN_NAME)$(EXEC_EXT)
59+
$(GO_BUILD) -ldflags=$(LDFLAGS) -o _build/$(BIN_NAME)$(EXEC_EXT) ./cmd/docker-app
6060

6161
bin-all: check_go_env
6262
@echo "Building for all platforms..."
63-
$(foreach OS, $(OS_LIST), GOOS=$(OS) $(GO_BUILD) -ldflags=$(LDFLAGS) -o _build/$(BIN_NAME)-$(OS)$(if $(filter windows, $(OS)),.exe,) || exit 1;)
63+
$(foreach OS, $(OS_LIST), GOOS=$(OS) $(GO_BUILD) -ldflags=$(LDFLAGS) -o _build/$(BIN_NAME)-$(OS)$(if $(filter windows, $(OS)),.exe,) ./cmd/docker-app || exit 1;)
6464

6565
e2e-all: check_go_env
6666
@echo "Building for all platforms..."

cmd/build.go

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

cmd/deploy.go

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

cmd/docker-app/build.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
log "github.com/sirupsen/logrus"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
// buildCmd represents the build command
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+
},
16+
}
17+
}

cmd/docker-app/deploy.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/docker/cli/cli"
8+
"github.com/docker/lunchbox/internal"
9+
"github.com/docker/lunchbox/renderer"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
var (
14+
deployComposeFiles []string
15+
deploySettingsFiles []string
16+
deployEnv []string
17+
deployOrchestrator string
18+
deployKubeConfig string
19+
deployNamespace string
20+
deployStackName string
21+
)
22+
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+
}
44+
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)")
51+
if internal.Experimental == "on" {
52+
cmd.Flags().StringArrayVarP(&deployComposeFiles, "compose-files", "c", []string{}, "Override Compose files")
53+
}
54+
return cmd
55+
}

cmd/docker-app/helm.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package main
2+
3+
import (
4+
"github.com/docker/cli/cli"
5+
"github.com/docker/lunchbox/internal"
6+
"github.com/docker/lunchbox/renderer"
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var (
11+
helmComposeFiles []string
12+
helmSettingsFile []string
13+
helmEnv []string
14+
helmRender bool
15+
)
16+
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+
}
31+
if internal.Experimental == "on" {
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
36+
be rendered instead of exported as a template.`
37+
}
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
41+
}

cmd/docker-app/image-add.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/docker/lunchbox/image"
8+
"github.com/docker/lunchbox/internal"
9+
"github.com/spf13/cobra"
10+
)
11+
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
23+
images it uses, and saves them from the local docker daemon to the images/
24+
subdirectory.`,
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]
34+
}
35+
return image.Add(args[0], args[1:], imageAddComposeFiles, imageAddSettingsFile, d)
36+
},
37+
}
38+
if internal.Experimental == "on" {
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")
42+
}
43+
return cmd
44+
}

cmd/docker-app/image-load.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package main
2+
3+
import (
4+
"github.com/docker/lunchbox/image"
5+
"github.com/spf13/cobra"
6+
)
7+
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+
},
16+
}
17+
}

cmd/docker-app/init.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"github.com/docker/cli/cli"
5+
"github.com/docker/lunchbox/packager"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var (
10+
initComposeFile string
11+
initDescription string
12+
initMaintainers []string
13+
initSingleFile bool
14+
)
15+
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
32+
}

cmd/docker-app/inspect.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package main
2+
3+
import (
4+
"github.com/docker/cli/cli"
5+
"github.com/docker/lunchbox/renderer"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
// inspectCmd represents the inspect command
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+
}
19+
}

0 commit comments

Comments
 (0)