Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit 282f351

Browse files
authored
Fix rebuild output for non-terminals (#148)
1 parent 70615c4 commit 282f351

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

internal/cmd/rebuild.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"strings"
78
"time"
89

@@ -12,6 +13,7 @@ import (
1213
"github.com/manifoldco/promptui"
1314
"github.com/spf13/cobra"
1415
"go.coder.com/flog"
16+
"golang.org/x/crypto/ssh/terminal"
1517
"golang.org/x/xerrors"
1618
)
1719

@@ -74,6 +76,9 @@ func trailBuildLogs(ctx context.Context, client *coder.Client, envID string) err
7476

7577
newSpinner := func() *spinner.Spinner { return spinner.New(spinner.CharSets[11], 100*time.Millisecond) }
7678

79+
// this tells us whether to show dynamic loaders when printing output
80+
isTerminal := terminal.IsTerminal(int(os.Stdout.Fd()))
81+
7782
logs, err := client.FollowEnvironmentBuildLog(ctx, envID)
7883
if err != nil {
7984
return err
@@ -88,23 +93,32 @@ func trailBuildLogs(ctx context.Context, client *coder.Client, envID string) err
8893
// the FE uses this to reset the UI
8994
// the CLI doesn't need to do anything here given that we only append to the trail
9095
case coder.BuildLogTypeStage:
96+
msg := fmt.Sprintf("%s %s", l.BuildLog.Time.Format(time.RFC3339), l.BuildLog.Msg)
97+
if !isTerminal {
98+
fmt.Println(msg)
99+
continue
100+
}
91101
if s != nil {
92102
s.Stop()
93103
fmt.Print("\n")
94104
}
95105
s = newSpinner()
96-
msg := fmt.Sprintf("%s %s", l.BuildLog.Time.Format(time.RFC3339), l.BuildLog.Msg)
97106
s.Suffix = fmt.Sprintf(" -- %s", msg)
98107
s.FinalMSG = fmt.Sprintf("%s -- %s", check, msg)
99108
s.Start()
100109
case coder.BuildLogTypeSubstage:
101110
// TODO(@cmoog) add verbose substage printing
102111
case coder.BuildLogTypeError:
112+
errMsg := color.RedString("\t%s", l.BuildLog.Msg)
113+
if !isTerminal {
114+
fmt.Println(errMsg)
115+
continue
116+
}
103117
if s != nil {
104118
s.FinalMSG = fmt.Sprintf("%s %s", failure, strings.TrimPrefix(s.Suffix, " "))
105119
s.Stop()
106120
}
107-
fmt.Print(color.RedString("\t%s", l.BuildLog.Msg))
121+
fmt.Print(errMsg)
108122
s = newSpinner()
109123
case coder.BuildLogTypeDone:
110124
if s != nil {

0 commit comments

Comments
 (0)