Skip to content

Commit 8aae81e

Browse files
no more os.Exit(1)
1 parent 4187957 commit 8aae81e

File tree

4 files changed

+33
-47
lines changed

4 files changed

+33
-47
lines changed

cmd/help_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ import (
77
"testing"
88

99
"github.com/localstack/lstk/internal/env"
10+
"github.com/localstack/lstk/internal/telemetry"
1011
)
1112

1213
func executeWithArgs(t *testing.T, args ...string) (string, error) {
1314
t.Helper()
1415
buf := new(bytes.Buffer)
15-
cmd := NewRootCmd(&env.Env{})
16+
cmd := NewRootCmd(&env.Env{}, telemetry.New("", true))
1617
cmd.SetOut(buf)
1718
cmd.SetErr(buf)
1819
cmd.SetArgs(args)

cmd/root.go

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,18 @@ import (
1717
"github.com/spf13/cobra"
1818
)
1919

20-
func NewRootCmd(cfg *env.Env) *cobra.Command {
20+
func NewRootCmd(cfg *env.Env, tel *telemetry.Client) *cobra.Command {
2121
root := &cobra.Command{
22-
Use: "lstk",
23-
Short: "LocalStack CLI",
24-
Long: "lstk is the command-line interface for LocalStack.",
22+
Use: "lstk",
23+
Short: "LocalStack CLI",
24+
Long: "lstk is the command-line interface for LocalStack.",
2525
PreRunE: initConfig,
26-
Run: func(cmd *cobra.Command, args []string) {
26+
RunE: func(cmd *cobra.Command, args []string) error {
2727
rt, err := runtime.NewDockerRuntime()
2828
if err != nil {
29-
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
30-
os.Exit(1)
31-
}
32-
33-
if err := runStart(cmd.Context(), rt, cfg); err != nil {
34-
if !output.IsSilent(err) {
35-
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
36-
}
37-
os.Exit(1)
29+
return err
3830
}
31+
return runStart(cmd.Context(), rt, cfg, tel)
3932
},
4033
}
4134

@@ -48,7 +41,7 @@ func NewRootCmd(cfg *env.Env) *cobra.Command {
4841
root.Flags().Lookup("version").Usage = "Show version"
4942

5043
root.AddCommand(
51-
newStartCmd(cfg),
44+
newStartCmd(cfg, tel),
5245
newStopCmd(),
5346
newLoginCmd(cfg),
5447
newLogoutCmd(cfg),
@@ -62,12 +55,22 @@ func NewRootCmd(cfg *env.Env) *cobra.Command {
6255

6356
func Execute(ctx context.Context) error {
6457
cfg := env.Init()
65-
return NewRootCmd(cfg).ExecuteContext(ctx)
66-
}
67-
68-
func runStart(ctx context.Context, rt runtime.Runtime, cfg *env.Env) error {
6958
tel := telemetry.New(cfg.AnalyticsEndpoint, cfg.DisableEvents)
7059
defer tel.Flush()
60+
61+
root := NewRootCmd(cfg, tel)
62+
root.SilenceErrors = true
63+
64+
if err := root.ExecuteContext(ctx); err != nil {
65+
if !output.IsSilent(err) {
66+
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
67+
}
68+
return err
69+
}
70+
return nil
71+
}
72+
73+
func runStart(ctx context.Context, rt runtime.Runtime, cfg *env.Env, tel *telemetry.Client) error {
7174
// TODO: replace map with a typed payload struct once event schema is finalised
7275
tel.Track("cli_cmd", map[string]any{"cmd": "lstk start", "params": []string{}})
7376

cmd/start.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,24 @@
11
package cmd
22

33
import (
4-
"fmt"
5-
"os"
6-
74
"github.com/localstack/lstk/internal/env"
8-
"github.com/localstack/lstk/internal/output"
95
"github.com/localstack/lstk/internal/runtime"
6+
"github.com/localstack/lstk/internal/telemetry"
107
"github.com/spf13/cobra"
118
)
129

13-
func newStartCmd(cfg *env.Env) *cobra.Command {
10+
func newStartCmd(cfg *env.Env, tel *telemetry.Client) *cobra.Command {
1411
return &cobra.Command{
1512
Use: "start",
1613
Short: "Start emulator",
1714
Long: "Start emulator and services.",
1815
PreRunE: initConfig,
19-
Run: func(cmd *cobra.Command, args []string) {
16+
RunE: func(cmd *cobra.Command, args []string) error {
2017
rt, err := runtime.NewDockerRuntime()
2118
if err != nil {
22-
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
23-
os.Exit(1)
24-
}
25-
26-
if err := runStart(cmd.Context(), rt, cfg); err != nil {
27-
if !output.IsSilent(err) {
28-
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
29-
}
30-
os.Exit(1)
19+
return err
3120
}
21+
return runStart(cmd.Context(), rt, cfg, tel)
3222
},
3323
}
3424
}

cmd/stop.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"fmt"
5-
"os"
65

76
"github.com/localstack/lstk/internal/container"
87
"github.com/localstack/lstk/internal/runtime"
@@ -15,21 +14,14 @@ func newStopCmd() *cobra.Command {
1514
Short: "Stop emulator",
1615
Long: "Stop emulator and services",
1716
PreRunE: initConfig,
18-
Run: func(cmd *cobra.Command, args []string) {
17+
RunE: func(cmd *cobra.Command, args []string) error {
1918
rt, err := runtime.NewDockerRuntime()
2019
if err != nil {
21-
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
22-
os.Exit(1)
20+
return err
2321
}
24-
25-
onProgress := func(msg string) {
22+
return container.Stop(cmd.Context(), rt, func(msg string) {
2623
fmt.Println(msg)
27-
}
28-
29-
if err := container.Stop(cmd.Context(), rt, onProgress); err != nil {
30-
fmt.Fprintln(os.Stderr, err)
31-
os.Exit(1)
32-
}
24+
})
3325
},
3426
}
3527
}

0 commit comments

Comments
 (0)