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

Commit 450c1f3

Browse files
author
Vincent Demeester
authored
Merge pull request #232 from vdemeester/use-docker-cli
Small cmd refactoring to use the same cli helper as docker/cli
2 parents 6fd4fc8 + 8688699 commit 450c1f3

15 files changed

Lines changed: 137 additions & 108 deletions

File tree

cmd/docker-app/deploy.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type deployOptions struct {
2828
}
2929

3030
// deployCmd represents the deploy command
31-
func deployCmd() *cobra.Command {
31+
func deployCmd(dockerCli *command.DockerCli) *cobra.Command {
3232
var opts deployOptions
3333

3434
cmd := &cobra.Command{
@@ -37,7 +37,7 @@ func deployCmd() *cobra.Command {
3737
Long: `Deploy the application on either Swarm or Kubernetes.`,
3838
Args: cli.RequiresMaxArgs(1),
3939
RunE: func(cmd *cobra.Command, args []string) error {
40-
return runDeploy(firstOrEmpty(args), opts)
40+
return runDeploy(dockerCli, firstOrEmpty(args), opts)
4141
},
4242
}
4343

@@ -53,7 +53,7 @@ func deployCmd() *cobra.Command {
5353
return cmd
5454
}
5555

56-
func runDeploy(appname string, opts deployOptions) error {
56+
func runDeploy(dockerCli *command.DockerCli, appname string, opts deployOptions) error {
5757
appname, cleanup, err := packager.Extract(appname)
5858
if err != nil {
5959
return err
@@ -74,8 +74,7 @@ func runDeploy(appname string, opts deployOptions) error {
7474
if err != nil {
7575
return err
7676
}
77-
cli := command.NewDockerCli(os.Stdin, os.Stdout, os.Stderr, true)
78-
cli.Initialize(&cliflags.ClientOptions{
77+
dockerCli.Initialize(&cliflags.ClientOptions{
7978
Common: &cliflags.CommonOptions{
8079
Orchestrator: deployOrchestrator,
8180
},
@@ -86,12 +85,12 @@ func runDeploy(appname string, opts deployOptions) error {
8685
}
8786
if deployOrchestrator == "swarm" {
8887
ctx := context.Background()
89-
return swarm.DeployCompose(ctx, cli, rendered, options.Deploy{
88+
return swarm.DeployCompose(ctx, dockerCli, rendered, options.Deploy{
9089
Namespace: stackName,
9190
})
9291
}
9392
// kube mode
94-
kubeCli, err := kubernetes.WrapCli(cli, kubernetes.Options{
93+
kubeCli, err := kubernetes.WrapCli(dockerCli, kubernetes.Options{
9594
Namespace: opts.deployNamespace,
9695
Config: opts.deployKubeConfig,
9796
})

cmd/docker-app/image-add.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,12 @@ subdirectory.`,
4949
}
5050
}
5151
if !s.IsDir() {
52+
target, err := os.Create(oappname)
53+
if err != nil {
54+
return err
55+
}
5256
// source was a tarball, rebuild it
53-
return packager.Pack(appname, oappname)
57+
return packager.Pack(appname, target)
5458
}
5559
return nil
5660
},

cmd/docker-app/inspect.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ import (
44
"github.com/docker/app/internal/packager"
55
"github.com/docker/app/internal/renderer"
66
"github.com/docker/cli/cli"
7+
"github.com/docker/cli/cli/command"
78
"github.com/spf13/cobra"
89
)
910

1011
// inspectCmd represents the inspect command
11-
func inspectCmd() *cobra.Command {
12+
func inspectCmd(dockerCli command.Cli) *cobra.Command {
1213
return &cobra.Command{
1314
Use: "inspect [<app-name>]",
1415
Short: "Shows metadata and settings for a given application",
@@ -19,7 +20,7 @@ func inspectCmd() *cobra.Command {
1920
return err
2021
}
2122
defer cleanup()
22-
return renderer.Inspect(appname)
23+
return renderer.Inspect(dockerCli.Out(), appname)
2324
},
2425
}
2526
}

cmd/docker-app/main.go

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

33
import (
44
"os"
5+
6+
"github.com/docker/cli/cli/command"
7+
"github.com/docker/docker/pkg/term"
8+
"github.com/sirupsen/logrus"
59
)
610

711
func main() {
8-
cmd := newRootCmd()
12+
// Set terminal emulation based on platform as required.
13+
stdin, stdout, stderr := term.StdStreams()
14+
logrus.SetOutput(stderr)
15+
16+
dockerCli := command.NewDockerCli(stdin, stdout, stderr, false)
17+
cmd := newRootCmd(dockerCli)
918
if err := cmd.Execute(); err != nil {
1019
os.Exit(1)
1120
}

cmd/docker-app/merge.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
11
package main
22

33
import (
4+
"io"
5+
"os"
6+
47
"github.com/docker/app/internal"
58
"github.com/docker/app/internal/packager"
69
"github.com/docker/cli/cli"
10+
"github.com/docker/cli/cli/command"
711
"github.com/spf13/cobra"
812
)
913

1014
var mergeOutputFile string
1115

12-
func mergeCmd() *cobra.Command {
16+
func mergeCmd(dockerCli command.Cli) *cobra.Command {
1317
cmd := &cobra.Command{
1418
Use: "merge [<app-name>] [-o output_dir]",
1519
Short: "Merge the application as a single file multi-document YAML",
1620
Args: cli.RequiresMaxArgs(1),
1721
RunE: func(cmd *cobra.Command, args []string) error {
18-
return packager.Merge(firstOrEmpty(args), mergeOutputFile)
22+
appname, cleanup, err := packager.Extract(firstOrEmpty(args))
23+
if err != nil {
24+
return err
25+
}
26+
defer cleanup()
27+
var target io.Writer
28+
if mergeOutputFile == "-" {
29+
target = dockerCli.Out()
30+
} else {
31+
target, err = os.Create(mergeOutputFile)
32+
if err != nil {
33+
return err
34+
}
35+
defer target.(io.WriteCloser).Close()
36+
}
37+
return packager.Merge(appname, target)
1938
},
2039
}
2140
if internal.Experimental == "on" {

cmd/docker-app/pack.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,43 @@
11
package main
22

33
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
48
"github.com/docker/app/internal/packager"
59
"github.com/docker/cli/cli"
10+
"github.com/docker/cli/cli/command"
611
"github.com/spf13/cobra"
12+
"golang.org/x/crypto/ssh/terminal"
713
)
814

915
var packOutputFile string
1016

11-
func packCmd() *cobra.Command {
17+
func packCmd(dockerCli command.Cli) *cobra.Command {
1218
cmd := &cobra.Command{
1319
Use: "pack [<app-name>] [-o output_file]",
1420
Short: "Pack the application as a single file",
1521
Args: cli.RequiresMaxArgs(1),
1622
RunE: func(cmd *cobra.Command, args []string) error {
17-
return packager.Pack(firstOrEmpty(args), packOutputFile)
23+
appname := firstOrEmpty(args)
24+
appname, cleanup, err := packager.Extract(appname)
25+
if err != nil {
26+
return err
27+
}
28+
defer cleanup()
29+
var target io.Writer
30+
if packOutputFile == "-" {
31+
if terminal.IsTerminal(int(dockerCli.Out().FD())) {
32+
return fmt.Errorf("Refusing to output to a terminal, use a shell redirect or the '-o' option")
33+
}
34+
} else {
35+
target, err = os.Create(packOutputFile)
36+
if err != nil {
37+
return err
38+
}
39+
}
40+
return packager.Pack(appname, target)
1841
},
1942
}
2043
cmd.Flags().StringVarP(&packOutputFile, "output", "o", "-", "Output file (- for stdout)")

cmd/docker-app/render.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/docker/app/internal/packager"
99
"github.com/docker/app/internal/renderer"
1010
"github.com/docker/cli/cli"
11+
"github.com/docker/cli/cli/command"
1112
"github.com/spf13/cobra"
1213
"gopkg.in/yaml.v2"
1314
)
@@ -19,7 +20,7 @@ var (
1920
renderOutput string
2021
)
2122

22-
func renderCmd() *cobra.Command {
23+
func renderCmd(dockerCli command.Cli) *cobra.Command {
2324
cmd := &cobra.Command{
2425
Use: "render <app-name> [-s key=value...] [-f settings-file...]",
2526
Short: "Render the Compose file for the application",
@@ -44,7 +45,7 @@ func renderCmd() *cobra.Command {
4445
return err
4546
}
4647
if renderOutput == "-" {
47-
fmt.Print(string(res))
48+
fmt.Fprint(dockerCli.Out(), string(res))
4849
} else {
4950
f, err := os.Create(renderOutput)
5051
if err != nil {

cmd/docker-app/root.go

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,36 +5,14 @@ import (
55
"strings"
66

77
"github.com/docker/app/internal"
8+
"github.com/docker/cli/cli/command"
89
log "github.com/sirupsen/logrus"
910
"github.com/spf13/cobra"
1011
)
1112

12-
var (
13-
commands = []*cobra.Command{
14-
deployCmd(),
15-
helmCmd(),
16-
initCmd(),
17-
inspectCmd(),
18-
lsCmd(),
19-
pushCmd(),
20-
renderCmd(),
21-
saveCmd(),
22-
versionCmd(),
23-
}
24-
experimentalCommands = []*cobra.Command{
25-
imageAddCmd(),
26-
imageLoadCmd(),
27-
loadCmd(),
28-
mergeCmd(),
29-
packCmd(),
30-
pullCmd(),
31-
splitCmd(),
32-
unpackCmd(),
33-
}
34-
)
35-
3613
// rootCmd represents the base command when called without any subcommands
37-
func newRootCmd() *cobra.Command {
14+
// FIXME(vdemeester) use command.Cli interface
15+
func newRootCmd(dockerCli *command.DockerCli) *cobra.Command {
3816
cmd := &cobra.Command{
3917
Use: "docker-app",
4018
Short: "Docker App Packages",
@@ -48,15 +26,35 @@ func newRootCmd() *cobra.Command {
4826
},
4927
}
5028
cmd.PersistentFlags().BoolVar(&internal.Debug, "debug", false, "Enable debug mode")
51-
for _, c := range commands {
52-
cmd.AddCommand(c)
53-
}
29+
addCommands(cmd, dockerCli)
30+
return cmd
31+
}
32+
33+
// addCommands adds all the commands from cli/command to the root command
34+
func addCommands(cmd *cobra.Command, dockerCli *command.DockerCli) {
35+
cmd.AddCommand(
36+
deployCmd(dockerCli),
37+
helmCmd(),
38+
initCmd(),
39+
inspectCmd(dockerCli),
40+
lsCmd(),
41+
pushCmd(),
42+
renderCmd(dockerCli),
43+
saveCmd(dockerCli),
44+
versionCmd(dockerCli),
45+
)
5446
if internal.Experimental == "on" {
55-
for _, c := range experimentalCommands {
56-
cmd.AddCommand(c)
57-
}
47+
cmd.AddCommand(
48+
imageAddCmd(),
49+
imageLoadCmd(),
50+
loadCmd(),
51+
mergeCmd(dockerCli),
52+
packCmd(dockerCli),
53+
pullCmd(),
54+
splitCmd(),
55+
unpackCmd(),
56+
)
5857
}
59-
return cmd
6058
}
6159

6260
func firstOrEmpty(list []string) string {

cmd/docker-app/save.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
"github.com/docker/app/internal/packager"
77
"github.com/docker/cli/cli"
8+
"github.com/docker/cli/cli/command"
89
"github.com/spf13/cobra"
910
)
1011

@@ -13,7 +14,7 @@ type saveOptions struct {
1314
tag string
1415
}
1516

16-
func saveCmd() *cobra.Command {
17+
func saveCmd(dockerCli command.Cli) *cobra.Command {
1718
var opts saveOptions
1819
cmd := &cobra.Command{
1920
Use: "save [<app-name>]",
@@ -22,7 +23,7 @@ func saveCmd() *cobra.Command {
2223
RunE: func(cmd *cobra.Command, args []string) error {
2324
imageName, err := packager.Save(firstOrEmpty(args), opts.namespace, opts.tag)
2425
if imageName != "" && err == nil {
25-
fmt.Printf("Saved application as image: %s\n", imageName)
26+
fmt.Fprintf(dockerCli.Out(), "Saved application as image: %s\n", imageName)
2627
}
2728
return err
2829
},

cmd/docker-app/split.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ func splitCmd() *cobra.Command {
1515
Short: "Split a single-file application into multiple files",
1616
Args: cli.RequiresMaxArgs(1),
1717
RunE: func(cmd *cobra.Command, args []string) error {
18-
return packager.Split(firstOrEmpty(args), splitOutputDir)
18+
appname, cleanup, err := packager.Extract(firstOrEmpty(args))
19+
if err != nil {
20+
return err
21+
}
22+
defer cleanup()
23+
return packager.Split(appname, splitOutputDir)
1924
},
2025
}
2126
if internal.Experimental == "on" {
22-
cmd.Flags().StringVarP(&splitOutputDir, "output", "o", "-", "Output directory")
27+
cmd.Flags().StringVarP(&splitOutputDir, "output", "o", ".", "Output directory")
2328
}
2429
return cmd
2530
}

0 commit comments

Comments
 (0)