-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathrun.go
More file actions
78 lines (65 loc) · 1.72 KB
/
run.go
File metadata and controls
78 lines (65 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package ui
import (
"context"
"errors"
"os"
tea "github.com/charmbracelet/bubbletea"
"github.com/localstack/lstk/internal/container"
"github.com/localstack/lstk/internal/endpoint"
"github.com/localstack/lstk/internal/output"
"github.com/localstack/lstk/internal/runtime"
"golang.org/x/term"
)
type programSender struct {
p *tea.Program
}
func (s programSender) Send(msg any) {
if s.p == nil {
return
}
s.p.Send(msg)
}
func Run(parentCtx context.Context, rt runtime.Runtime, version string, opts container.StartOptions) error {
ctx, cancel := context.WithCancel(parentCtx)
defer cancel()
// FIXME: This assumes a single emulator; revisit for proper multi-emulator support
emulatorName := "LocalStack Emulator"
host := endpoint.Hostname
if len(opts.Containers) > 0 {
emulatorName = opts.Containers[0].DisplayName()
if opts.Containers[0].Port != "" {
host, _ = endpoint.ResolveHost(opts.Containers[0].Port, opts.LocalStackHost)
}
}
app := NewApp(version, emulatorName, host, cancel)
p := tea.NewProgram(app)
runErrCh := make(chan error, 1)
go func() {
var err error
defer func() { runErrCh <- err }()
err = container.Start(ctx, rt, output.NewTUISink(programSender{p: p}), opts, true)
if err != nil {
if errors.Is(err, context.Canceled) {
return
}
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 output.NewSilentError(app.Err())
}
runErr := <-runErrCh
if runErr != nil && !errors.Is(runErr, context.Canceled) {
return runErr
}
return nil
}
func IsInteractive() bool {
return term.IsTerminal(int(os.Stdout.Fd())) && term.IsTerminal(int(os.Stdin.Fd()))
}