Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit db38d12

Browse files
committed
Retrieve compose failure category by exit code
Signed-off-by: Nicolas De Loof <[email protected]>
1 parent d8aa00a commit db38d12

File tree

5 files changed

+29
-10
lines changed

5 files changed

+29
-10
lines changed

cli/cmd/compose/remove.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func runRemove(ctx context.Context, backend compose.Service, opts removeOptions,
7878
}
7979

8080
reosurces, err := backend.Remove(ctx, project, compose.RemoveOptions{
81-
DryRun: true,
81+
DryRun: true,
8282
Services: services,
8383
})
8484
if err != nil {

cli/cmd/compose/stop.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func runStop(ctx context.Context, backend compose.Service, opts stopOptions, ser
6565
}
6666
_, err = progress.Run(ctx, func(ctx context.Context) (string, error) {
6767
return "", backend.Stop(ctx, project, compose.StopOptions{
68-
Timeout: timeout,
68+
Timeout: timeout,
6969
Services: services,
7070
})
7171
})

cli/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ func exit(ctx string, err error, ctype string) {
294294

295295
if errors.Is(err, errdefs.ErrNotImplemented) {
296296
name := metrics.GetCommand(os.Args[1:])
297-
fmt.Fprintf(os.Stderr, "RootCommand %q not available in current context (%s)\n", name, ctx)
297+
fmt.Fprintf(os.Stderr, "Command %q not available in current context (%s)\n", name, ctx)
298298

299299
os.Exit(1)
300300
}
@@ -314,7 +314,7 @@ func checkIfUnknownCommandExistInDefaultContext(err error, currentContext string
314314
dockerCommand := string(submatch[1])
315315

316316
if mobycli.IsDefaultContextCommand(dockerCommand) {
317-
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)
317+
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)
318318
metrics.Track(contextType, os.Args[1:], metrics.FailureStatus)
319319
os.Exit(1)
320320
}

cli/metrics/definitions.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,26 @@ var (
5555
// PullFailure failure while pulling image
5656
PullFailure = FailureCategory{MetricsStatus: PullFailureStatus, ExitCode: 18}
5757
)
58+
59+
//ByExitCode retrieve FailureCategory based on command exit code
60+
func ByExitCode(exitCode int) FailureCategory {
61+
switch exitCode {
62+
case 0:
63+
return FailureCategory{MetricsStatus: SuccessStatus, ExitCode: 0}
64+
case 14:
65+
return FileNotFoundFailure
66+
case 15:
67+
return ComposeParseFailure
68+
case 16:
69+
return CommandSyntaxFailure
70+
case 17:
71+
return BuildFailure
72+
case 18:
73+
return PullFailure
74+
case 130:
75+
return FailureCategory{MetricsStatus: CanceledStatus, ExitCode: exitCode}
76+
default:
77+
return FailureCategory{MetricsStatus: FailureStatus, ExitCode: exitCode}
78+
}
79+
80+
}

cli/mobycli/exec.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,8 @@ func Exec(root *cobra.Command) {
6868
if err != nil {
6969
if exiterr, ok := err.(*exec.ExitError); ok {
7070
exitCode := exiterr.ExitCode()
71-
if exitCode == 130 {
72-
metrics.Track(store.DefaultContextType, os.Args[1:], metrics.CanceledStatus)
73-
} else {
74-
metrics.Track(store.DefaultContextType, os.Args[1:], metrics.FailureStatus)
75-
}
76-
os.Exit(exiterr.ExitCode())
71+
metrics.Track(store.DefaultContextType, os.Args[1:], metrics.ByExitCode(exitCode).MetricsStatus)
72+
os.Exit(exitCode)
7773
}
7874
metrics.Track(store.DefaultContextType, os.Args[1:], metrics.FailureStatus)
7975
fmt.Fprintln(os.Stderr, err)

0 commit comments

Comments
 (0)