瀏覽代碼

Retrieve compose failure category by exit code

Signed-off-by: Nicolas De Loof <[email protected]>
Nicolas De Loof 4 年之前
父節點
當前提交
db38d1244c
共有 5 個文件被更改,包括 29 次插入10 次删除
  1. 1 1
      cli/cmd/compose/remove.go
  2. 1 1
      cli/cmd/compose/stop.go
  3. 2 2
      cli/main.go
  4. 23 0
      cli/metrics/definitions.go
  5. 2 6
      cli/mobycli/exec.go

+ 1 - 1
cli/cmd/compose/remove.go

@@ -78,7 +78,7 @@ func runRemove(ctx context.Context, backend compose.Service, opts removeOptions,
 	}
 
 	reosurces, err := backend.Remove(ctx, project, compose.RemoveOptions{
-		DryRun: true,
+		DryRun:   true,
 		Services: services,
 	})
 	if err != nil {

+ 1 - 1
cli/cmd/compose/stop.go

@@ -65,7 +65,7 @@ func runStop(ctx context.Context, backend compose.Service, opts stopOptions, ser
 	}
 	_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
 		return "", backend.Stop(ctx, project, compose.StopOptions{
-			Timeout: timeout,
+			Timeout:  timeout,
 			Services: services,
 		})
 	})

+ 2 - 2
cli/main.go

@@ -294,7 +294,7 @@ func exit(ctx string, err error, ctype string) {
 
 	if errors.Is(err, errdefs.ErrNotImplemented) {
 		name := metrics.GetCommand(os.Args[1:])
-		fmt.Fprintf(os.Stderr, "RootCommand %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)
 	}
@@ -314,7 +314,7 @@ func checkIfUnknownCommandExistInDefaultContext(err error, currentContext string
 		dockerCommand := string(submatch[1])
 
 		if mobycli.IsDefaultContextCommand(dockerCommand) {
-			fmt.Fprintf(os.Stderr, "RootCommand %q not available in current context (%s), you can use the \"default\" context to run this command\n", dockerCommand, currentContext)
+			fmt.Fprintf(os.Stderr, "Command %q not available in current context (%s), you can use the \"default\" context to run this command\n", dockerCommand, currentContext)
 			metrics.Track(contextType, os.Args[1:], metrics.FailureStatus)
 			os.Exit(1)
 		}

+ 23 - 0
cli/metrics/definitions.go

@@ -55,3 +55,26 @@ var (
 	// PullFailure failure while pulling image
 	PullFailure = FailureCategory{MetricsStatus: PullFailureStatus, ExitCode: 18}
 )
+
+//ByExitCode retrieve FailureCategory based on command exit code
+func ByExitCode(exitCode int) FailureCategory {
+	switch exitCode {
+	case 0:
+		return FailureCategory{MetricsStatus: SuccessStatus, ExitCode: 0}
+	case 14:
+		return FileNotFoundFailure
+	case 15:
+		return ComposeParseFailure
+	case 16:
+		return CommandSyntaxFailure
+	case 17:
+		return BuildFailure
+	case 18:
+		return PullFailure
+	case 130:
+		return FailureCategory{MetricsStatus: CanceledStatus, ExitCode: exitCode}
+	default:
+		return FailureCategory{MetricsStatus: FailureStatus, ExitCode: exitCode}
+	}
+
+}

+ 2 - 6
cli/mobycli/exec.go

@@ -68,12 +68,8 @@ func Exec(root *cobra.Command) {
 	if err != nil {
 		if exiterr, ok := err.(*exec.ExitError); ok {
 			exitCode := exiterr.ExitCode()
-			if exitCode == 130 {
-				metrics.Track(store.DefaultContextType, os.Args[1:], metrics.CanceledStatus)
-			} else {
-				metrics.Track(store.DefaultContextType, os.Args[1:], metrics.FailureStatus)
-			}
-			os.Exit(exiterr.ExitCode())
+			metrics.Track(store.DefaultContextType, os.Args[1:], metrics.ByExitCode(exitCode).MetricsStatus)
+			os.Exit(exitCode)
 		}
 		metrics.Track(store.DefaultContextType, os.Args[1:], metrics.FailureStatus)
 		fmt.Fprintln(os.Stderr, err)