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

Commit ac854b9

Browse files
silvin-lubeckichris-crone
authored andcommitted
Convert docker-app to a docker cli plugin
* Use the docker/cli/cli-plugins/plugin.Run helper to create and execute the commands as a plugin * Refactor e2e tests to use “docker app” (docker cli invoking docker-app plugin) * Add CLI plugin invocation e2e tests * Bump docker-cli to include the plugin work * Bump gotest.tools to 2.3.0 Signed-off-by: Silvin Lubecki <silvin.lubecki@docker.com>
1 parent 36fd67f commit ac854b9

24 files changed

Lines changed: 485 additions & 211 deletions

File tree

Gopkg.lock

Lines changed: 10 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ required = ["github.com/wadey/gocovmerge"]
3838

3939
[[override]]
4040
name = "github.com/docker/cli"
41-
revision = "06b837a7d7e1115f3d2aa65c47765e25d4bf845b"
41+
revision = "3ddb3133f5b5a74dd4e3f721ced21f4d0b9651b6"
4242

4343
[[override]]
4444
name = "github.com/deislabs/duffle"

cmd/docker-app/inspect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func inspectCmd(dockerCli command.Cli) *cobra.Command {
3333
}
3434

3535
func runInspect(dockerCli command.Cli, appname string, opts inspectOptions) error {
36-
muteDockerCli(dockerCli)
36+
defer muteDockerCli(dockerCli)()
3737

3838
c, err := claim.New("inspect")
3939
if err != nil {

cmd/docker-app/install.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func installCmd(dockerCli command.Cli) *cobra.Command {
6868
}
6969

7070
func runInstall(dockerCli command.Cli, appname string, opts installOptions) error {
71-
muteDockerCli(dockerCli)
71+
defer muteDockerCli(dockerCli)()
7272
if opts.sendRegistryAuth {
7373
return errors.New("with-registry-auth is not supported at the moment")
7474
}

cmd/docker-app/main.go

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

33
import (
4-
"fmt"
5-
"os"
6-
4+
"github.com/docker/app/internal"
5+
"github.com/docker/cli/cli-plugins/manager"
6+
"github.com/docker/cli/cli-plugins/plugin"
77
"github.com/docker/cli/cli/command"
8-
"github.com/sirupsen/logrus"
8+
"github.com/spf13/cobra"
99
)
1010

1111
func main() {
12-
dockerCli, err := command.NewDockerCli()
13-
if err != nil {
14-
fmt.Fprintln(os.Stderr, err)
15-
os.Exit(1)
16-
}
17-
logrus.SetOutput(dockerCli.Err())
18-
cmd := newRootCmd(dockerCli)
19-
if err := cmd.Execute(); err != nil {
20-
os.Exit(1)
21-
}
12+
plugin.Run(func(dockerCli command.Cli) *cobra.Command {
13+
return newRootCmd(dockerCli)
14+
}, manager.Metadata{
15+
SchemaVersion: "0.1.0",
16+
Vendor: "Docker Inc.",
17+
Version: internal.Version,
18+
})
2219
}

cmd/docker-app/root.go

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

33
import (
4-
"fmt"
54
"io/ioutil"
65

7-
"github.com/docker/app/internal"
8-
"github.com/docker/cli/cli"
96
"github.com/docker/cli/cli/command"
10-
cliflags "github.com/docker/cli/cli/flags"
117
"github.com/spf13/cobra"
128
"github.com/spf13/pflag"
139
)
1410

1511
// rootCmd represents the base command when called without any subcommands
16-
func newRootCmd(dockerCli *command.DockerCli) *cobra.Command {
17-
var (
18-
opts *cliflags.ClientOptions
19-
flags *pflag.FlagSet
20-
)
21-
12+
// FIXME(vdemeester) use command.Cli interface
13+
func newRootCmd(dockerCli command.Cli) *cobra.Command {
2214
cmd := &cobra.Command{
23-
Use: "docker-app",
24-
Short: "Docker Application Packages",
25-
Long: `Build and deploy Docker Application Packages.`,
26-
SilenceUsage: true,
27-
TraverseChildren: true,
28-
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
29-
opts.Common.SetDefaultOptions(flags)
30-
return dockerCli.Initialize(opts)
31-
},
32-
Version: fmt.Sprintf("%s, build %s", internal.Version, internal.GitCommit),
15+
Use: "app",
16+
Short: "Docker Application Packages",
17+
Long: `Build and deploy Docker Application Packages.`,
3318
}
34-
opts, flags, _ = cli.SetupRootCommand(cmd)
35-
flags.BoolP("version", "v", false, "Print version information")
36-
cmd.SetVersionTemplate("docker-app version {{.Version}}\n")
3719
addCommands(cmd, dockerCli)
3820
return cmd
3921
}
@@ -66,8 +48,13 @@ func firstOrEmpty(list []string) string {
6648
return ""
6749
}
6850

69-
func muteDockerCli(dockerCli command.Cli) {
51+
func muteDockerCli(dockerCli command.Cli) func() {
52+
stdout := dockerCli.Out()
53+
stderr := dockerCli.Err()
7054
dockerCli.Apply(command.WithCombinedStreams(ioutil.Discard))
55+
return func() {
56+
dockerCli.Apply(command.WithOutputStream(stdout), command.WithErrorStream(stderr))
57+
}
7158
}
7259

7360
type parametersOptions struct {

cmd/docker-app/status.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func statusCmd(dockerCli command.Cli) *cobra.Command {
2929
}
3030

3131
func runStatus(dockerCli command.Cli, claimName string, opts credentialOptions) error {
32-
muteDockerCli(dockerCli)
32+
defer muteDockerCli(dockerCli)()
3333
h := duffleHome()
3434

3535
claimStore := claim.NewClaimStore(crud.NewFileSystemStore(h.Claims(), "json"))

cmd/docker-app/uninstall.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func uninstallCmd(dockerCli command.Cli) *cobra.Command {
2929
}
3030

3131
func runUninstall(dockerCli command.Cli, claimName string, opts credentialOptions) error {
32-
muteDockerCli(dockerCli)
32+
defer muteDockerCli(dockerCli)()
3333
h := duffleHome()
3434

3535
claimStore := claim.NewClaimStore(crud.NewFileSystemStore(h.Claims(), "json"))

cmd/docker-app/upgrade.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func upgradeCmd(dockerCli command.Cli) *cobra.Command {
3939
}
4040

4141
func runUpgrade(dockerCli command.Cli, installationName string, opts upgradeOptions) error {
42-
muteDockerCli(dockerCli)
42+
defer muteDockerCli(dockerCli)()
4343
targetContext := getTargetContext(opts.targetContext, dockerCli.CurrentContext())
4444
h := duffleHome()
4545
claimStore := claim.NewClaimStore(crud.NewFileSystemStore(h.Claims(), "json"))

e2e/cnab_test.go

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package e2e
22

33
import (
44
"fmt"
5+
"os"
56
"path"
67
"runtime"
78
"testing"
@@ -28,7 +29,7 @@ func TestCallCustomStatusAction(t *testing.T) {
2829
{
2930
name: "missingCustomStatusAction",
3031
exitCode: 1,
31-
expectedOutput: "Error: Status failed: action not defined for bundle",
32+
expectedOutput: "Status failed: action not defined for bundle",
3233
cnab: "cnab-without-status",
3334
},
3435
}
@@ -38,9 +39,7 @@ func TestCallCustomStatusAction(t *testing.T) {
3839
tmpDir := fs.NewDir(t, t.Name())
3940
defer tmpDir.Remove()
4041
testDir := path.Join("testdata", testCase.cnab)
41-
cmd := icmd.Cmd{
42-
Env: []string{fmt.Sprintf("DUFFLE_HOME=%s", tmpDir.Path())},
43-
}
42+
cmd := icmd.Cmd{Env: append(os.Environ(), fmt.Sprintf("DUFFLE_HOME=%s", tmpDir.Path()))}
4443

4544
// We need to explicitly set the SYSTEMROOT on windows
4645
// otherwise we get the error:
@@ -50,21 +49,21 @@ func TestCallCustomStatusAction(t *testing.T) {
5049
cmd.Env = append(cmd.Env, `SYSTEMROOT=C:\WINDOWS`)
5150
}
5251
// Build CNAB invocation image
53-
cmd.Command = []string{"docker", "build", "-f", path.Join(testDir, "cnab", "build", "Dockerfile"), "-t", fmt.Sprintf("e2e/%s:v0.1.0", testCase.cnab), testDir}
52+
cmd.Command = dockerCli.Command("build", "--file", path.Join(testDir, "cnab", "build", "Dockerfile"), "--tag", fmt.Sprintf("e2e/%s:v0.1.0", testCase.cnab), testDir)
5453
icmd.RunCmd(cmd).Assert(t, icmd.Success)
5554

56-
// docker-app install
57-
cmd.Command = []string{dockerApp, "install", path.Join(testDir, "bundle.json"), "--name", testCase.name}
55+
// docker app install
56+
cmd.Command = dockerCli.Command("app", "install", path.Join(testDir, "bundle.json"), "--name", testCase.name)
5857
icmd.RunCmd(cmd).Assert(t, icmd.Success)
5958

60-
// docker-app uninstall
59+
// docker app uninstall
6160
defer func() {
62-
cmd.Command = []string{dockerApp, "uninstall", testCase.name}
61+
cmd.Command = dockerCli.Command("app", "uninstall", testCase.name)
6362
icmd.RunCmd(cmd).Assert(t, icmd.Success)
6463
}()
6564

66-
// docker-app status
67-
cmd.Command = []string{dockerApp, "status", testCase.name}
65+
// docker app status
66+
cmd.Command = dockerCli.Command("app", "status", testCase.name)
6867
result := icmd.RunCmd(cmd)
6968
result.Assert(t, icmd.Expected{ExitCode: testCase.exitCode})
7069
assert.Assert(t, is.Contains(result.Combined(), testCase.expectedOutput))

0 commit comments

Comments
 (0)