Skip to content

Commit 42f890f

Browse files
committed
fix: make LAN preparation status transient
1 parent 458f600 commit 42f890f

3 files changed

Lines changed: 31 additions & 7 deletions

File tree

internal/app/lan_qr.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,17 @@ func renderLANSetupQR(setupURL string) (string, int, error) {
4343
return output.String(), width, nil
4444
}
4545

46-
func maybePrintLANSetupQR(output io.Writer, setupURL string) bool {
46+
func isTerminalOutput(output io.Writer) bool {
4747
file, ok := output.(*os.File)
4848
if !ok {
4949
return false
5050
}
5151
info, err := file.Stat()
52-
if err != nil || info.Mode()&os.ModeCharDevice == 0 {
52+
return err == nil && info.Mode()&os.ModeCharDevice != 0
53+
}
54+
55+
func maybePrintLANSetupQR(output io.Writer, setupURL string) bool {
56+
if !isTerminalOutput(output) {
5357
return false
5458
}
5559
rendered, width, err := renderLANSetupQR(setupURL)

internal/app/lan_share.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ func createLANShare(ctx context.Context, client adminClient, cmd cli.Command, re
2727
if !ok {
2828
return nil, errors.New("router control does not support LAN sharing; update gohere")
2929
}
30-
if progress != nil {
31-
fmt.Fprintln(progress, "Preparing LAN access…")
32-
}
30+
finishProgress := startLANProgress(progress, isTerminalOutput(progress))
31+
defer finishProgress()
3332
result, err := lanClient.CreateLANShare(ctx, ref)
3433
if err != nil {
3534
if errors.Is(err, context.DeadlineExceeded) || strings.Contains(err.Error(), "Client.Timeout exceeded") {
@@ -40,6 +39,15 @@ func createLANShare(ctx context.Context, client adminClient, cmd cli.Command, re
4039
return &result, nil
4140
}
4241

42+
func startLANProgress(output io.Writer, terminal bool) func() {
43+
if output == nil || !terminal {
44+
return func() {}
45+
}
46+
const message = "Preparing LAN access…"
47+
_, _ = fmt.Fprint(output, message)
48+
return func() { _, _ = fmt.Fprintf(output, "\r%s\r", strings.Repeat(" ", len([]rune(message)))) }
49+
}
50+
4351
func deleteLANShare(ctx context.Context, client adminClient, ref router.RouteRef) error {
4452
lanClient, ok := client.(lanShareClient)
4553
if !ok {

internal/app/share_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func TestCreateAndPrintLANShare(t *testing.T) {
3131
if err != nil {
3232
t.Fatal(err)
3333
}
34-
if progress.String() != "Preparing LAN access…\n" {
35-
t.Fatalf("progress = %q", progress.String())
34+
if progress.String() != "" {
35+
t.Fatalf("non-terminal progress = %q", progress.String())
3636
}
3737
if client.created != ref || result.URL != "https://shop.local" {
3838
t.Fatalf("created = %#v, result = %#v", client.created, result)
@@ -51,6 +51,18 @@ func TestCreateAndPrintLANShare(t *testing.T) {
5151
}
5252
}
5353

54+
func TestLANProgressIsTransientOnTerminal(t *testing.T) {
55+
var output bytes.Buffer
56+
finish := startLANProgress(&output, true)
57+
if output.String() != "Preparing LAN access…" {
58+
t.Fatalf("progress start = %q", output.String())
59+
}
60+
finish()
61+
if output.String() != "Preparing LAN access…\r \r" {
62+
t.Fatalf("progress finish = %q", output.String())
63+
}
64+
}
65+
5466
func TestCreateLANShareExplainsWindowsFirewallTimeout(t *testing.T) {
5567
client := &fakeLANShareClient{createErr: errors.New(`companion authority_error: Post "http://127.0.0.1:39399/v2/lan-shares": context deadline exceeded (Client.Timeout exceeded while awaiting headers)`)}
5668
_, err := createLANShare(t.Context(), client, cli.Command{ShareMode: "lan"}, router.RouteRef{ID: "route-1", Generation: 1}, nil)

0 commit comments

Comments
 (0)