Skip to content

Commit 83f3e09

Browse files
committed
cli.WrapRun(): change to be given to RunE field
this field actually propagates the error through Cobra to the `Command.Execute()`
1 parent c43746e commit 83f3e09

2 files changed

Lines changed: 11 additions & 15 deletions

File tree

app/cli/cobrawrappers.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,18 @@ func Execute(app *cobra.Command) {
3636
// fixes problems of cobra commands' bare run callbacks with regards to application quality:
3737
// 1. logging not configured
3838
// 2. no interrupt handling
39-
// 3. no error handling
40-
func WrapRun(run func(ctx context.Context, args []string) error) func(*cobra.Command, []string) {
41-
return func(_ *cobra.Command, args []string) {
39+
//
40+
// this is intended to be given to `RunE` field in `*cobra.Command`
41+
func WrapRun(run func(ctx context.Context, args []string) error) func(*cobra.Command, []string) error {
42+
return func(_ *cobra.Command, args []string) error {
4243
// handle logging
4344
configureLogging()
4445

4546
// handle interrupts
4647
ctx := notifyContextInterruptOrTerminate()
4748

4849
// run the actual code (jump from CLI context to higher-level application context)
49-
// this can be kinda read as:
50-
// output = logic(input)
51-
err := run(ctx, args)
52-
53-
// handle errors
54-
osutil.ExitIfError(err)
50+
return run(ctx, args)
5551
}
5652
}
5753

os/systemdcli/cli.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func Entrypoint(serviceName string, makeAdditionalCommands func(string) []*cobra
3939
Use: "start",
4040
Short: "Start the service",
4141
Args: cobra.NoArgs,
42-
Run: cli.WrapRun(func(ctx context.Context, _ []string) error {
42+
RunE: cli.WrapRun(func(ctx context.Context, _ []string) error {
4343
return runSystemctlVerb(ctx, "start")
4444
}),
4545
})
@@ -48,7 +48,7 @@ func Entrypoint(serviceName string, makeAdditionalCommands func(string) []*cobra
4848
Use: "stop",
4949
Short: "Stop the service",
5050
Args: cobra.NoArgs,
51-
Run: cli.WrapRun(func(ctx context.Context, _ []string) error {
51+
RunE: cli.WrapRun(func(ctx context.Context, _ []string) error {
5252
return runSystemctlVerb(ctx, "stop")
5353
}),
5454
})
@@ -57,7 +57,7 @@ func Entrypoint(serviceName string, makeAdditionalCommands func(string) []*cobra
5757
Use: "restart",
5858
Short: "Restart the service",
5959
Args: cobra.NoArgs,
60-
Run: cli.WrapRun(func(ctx context.Context, _ []string) error {
60+
RunE: cli.WrapRun(func(ctx context.Context, _ []string) error {
6161
return runSystemctlVerb(ctx, "restart")
6262
}),
6363
})
@@ -66,7 +66,7 @@ func Entrypoint(serviceName string, makeAdditionalCommands func(string) []*cobra
6666
Use: "status",
6767
Short: "Show status of the service",
6868
Args: cobra.NoArgs,
69-
Run: cli.WrapRun(func(ctx context.Context, _ []string) error {
69+
RunE: cli.WrapRun(func(ctx context.Context, _ []string) error {
7070
translateNonError := func(err error) error {
7171
if err != nil {
7272
// LSB dictates that successful status show of non-running program must return 3:
@@ -89,7 +89,7 @@ func Entrypoint(serviceName string, makeAdditionalCommands func(string) []*cobra
8989
Use: "logs",
9090
Short: "Get logs for the service",
9191
Args: cobra.NoArgs,
92-
Run: cli.WrapRun(func(ctx context.Context, _ []string) error {
92+
RunE: cli.WrapRun(func(ctx context.Context, _ []string) error {
9393
//nolint:gosec // ok, is expected to not be user input.
9494
logsCmd := exec.CommandContext(ctx, "journalctl", "--unit="+serviceName)
9595
logsCmd.Stdout = os.Stdout
@@ -108,7 +108,7 @@ func WithInstallAndUninstallCommands(makeSvc func(string) (*systemdinstaller.Ser
108108
Use: "install",
109109
Short: "Installs the background service",
110110
Args: cobra.NoArgs,
111-
Run: cli.WrapRun(func(_ context.Context, _ []string) error {
111+
RunE: cli.WrapRun(func(_ context.Context, _ []string) error {
112112
svc, err := makeSvc(serviceName)
113113
if err != nil {
114114
return err

0 commit comments

Comments
 (0)