Skip to content

Commit fe08870

Browse files
committed
Move error rendering out of cmd
1 parent 308a62b commit fe08870

File tree

9 files changed

+256
-83
lines changed

9 files changed

+256
-83
lines changed

cmd/error_output.go

Lines changed: 0 additions & 66 deletions
This file was deleted.

cmd/root.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ var rootCmd = &cobra.Command{
2626
Run: func(cmd *cobra.Command, args []string) {
2727
rt, err := newRuntime(cmd.Context())
2828
if err != nil {
29-
exitWithStartError(err)
29+
ui.ExitWithStartError(err)
3030
}
3131

3232
if err := runStart(cmd.Context(), rt); err != nil {
33-
exitWithStartError(err)
33+
ui.ExitWithStartError(err)
3434
}
3535
},
3636
}

cmd/start.go

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

33
import (
4+
"github.com/localstack/lstk/internal/ui"
45
"github.com/spf13/cobra"
56
)
67

@@ -12,11 +13,11 @@ var startCmd = &cobra.Command{
1213
Run: func(cmd *cobra.Command, args []string) {
1314
rt, err := newRuntime(cmd.Context())
1415
if err != nil {
15-
exitWithStartError(err)
16+
ui.ExitWithStartError(err)
1617
}
1718

1819
if err := runStart(cmd.Context(), rt); err != nil {
19-
exitWithStartError(err)
20+
ui.ExitWithStartError(err)
2021
}
2122
},
2223
}

internal/runtime/engine_check_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,20 @@ package runtime
33
import (
44
"context"
55
"errors"
6-
"io"
76
"strings"
87
"testing"
8+
9+
"go.uber.org/mock/gomock"
910
)
1011

11-
type fakeRuntime struct {
12+
type runtimeWithChecker struct {
13+
Runtime
1214
err error
1315
}
1416

15-
func (f *fakeRuntime) PullImage(context.Context, string, chan<- PullProgress) error { return nil }
16-
func (f *fakeRuntime) Start(context.Context, ContainerConfig) (string, error) { return "", nil }
17-
func (f *fakeRuntime) Stop(context.Context, string) error { return nil }
18-
func (f *fakeRuntime) Remove(context.Context, string) error { return nil }
19-
func (f *fakeRuntime) IsRunning(context.Context, string) (bool, error) { return false, nil }
20-
func (f *fakeRuntime) Logs(context.Context, string, int) (string, error) { return "", nil }
21-
func (f *fakeRuntime) StreamLogs(context.Context, string, io.Writer) error { return nil }
22-
func (f *fakeRuntime) GetImageVersion(context.Context, string) (string, error) { return "", nil }
23-
func (f *fakeRuntime) CheckConnection(context.Context) error { return f.err }
17+
func (r *runtimeWithChecker) CheckConnection(context.Context) error {
18+
return r.err
19+
}
2420

2521
func TestFormatDockerConnectionErrorFriendly(t *testing.T) {
2622
endpoint := "unix:///Users/geo/.docker/run/docker.sock"
@@ -73,8 +69,12 @@ func TestFormatDockerConnectionErrorPermission(t *testing.T) {
7369
}
7470

7571
func TestCheckContainerEngineUsesChecker(t *testing.T) {
72+
ctrl := gomock.NewController(t)
7673
expected := errors.New("boom")
77-
rt := &fakeRuntime{err: expected}
74+
rt := &runtimeWithChecker{
75+
Runtime: NewMockRuntime(ctrl),
76+
err: expected,
77+
}
7878

7979
err := CheckContainerEngine(context.Background(), rt)
8080
if !errors.Is(err, expected) {

internal/runtime/mock_runtime_test.go

Lines changed: 158 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/runtime/runtime.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"io"
66
)
77

8+
//go:generate mockgen -source=runtime.go -destination=mock_runtime_test.go -package=runtime
9+
810
type ContainerConfig struct {
911
Image string
1012
Name string

internal/ui/error_output.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package ui
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"os"
7+
"strings"
8+
9+
"github.com/charmbracelet/lipgloss"
10+
"github.com/localstack/lstk/internal/runtime"
11+
"github.com/localstack/lstk/internal/ui/styles"
12+
)
13+
14+
func ExitWithStartError(err error) {
15+
if runtimeErr, ok := runtime.AsRuntimeUnavailableError(err); ok {
16+
fmt.Fprintln(os.Stderr, styles.ErrorTitle.Render("✗ Could not start LocalStack"))
17+
fmt.Fprintln(os.Stderr, styles.ErrorSummaryPrefix.Render("> ")+styles.ErrorBody.Render("No container runtime detected."))
18+
printIndentedStyled(os.Stderr, summarizeRuntimeDetail(runtimeErr.Detail), " ", styles.ErrorNote)
19+
fmt.Fprintln(os.Stderr)
20+
printActionLine(os.Stderr, "Install Docker:", "https://docs.docker.com/get-started", styles.ErrorBody)
21+
printActionLine(os.Stderr, "Using Podman? Run", "`lstk configure podman`", styles.ErrorActionText)
22+
printActionLine(os.Stderr, "Deploying in Kubernetes?", "https://www.localstack.cloud/demo", styles.ErrorK8s)
23+
os.Exit(1)
24+
}
25+
26+
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
27+
os.Exit(1)
28+
}
29+
30+
func printIndentedStyled(out io.Writer, text, indent string, style lipgloss.Style) {
31+
lines := strings.Split(text, "\n")
32+
for i, line := range lines {
33+
lineIndent := indent
34+
if i > 0 {
35+
lineIndent += " "
36+
}
37+
fmt.Fprintln(out, lineIndent+style.Render(line))
38+
}
39+
}
40+
41+
func printActionLine(out io.Writer, label, value string, labelStyle lipgloss.Style) {
42+
fmt.Fprintln(
43+
out,
44+
styles.ErrorActionArrow.Render("⇒")+" "+
45+
labelStyle.Render(label)+" "+
46+
styles.ErrorLink.Render(value),
47+
)
48+
}
49+
50+
func summarizeRuntimeDetail(detail string) string {
51+
msg := strings.ToLower(detail)
52+
if strings.Contains(msg, "cannot connect to the docker daemon") &&
53+
strings.Contains(msg, "is the docker daemon running") {
54+
return "Cannot connect to Docker daemon."
55+
}
56+
return detail
57+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package cmd
1+
package ui
22

33
import "testing"
44

0 commit comments

Comments
 (0)