Explorar o código

Do not fail when constructing error message for commands not implemented. Use metrics GetCommand() the is already used 3 lines above for metrics tracking.

Signed-off-by: Guillaume Tardif <[email protected]>
Guillaume Tardif %!s(int64=5) %!d(string=hai) anos
pai
achega
b8968e83cd
Modificáronse 4 ficheiros con 24 adicións e 11 borrados
  1. 2 6
      cli/main.go
  2. 3 2
      metrics/metrics.go
  3. 13 3
      metrics/metrics_test.go
  4. 6 0
      tests/e2e/e2e_test.go

+ 2 - 6
cli/main.go

@@ -224,13 +224,9 @@ func exit(root *cobra.Command, ctx string, err error, ctype string) {
 		os.Exit(errdefs.ExitCodeLoginRequired)
 	}
 	if errors.Is(err, errdefs.ErrNotImplemented) {
-		cmd, _, _ := root.Traverse(os.Args[1:])
-		name := cmd.Name()
-		parent := cmd.Parent()
-		if parent != nil && parent.Parent() != nil {
-			name = parent.Name() + " " + name
-		}
+		name := metrics.GetCommand(os.Args[1:], root.PersistentFlags())
 		fmt.Fprintf(os.Stderr, "Command %q not available in current context (%s)\n", name, ctx)
+
 		os.Exit(1)
 	}
 

+ 3 - 2
metrics/metrics.go

@@ -78,7 +78,7 @@ const (
 
 // Track sends the tracking analytics to Docker Desktop
 func Track(context string, args []string, flags *flag.FlagSet, status string) {
-	command := getCommand(args, flags)
+	command := GetCommand(args, flags)
 	if command != "" {
 		c := NewClient()
 		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 := ""
 	strippedArgs := stripFlags(args, flags)
 

+ 13 - 3
metrics/metrics_test.go

@@ -133,11 +133,21 @@ func TestFlag(t *testing.T) {
 			args:     []string{"create", "--rm", "test"},
 			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",
+		},
 	}
 
 	for _, testCase := range testCases {
 		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)
 		})
 	}
@@ -210,7 +220,7 @@ func TestEcs(t *testing.T) {
 
 	for _, testCase := range testCases {
 		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)
 		})
 	}
@@ -258,7 +268,7 @@ func TestScan(t *testing.T) {
 
 	for _, testCase := range testCases {
 		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)
 		})
 	}

+ 6 - 0
tests/e2e/e2e_test.go

@@ -56,6 +56,12 @@ func TestComposeNotImplemented(t *testing.T) {
 		ExitCode: 1,
 		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) {