Only add `--domainname` flag to `docker run` in ACI context

Signed-off-by: Guillaume Tardif <guillaume.tardif@docker.com>
This commit is contained in:
Guillaume Tardif 2020-09-21 15:39:05 +02:00
parent 701d1b834e
commit 268c02523a
4 changed files with 21 additions and 7 deletions

View File

@ -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
}

View File

@ -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"))
}

View File

@ -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())

View File

@ -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()))