From 268c02523a4d60c49a2b7a2e76a3cfcb1f3c81e9 Mon Sep 17 00:00:00 2001 From: Guillaume Tardif Date: Mon, 21 Sep 2020 15:39:05 +0200 Subject: [PATCH] Only add `--domainname` flag to `docker run` in ACI context Signed-off-by: Guillaume Tardif --- cli/cmd/run/run.go | 11 +++++++---- cli/cmd/run/run_test.go | 12 +++++++++++- cli/main.go | 3 ++- cli/main_test.go | 2 +- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/cli/cmd/run/run.go b/cli/cmd/run/run.go index 5c3f36f58..0c1559b3b 100644 --- a/cli/cmd/run/run.go +++ b/cli/cmd/run/run.go @@ -25,15 +25,15 @@ import ( "github.com/containerd/console" "github.com/spf13/cobra" - "github.com/docker/compose-cli/api/containers" - "github.com/docker/compose-cli/api/client" + "github.com/docker/compose-cli/api/containers" "github.com/docker/compose-cli/cli/options/run" + "github.com/docker/compose-cli/context/store" "github.com/docker/compose-cli/progress" ) // Command runs a container -func Command() *cobra.Command { +func Command(contextType string) *cobra.Command { var opts run.Opts cmd := &cobra.Command{ Use: "run", @@ -46,7 +46,6 @@ func Command() *cobra.Command { cmd.Flags().StringArrayVarP(&opts.Publish, "publish", "p", []string{}, "Publish a container's port(s). [HOST_PORT:]CONTAINER_PORT") cmd.Flags().StringVar(&opts.Name, "name", "", "Assign a name to the container") - cmd.Flags().StringVar(&opts.DomainName, "domainname", "", "Container NIS domain name") cmd.Flags().StringArrayVarP(&opts.Labels, "label", "l", []string{}, "Set meta data on a container") cmd.Flags().StringArrayVarP(&opts.Volumes, "volume", "v", []string{}, "Volume. Ex: storageaccount/my_share[:/absolute/path/to/target][:ro]") cmd.Flags().BoolVarP(&opts.Detach, "detach", "d", false, "Run container in background and print container ID") @@ -55,6 +54,10 @@ func Command() *cobra.Command { cmd.Flags().StringArrayVarP(&opts.Environment, "env", "e", []string{}, "Set environment variables") cmd.Flags().StringVarP(&opts.RestartPolicyCondition, "restart", "", containers.RestartPolicyNone, "Restart policy to apply when a container exits") + if contextType == store.AciContextType { + cmd.Flags().StringVar(&opts.DomainName, "domainname", "", "Container NIS domain name") + } + return cmd } diff --git a/cli/cmd/run/run_test.go b/cli/cmd/run/run_test.go index b781179e0..78b23c540 100644 --- a/cli/cmd/run/run_test.go +++ b/cli/cmd/run/run_test.go @@ -18,15 +18,25 @@ package run import ( "bytes" + "strings" "testing" + "gotest.tools/v3/assert" "gotest.tools/v3/golden" ) func TestHelp(t *testing.T) { var b bytes.Buffer - c := Command() + c := Command("aci") c.SetOutput(&b) _ = c.Help() golden.Assert(t, b.String(), "run-help.golden") } + +func TestHelpNoDomainFlag(t *testing.T) { + var b bytes.Buffer + c := Command("default") + c.SetOutput(&b) + _ = c.Help() + assert.Assert(t, !strings.Contains(b.String(), "domainname")) +} diff --git a/cli/main.go b/cli/main.go index 56abe671f..ead230b1a 100644 --- a/cli/main.go +++ b/cli/main.go @@ -115,7 +115,6 @@ func main() { contextcmd.Command(), cmd.PsCommand(), cmd.ServeCommand(), - run.Command(), cmd.ExecCommand(), cmd.LogsCommand(), cmd.RmCommand(), @@ -180,6 +179,8 @@ func main() { ctype = cc.Type() } + root.AddCommand(run.Command(ctype)) + if ctype == store.AciContextType { // we can also pass ctype as a parameter to the volume command and customize subcommands, flags, etc. when we have other backend implementations root.AddCommand(volume.ACICommand()) diff --git a/cli/main_test.go b/cli/main_test.go index f634b4672..c151ce0d0 100644 --- a/cli/main_test.go +++ b/cli/main_test.go @@ -64,7 +64,7 @@ func TestCheckOwnCommand(t *testing.T) { assert.Assert(t, isContextAgnosticCommand(login.Command())) assert.Assert(t, isContextAgnosticCommand(context.Command())) assert.Assert(t, isContextAgnosticCommand(cmd.ServeCommand())) - assert.Assert(t, !isContextAgnosticCommand(run.Command())) + assert.Assert(t, !isContextAgnosticCommand(run.Command("default"))) assert.Assert(t, !isContextAgnosticCommand(cmd.ExecCommand())) assert.Assert(t, !isContextAgnosticCommand(cmd.LogsCommand())) assert.Assert(t, !isContextAgnosticCommand(cmd.PsCommand()))