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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.26.0

require (
github.com/99designs/keyring v1.2.2
github.com/charmbracelet/bubbles v1.0.0
github.com/charmbracelet/bubbletea v1.3.10
github.com/charmbracelet/lipgloss v1.1.0
github.com/charmbracelet/x/exp/teatest v0.0.0-20260216111343-536eb63c1f4c
Expand All @@ -27,7 +28,6 @@ require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/aymanbagabas/go-udiff v0.3.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/charmbracelet/bubbles v1.0.0 // indirect
github.com/charmbracelet/colorprofile v0.4.2 // indirect
github.com/charmbracelet/x/ansi v0.11.6 // indirect
github.com/charmbracelet/x/cellbuf v0.0.15 // indirect
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ github.com/charmbracelet/x/ansi v0.11.6 h1:GhV21SiDz/45W9AnV2R61xZMRri5NlLnl6CVF
github.com/charmbracelet/x/ansi v0.11.6/go.mod h1:2JNYLgQUsyqaiLovhU2Rv/pb8r6ydXKS3NIttu3VGZQ=
github.com/charmbracelet/x/cellbuf v0.0.15 h1:ur3pZy0o6z/R7EylET877CBxaiE1Sp1GMxoFPAIztPI=
github.com/charmbracelet/x/cellbuf v0.0.15/go.mod h1:J1YVbR7MUuEGIFPCaaZ96KDl5NoS0DAWkskup+mOY+Q=
github.com/charmbracelet/x/exp/golden v0.0.0-20240806155701-69247e0abc2a h1:G99klV19u0QnhiizODirwVksQB91TJKV/UaTnACcG30=
github.com/charmbracelet/x/exp/golden v0.0.0-20240806155701-69247e0abc2a/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=
github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91 h1:payRxjMjKgx2PaCWLZ4p3ro9y97+TVLZNaRZgJwSVDQ=
github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U=
github.com/charmbracelet/x/exp/teatest v0.0.0-20260216111343-536eb63c1f4c h1:/pbU92+xMwttewB4XK69/B9ISH0HMhOMrTIVhV4AS7M=
Expand Down
4 changes: 3 additions & 1 deletion internal/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ func (a *Auth) GetToken(ctx context.Context) (string, error) {
return "", fmt.Errorf("authentication required: set LOCALSTACK_AUTH_TOKEN or run in interactive mode")
}

output.EmitInfo(a.sink, "No existing credentials found. Please log in:")
token, err := a.login.Login(ctx)
if err != nil {
if errors.Is(err, context.Canceled) {
return "", err
}
output.EmitWarning(a.sink, "Authentication failed.")
return "", err
}
Expand Down
40 changes: 13 additions & 27 deletions internal/auth/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,46 +35,32 @@ func (l *loginProvider) Login(ctx context.Context) (string, error) {
}

authURL := fmt.Sprintf("%s/auth/request/%s", getWebAppURL(), authReq.ID)
output.EmitInfo(l.sink, fmt.Sprintf("Visit: %s", authURL))
output.EmitInfo(l.sink, fmt.Sprintf("Verification code: %s", authReq.Code))

// Ask whether to open the browser; ENTER or Y accepts (default yes), N skips
browserCh := make(chan output.InputResponse, 1)
output.EmitUserInputRequest(l.sink, output.UserInputRequestEvent{
Prompt: "Open browser now?",
Options: []output.InputOption{{Key: "y", Label: "Y"}, {Key: "n", Label: "n"}},
ResponseCh: browserCh,
output.EmitAuth(l.sink, output.AuthEvent{
Preamble: "Welcome to lstk, a command-line interface for LocalStack",
Code: authReq.Code,
URL: authURL,
})
_ = browser.OpenURL(authURL)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question to @silv-io: what did you have in mind exactly when you suggested an InstructionsEvent? #54 (comment)
Here it has a a code and URL so it really looks more like a specific AuthEvent

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe something in the direction "problem/task":"this", "guidance":"that".

But that might be too generic to create a nice looking UI in this case? So I think that's actually fine?

Maybe my "doctrine" to keep all event definitions in one place was misplaced as well because it turns out we need quite a bit of different ones. I'll think about it if it wouldn't be better solved by having something more "duck-typed" with an interface. I'll think about that, but so far this can just go in like that IMO 👍🏼

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, both! Renamed in d36e7f6.


select {
case resp := <-browserCh:
if resp.Cancelled {
return "", context.Canceled
}
if resp.SelectedKey != "n" {
if err := browser.OpenURL(authURL); err != nil {
output.EmitWarning(l.sink, fmt.Sprintf("Failed to open browser: %v", err))
}
}
case <-ctx.Done():
return "", ctx.Err()
}
output.EmitSpinnerStart(l.sink, "Waiting for authorization...")

// Wait for the user to complete authentication in the browser
enterCh := make(chan output.InputResponse, 1)
responseCh := make(chan output.InputResponse, 1)
output.EmitUserInputRequest(l.sink, output.UserInputRequestEvent{
Prompt: "Waiting for authentication",
Options: []output.InputOption{{Key: "enter", Label: "Press ENTER when complete"}},
ResponseCh: enterCh,
Prompt: "Waiting for authorization...",
Options: []output.InputOption{{Key: "any", Label: "Press any key when complete"}},
ResponseCh: responseCh,
})

select {
case resp := <-enterCh:
case resp := <-responseCh:
output.EmitSpinnerStop(l.sink)
if resp.Cancelled {
return "", context.Canceled
}
return l.completeAuth(ctx, authReq)
case <-ctx.Done():
output.EmitSpinnerStop(l.sink)
return "", ctx.Err()
}
}
Expand Down
12 changes: 11 additions & 1 deletion internal/output/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@ type ErrorEvent struct {
Actions []ErrorAction
}

type AuthEvent struct {
Preamble string
Code string
URL string
}

type Event interface {
MessageEvent | SpinnerEvent | ErrorEvent | ContainerStatusEvent | ProgressEvent | UserInputRequestEvent | ContainerLogLineEvent
MessageEvent | AuthEvent | SpinnerEvent | ErrorEvent | ContainerStatusEvent | ProgressEvent | UserInputRequestEvent | ContainerLogLineEvent
}

type Sink interface {
Expand Down Expand Up @@ -143,6 +149,10 @@ func EmitUserInputRequest(sink Sink, event UserInputRequestEvent) {
Emit(sink, event)
}

func EmitAuth(sink Sink, event AuthEvent) {
Emit(sink, event)
}

func EmitContainerLogLine(sink Sink, line string) {
Emit(sink, ContainerLogLineEvent{Line: line})
}
Expand Down
47 changes: 47 additions & 0 deletions internal/output/events_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package output

import "testing"

type captureSink struct {
events []any
}

func (s *captureSink) emit(event any) {
s.events = append(s.events, event)
}

func TestEmitAuth(t *testing.T) {
t.Parallel()

sink := &captureSink{}
EmitAuth(sink, AuthEvent{
Preamble: "Welcome",
Code: "ABC123",
URL: "https://example.com",
})

if len(sink.events) != 1 {
t.Fatalf("expected 1 event, got %d", len(sink.events))
}
event, ok := sink.events[0].(AuthEvent)
if !ok {
t.Fatalf("expected AuthEvent, got %T", sink.events[0])
}
if event.Code != "ABC123" {
t.Fatalf("expected code %q, got %q", "ABC123", event.Code)
}
if event.URL != "https://example.com" {
t.Fatalf("expected URL %q, got %q", "https://example.com", event.URL)
}
if event.Preamble != "Welcome" {
t.Fatalf("expected preamble %q, got %q", "Welcome", event.Preamble)
}

line, ok := FormatEventLine(event)
if !ok {
t.Fatal("expected formatter output")
}
if line != "Welcome\nYour one-time code: ABC123\nOpening browser to login...\nhttps://example.com" {
t.Fatalf("unexpected formatted line: %q", line)
}
}
55 changes: 47 additions & 8 deletions internal/output/plain_format.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ func FormatEventLine(event any) (string, bool) {
switch e := event.(type) {
case MessageEvent:
return formatMessageEvent(e), true
case AuthEvent:
return formatAuthEvent(e), true
case SpinnerEvent:
if e.Active {
return e.Text + "...", true
Expand Down Expand Up @@ -63,18 +65,55 @@ func formatProgressLine(e ProgressEvent) (string, bool) {
}

func formatUserInputRequest(e UserInputRequestEvent) string {
switch len(e.Options) {
return FormatPrompt(e.Prompt, e.Options)
}

// FormatPrompt formats a prompt string with its options into a display line.
func FormatPrompt(prompt string, options []InputOption) string {
lines := strings.Split(prompt, "\n")
firstLine := lines[0]
rest := lines[1:]
labels := make([]string, 0, len(options))
for _, opt := range options {
if opt.Label != "" {
labels = append(labels, opt.Label)
}
}

switch len(labels) {
case 0:
return e.Prompt
if len(rest) == 0 {
return firstLine
}
return strings.Join(append([]string{firstLine}, rest...), "\n")
case 1:
return fmt.Sprintf("%s (%s)", e.Prompt, e.Options[0].Label)
firstLine = fmt.Sprintf("%s (%s)", firstLine, labels[0])
default:
labels := make([]string, len(e.Options))
for i, opt := range e.Options {
labels[i] = opt.Label
}
return fmt.Sprintf("%s [%s]", e.Prompt, strings.Join(labels, "/"))
firstLine = fmt.Sprintf("%s [%s]", firstLine, strings.Join(labels, "/"))
}

if len(rest) == 0 {
return firstLine
}
return strings.Join(append([]string{firstLine}, rest...), "\n")
}

func formatAuthEvent(e AuthEvent) string {
var sb strings.Builder
if e.Preamble != "" {
sb.WriteString(e.Preamble)
sb.WriteString("\n")
}
if e.Code != "" {
sb.WriteString("Your one-time code: ")
sb.WriteString(e.Code)
sb.WriteString("\n")
}
if e.URL != "" {
sb.WriteString("Opening browser to login...\n")
sb.WriteString(e.URL)
}
return strings.TrimRight(sb.String(), "\n")
}

func formatMessageEvent(e MessageEvent) string {
Expand Down
12 changes: 12 additions & 0 deletions internal/output/plain_format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@ func TestFormatEventLine(t *testing.T) {
want: "> Warning: careful",
wantOK: true,
},
{
name: "instructions event full",
event: AuthEvent{Preamble: "Welcome", Code: "ABC123", URL: "https://example.com"},
want: "Welcome\nYour one-time code: ABC123\nOpening browser to login...\nhttps://example.com",
wantOK: true,
},
{
name: "instructions event code only",
event: AuthEvent{Code: "XYZ"},
want: "Your one-time code: XYZ",
wantOK: true,
},
{
name: "status pulling",
event: ContainerStatusEvent{Phase: "pulling", Container: "localstack/localstack:latest"},
Expand Down
3 changes: 3 additions & 0 deletions internal/output/plain_sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ func TestPlainSink_UsesFormatterParity(t *testing.T) {
MessageEvent{Severity: SeverityWarning, Text: "careful"},
MessageEvent{Severity: SeveritySuccess, Text: "done"},
MessageEvent{Severity: SeverityNote, Text: "fyi"},
AuthEvent{Code: "ABC123", URL: "https://example.com"},
SpinnerEvent{Active: true, Text: "Loading"},
ErrorEvent{Title: "Failed", Summary: "Something broke"},
ContainerStatusEvent{Phase: "starting", Container: "localstack"},
Expand All @@ -232,6 +233,8 @@ func TestPlainSink_UsesFormatterParity(t *testing.T) {
switch e := event.(type) {
case MessageEvent:
Emit(sink, e)
case AuthEvent:
Emit(sink, e)
case SpinnerEvent:
Emit(sink, e)
case ErrorEvent:
Expand Down
Loading