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

Commit e50806e

Browse files
committed
Seperate clog errors from messages
1 parent a430bb5 commit e50806e

File tree

7 files changed

+52
-43
lines changed

7 files changed

+52
-43
lines changed

cmd/coder/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"cdr.dev/coder-cli/internal/clog"
1313
"cdr.dev/coder-cli/internal/cmd"
1414
"cdr.dev/coder-cli/internal/x/xterminal"
15-
16-
"go.coder.com/flog"
1715
)
1816

1917
// Using a global for the version so it can be set at build time using ldflags.
@@ -32,7 +30,8 @@ func main() {
3230

3331
stdoutState, err := xterminal.MakeOutputRaw(os.Stdout.Fd())
3432
if err != nil {
35-
flog.Fatal("set output to raw: %s", err)
33+
clog.Log(clog.Fatal(fmt.Sprintf("set output to raw: %s", err)))
34+
os.Exit(1)
3635
}
3736
defer func() {
3837
// Best effort. Would result in broken terminal on window but nothing we can do about it.

internal/clog/error.go

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package clog
33
import (
44
"errors"
55
"fmt"
6+
"os"
67
"strings"
78

89
"github.com/fatih/color"
@@ -15,6 +16,10 @@ type RichCLIMessage struct {
1516
Color color.Attribute
1617
Header string
1718
Lines []string
19+
}
20+
21+
type RichCLIError struct {
22+
RichCLIMessage
1823
error
1924
}
2025

@@ -31,30 +36,29 @@ func (r RichCLIMessage) String() string {
3136
return str.String()
3237
}
3338

34-
// String gives the same formatting as Error.
35-
func (r RichCLIMessage) Error() string {
36-
return r.error.Error()
37-
}
3839

40+
// Wrap wraps the underling error, but does not add context IF a downstream
41+
// error is a RichCLIError. The original human-readable message is retained.
3942
func Wrap(err error, msg string) error {
40-
var cliError RichCLIMessage
43+
var cliError RichCLIError
4144
if xerrors.As(err, &cliError) {
4245
return xerrors.Errorf("%s: %w", msg, cliError.error)
4346
}
4447
return xerrors.Errorf("%s: %w", msg, err)
4548
}
4649

50+
// Log logs the given error to stderr, defaulting to "fatal" if the error is not a RichCLIError.
4751
func Log(err error) {
48-
var cliErr RichCLIMessage
52+
var cliErr RichCLIError
4953
if !xerrors.As(err, &cliErr) {
5054
cliErr = Fatal(err.Error())
5155
}
52-
fmt.Println(cliErr.String())
56+
fmt.Fprintln(os.Stderr, cliErr.String())
5357
}
5458

5559
// Info formats according to a format specifier and prints the resulting CLI message.
5660
func Info(format string, args ...interface{}) {
57-
fmt.Println(RichCLIMessage{
61+
fmt.Fprintln(os.Stderr, RichCLIMessage{
5862
Level: "info",
5963
Color: color.FgBlue,
6064
Header: fmt.Sprintf(format, args...),
@@ -63,43 +67,49 @@ func Info(format string, args ...interface{}) {
6367

6468
// Success formats according to a format specifier and prints the resulting CLI message.
6569
func Success(format string, args ...interface{}) {
66-
fmt.Println(RichCLIMessage{
70+
fmt.Fprintln(os.Stderr, RichCLIMessage{
6771
Level: "success",
6872
Color: color.FgGreen,
6973
Header: fmt.Sprintf(format, args...),
7074
}.String())
7175
}
7276

7377
// Warn creates an error with the level "warning".
74-
func Warn(header string, lines ...string) RichCLIMessage {
75-
return RichCLIMessage{
76-
Color: color.FgYellow,
77-
Level: "warning",
78-
Header: header,
79-
Lines: lines,
80-
error: errors.New(header),
78+
func Warn(header string, lines ...string) RichCLIError {
79+
return RichCLIError{
80+
RichCLIMessage: RichCLIMessage{
81+
Color: color.FgYellow,
82+
Level: "warning",
83+
Header: header,
84+
Lines: lines,
85+
},
86+
error: errors.New(header),
8187
}
8288
}
8389

8490
// Error creates an error with the level "error".
85-
func Error(header string, lines ...string) RichCLIMessage {
86-
return RichCLIMessage{
87-
Color: color.FgRed,
88-
Level: "error",
89-
Header: header,
90-
Lines: lines,
91-
error: errors.New(header),
91+
func Error(header string, lines ...string) RichCLIError {
92+
return RichCLIError{
93+
RichCLIMessage: RichCLIMessage{
94+
Color: color.FgRed,
95+
Level: "error",
96+
Header: header,
97+
Lines: lines,
98+
},
99+
error: errors.New(header),
92100
}
93101
}
94102

95103
// Fatal creates an error with the level "fatal".
96-
func Fatal(header string, lines ...string) RichCLIMessage {
97-
return RichCLIMessage{
98-
Color: color.FgRed,
99-
Level: "fatal",
100-
Header: header,
101-
Lines: lines,
102-
error: errors.New(header),
104+
func Fatal(header string, lines ...string) RichCLIError {
105+
return RichCLIError{
106+
RichCLIMessage: RichCLIMessage{
107+
Color: color.FgRed,
108+
Level: "fatal",
109+
Header: header,
110+
Lines: lines,
111+
},
112+
error: errors.New(header),
103113
}
104114
}
105115

internal/cmd/envs.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"github.com/spf13/cobra"
1313
"golang.org/x/sync/errgroup"
1414
"golang.org/x/xerrors"
15-
16-
"go.coder.com/flog"
1715
)
1816

1917
func envsCommand() *cobra.Command {
@@ -40,7 +38,7 @@ func envsCommand() *cobra.Command {
4038
return err
4139
}
4240
if len(envs) < 1 {
43-
flog.Info("no environments found")
41+
clog.Info("no environments found")
4442
return nil
4543
}
4644

internal/cmd/login.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ import (
99
"strings"
1010

1111
"cdr.dev/coder-cli/coder-sdk"
12+
"cdr.dev/coder-cli/internal/clog"
1213
"cdr.dev/coder-cli/internal/config"
1314
"cdr.dev/coder-cli/internal/loginsrv"
1415
"github.com/pkg/browser"
1516
"github.com/spf13/cobra"
1617
"golang.org/x/sync/errgroup"
1718
"golang.org/x/xerrors"
18-
19-
"go.coder.com/flog"
2019
)
2120

2221
func makeLoginCmd() *cobra.Command {
@@ -140,7 +139,7 @@ func login(cmd *cobra.Command, envURL *url.URL, urlCfg, sessionCfg config.File)
140139
return xerrors.Errorf("store config: %w", err)
141140
}
142141

143-
flog.Success("Logged in.")
142+
clog.Success("logged in")
144143

145144
return nil
146145
}

internal/cmd/logout.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"os"
55

6+
"cdr.dev/coder-cli/internal/clog"
67
"cdr.dev/coder-cli/internal/config"
78
"github.com/spf13/cobra"
89
"golang.org/x/xerrors"
@@ -27,6 +28,6 @@ func logout(_ *cobra.Command, _ []string) error {
2728
}
2829
return xerrors.Errorf("delete session: %w", err)
2930
}
30-
flog.Success("logged out")
31+
clog.Success("logged out")
3132
return nil
3233
}

internal/cmd/secrets.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"golang.org/x/xerrors"
1111

1212
"cdr.dev/coder-cli/coder-sdk"
13+
"cdr.dev/coder-cli/internal/clog"
1314
"cdr.dev/coder-cli/internal/x/xtabwriter"
1415

1516
"go.coder.com/flog"
@@ -215,7 +216,7 @@ func makeRemoveSecrets(userEmail *string) func(c *cobra.Command, args []string)
215216
flog.Error("failed to delete secret %q: %v", n, err)
216217
errorSeen = true
217218
} else {
218-
flog.Success("successfully deleted secret %q", n)
219+
clog.Success("successfully deleted secret: %q", n)
219220
}
220221
}
221222
if errorSeen {

internal/sync/sync.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"cdr.dev/coder-cli/coder-sdk"
2727
"cdr.dev/coder-cli/internal/activity"
28+
"cdr.dev/coder-cli/internal/clog"
2829
"cdr.dev/wsep"
2930
)
3031

@@ -128,7 +129,7 @@ func (s Sync) initSync() error {
128129
if err := s.syncPaths(true, s.LocalDir+"/.", s.RemoteDir); err != nil {
129130
return err
130131
}
131-
flog.Success("finished initial sync (%s)", time.Since(start).Truncate(time.Millisecond))
132+
clog.Success("finished initial sync (%s)", time.Since(start).Truncate(time.Millisecond))
132133
return nil
133134
}
134135

@@ -202,7 +203,7 @@ func (s Sync) work(ev timedEvent) {
202203
if err != nil {
203204
flog.Error(log+": %s", err)
204205
} else {
205-
flog.Success(log)
206+
clog.Success(log)
206207
}
207208
}
208209

0 commit comments

Comments
 (0)