mirror of
https://github.com/docker/compose.git
synced 2025-07-22 21:24:38 +02:00
Merge pull request #699 from gtardif/fix_compose_default_context
Do not fail when constructing error message for commands not implemented, if command flags are not recognized.
This commit is contained in:
commit
4f48318332
@ -224,13 +224,9 @@ func exit(root *cobra.Command, ctx string, err error, ctype string) {
|
|||||||
os.Exit(errdefs.ExitCodeLoginRequired)
|
os.Exit(errdefs.ExitCodeLoginRequired)
|
||||||
}
|
}
|
||||||
if errors.Is(err, errdefs.ErrNotImplemented) {
|
if errors.Is(err, errdefs.ErrNotImplemented) {
|
||||||
cmd, _, _ := root.Traverse(os.Args[1:])
|
name := metrics.GetCommand(os.Args[1:], root.PersistentFlags())
|
||||||
name := cmd.Name()
|
|
||||||
parent := cmd.Parent()
|
|
||||||
if parent != nil && parent.Parent() != nil {
|
|
||||||
name = parent.Name() + " " + name
|
|
||||||
}
|
|
||||||
fmt.Fprintf(os.Stderr, "Command %q not available in current context (%s)\n", name, ctx)
|
fmt.Fprintf(os.Stderr, "Command %q not available in current context (%s)\n", name, ctx)
|
||||||
|
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,7 +78,7 @@ const (
|
|||||||
|
|
||||||
// Track sends the tracking analytics to Docker Desktop
|
// Track sends the tracking analytics to Docker Desktop
|
||||||
func Track(context string, args []string, flags *flag.FlagSet, status string) {
|
func Track(context string, args []string, flags *flag.FlagSet, status string) {
|
||||||
command := getCommand(args, flags)
|
command := GetCommand(args, flags)
|
||||||
if command != "" {
|
if command != "" {
|
||||||
c := NewClient()
|
c := NewClient()
|
||||||
c.Send(Command{
|
c.Send(Command{
|
||||||
@ -90,7 +90,8 @@ func Track(context string, args []string, flags *flag.FlagSet, status string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getCommand(args []string, flags *flag.FlagSet) string {
|
// GetCommand get the invoked command
|
||||||
|
func GetCommand(args []string, flags *flag.FlagSet) string {
|
||||||
command := ""
|
command := ""
|
||||||
strippedArgs := stripFlags(args, flags)
|
strippedArgs := stripFlags(args, flags)
|
||||||
|
|
||||||
|
@ -25,7 +25,7 @@ import (
|
|||||||
|
|
||||||
func TestFlag(t *testing.T) {
|
func TestFlag(t *testing.T) {
|
||||||
root := &cobra.Command{}
|
root := &cobra.Command{}
|
||||||
root.PersistentFlags().BoolP("debug", "d", false, "debug")
|
root.PersistentFlags().BoolP("debug", "D", false, "debug")
|
||||||
root.PersistentFlags().String("str", "str", "str")
|
root.PersistentFlags().String("str", "str", "str")
|
||||||
|
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
@ -40,7 +40,7 @@ func TestFlag(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with short flags",
|
name: "with short flags",
|
||||||
args: []string{"-d", "run"},
|
args: []string{"-D", "run"},
|
||||||
expected: "run",
|
expected: "run",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -133,11 +133,26 @@ func TestFlag(t *testing.T) {
|
|||||||
args: []string{"create", "--rm", "test"},
|
args: []string{"create", "--rm", "test"},
|
||||||
expected: "create",
|
expected: "create",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "compose up -f xxx",
|
||||||
|
args: []string{"compose", "up", "-f", "titi.yaml"},
|
||||||
|
expected: "compose up",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "compose -f xxx up",
|
||||||
|
args: []string{"compose", "-f", "titi.yaml", "up"},
|
||||||
|
expected: "compose up",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "-D compose -f xxx up",
|
||||||
|
args: []string{"--debug", "compose", "-f", "titi.yaml", "up"},
|
||||||
|
expected: "compose up",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, testCase := range testCases {
|
for _, testCase := range testCases {
|
||||||
t.Run(testCase.name, func(t *testing.T) {
|
t.Run(testCase.name, func(t *testing.T) {
|
||||||
result := getCommand(testCase.args, root.PersistentFlags())
|
result := GetCommand(testCase.args, root.PersistentFlags())
|
||||||
assert.Equal(t, testCase.expected, result)
|
assert.Equal(t, testCase.expected, result)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -210,7 +225,7 @@ func TestEcs(t *testing.T) {
|
|||||||
|
|
||||||
for _, testCase := range testCases {
|
for _, testCase := range testCases {
|
||||||
t.Run(testCase.name, func(t *testing.T) {
|
t.Run(testCase.name, func(t *testing.T) {
|
||||||
result := getCommand(testCase.args, root.PersistentFlags())
|
result := GetCommand(testCase.args, root.PersistentFlags())
|
||||||
assert.Equal(t, testCase.expected, result)
|
assert.Equal(t, testCase.expected, result)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -258,7 +273,7 @@ func TestScan(t *testing.T) {
|
|||||||
|
|
||||||
for _, testCase := range testCases {
|
for _, testCase := range testCases {
|
||||||
t.Run(testCase.name, func(t *testing.T) {
|
t.Run(testCase.name, func(t *testing.T) {
|
||||||
result := getCommand(testCase.args, root.PersistentFlags())
|
result := GetCommand(testCase.args, root.PersistentFlags())
|
||||||
assert.Equal(t, testCase.expected, result)
|
assert.Equal(t, testCase.expected, result)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -56,6 +56,12 @@ func TestComposeNotImplemented(t *testing.T) {
|
|||||||
ExitCode: 1,
|
ExitCode: 1,
|
||||||
Err: `Command "compose up" not available in current context (default)`,
|
Err: `Command "compose up" not available in current context (default)`,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
res = c.RunDockerOrExitError("compose", "-f", "titi.yaml", "up")
|
||||||
|
res.Assert(t, icmd.Expected{
|
||||||
|
ExitCode: 1,
|
||||||
|
Err: `Command "compose up" not available in current context (default)`,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContextDefault(t *testing.T) {
|
func TestContextDefault(t *testing.T) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user