Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions cmd/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"os"

"github.com/localstack/lstk/internal/container"
"github.com/localstack/lstk/internal/output"
"github.com/localstack/lstk/internal/runtime"
"github.com/localstack/lstk/internal/ui"
"github.com/spf13/cobra"
)

Expand All @@ -22,14 +24,19 @@ func newStopCmd() *cobra.Command {
os.Exit(1)
}

onProgress := func(msg string) {
fmt.Println(msg)
if ui.IsInteractive() {
if err := ui.RunStop(cmd.Context(), rt); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
return
}

if err := container.Stop(cmd.Context(), rt, onProgress); err != nil {
if err := container.Stop(cmd.Context(), rt, output.NewPlainSink(os.Stdout)); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
},
}
}

20 changes: 13 additions & 7 deletions internal/container/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,33 @@ import (
"context"
"fmt"

"github.com/containerd/errdefs"
"github.com/localstack/lstk/internal/config"
"github.com/localstack/lstk/internal/output"
"github.com/localstack/lstk/internal/runtime"
)

func Stop(ctx context.Context, rt runtime.Runtime, onProgress func(string)) error {
func Stop(ctx context.Context, rt runtime.Runtime, sink output.Sink) error {
cfg, err := config.Get()
if err != nil {
return fmt.Errorf("failed to get config: %w", err)
}

for _, c := range cfg.Containers {
name := c.Name()
onProgress(fmt.Sprintf("Stopping %s...", name))
running, err := rt.IsRunning(ctx, name)
if err != nil {
return fmt.Errorf("checking %s running: %w", name, err)
}
if !running {
return fmt.Errorf("%s is not running", name)
}
output.EmitSpinnerStart(sink, fmt.Sprintf("Stopping %s", name))
if err := rt.Stop(ctx, name); err != nil {
if errdefs.IsNotFound(err) {
return fmt.Errorf("%s is not running", name)
}
output.EmitSpinnerStop(sink)
return fmt.Errorf("failed to stop %s: %w", name, err)
}
onProgress(fmt.Sprintf("%s stopped", name))
output.EmitSpinnerStop(sink)
output.EmitSuccess(sink, fmt.Sprintf("%s stopped", name))
}

return nil
Expand Down
3 changes: 3 additions & 0 deletions internal/runtime/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ func (d *DockerRuntime) Remove(ctx context.Context, containerName string) error
func (d *DockerRuntime) IsRunning(ctx context.Context, containerID string) (bool, error) {
inspect, err := d.client.ContainerInspect(ctx, containerID)
if err != nil {
if errdefs.IsNotFound(err) {
return false, nil
}
return false, err
}
return inspect.State.Running, nil
Expand Down
47 changes: 47 additions & 0 deletions internal/ui/run_stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package ui

import (
"context"
"errors"
"os"

tea "github.com/charmbracelet/bubbletea"
"github.com/localstack/lstk/internal/container"
"github.com/localstack/lstk/internal/output"
"github.com/localstack/lstk/internal/runtime"
)

func RunStop(parentCtx context.Context, rt runtime.Runtime) error {
ctx, cancel := context.WithCancel(parentCtx)
defer cancel()

app := NewApp("", "", "", cancel, withoutHeader())
p := tea.NewProgram(app, tea.WithInput(os.Stdin), tea.WithOutput(os.Stdout))
runErrCh := make(chan error, 1)

go func() {
err := container.Stop(ctx, rt, output.NewTUISink(programSender{p: p}))
runErrCh <- err
if err != nil && !errors.Is(err, context.Canceled) {
p.Send(runErrMsg{err: err})
return
}
p.Send(runDoneMsg{})
}()

model, err := p.Run()
if err != nil {
return err
}

if app, ok := model.(App); ok && app.Err() != nil {
return app.Err()
}

runErr := <-runErrCh
if runErr != nil && !errors.Is(runErr, context.Canceled) {
return runErr
}

return nil
}