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

Commit 103b0b7

Browse files
committed
Migrate more logs to clog
1 parent e50806e commit 103b0b7

File tree

11 files changed

+60
-29
lines changed

11 files changed

+60
-29
lines changed

internal/activity/pusher.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package activity
22

33
import (
44
"context"
5+
"fmt"
56
"time"
67

78
"golang.org/x/time/rate"
89

910
"cdr.dev/coder-cli/coder-sdk"
10-
11-
"go.coder.com/flog"
11+
"cdr.dev/coder-cli/internal/clog"
1212
)
1313

1414
const pushInterval = time.Minute
@@ -42,6 +42,6 @@ func (p *Pusher) Push(ctx context.Context) {
4242
}
4343

4444
if err := p.client.PushActivity(ctx, p.source, p.envID); err != nil {
45-
flog.Error("push activity: %s", err)
45+
clog.Log(clog.Error(fmt.Sprintf("push activity: %s", err)))
4646
}
4747
}

internal/clog/error.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,28 @@ func Log(err error) {
5656
fmt.Fprintln(os.Stderr, cliErr.String())
5757
}
5858

59-
// Info formats according to a format specifier and prints the resulting CLI message.
60-
func Info(format string, args ...interface{}) {
59+
// LogInfo prints the given info message to stderr.
60+
func LogInfo(header string, lines ...string) {
6161
fmt.Fprintln(os.Stderr, RichCLIMessage{
6262
Level: "info",
6363
Color: color.FgBlue,
64-
Header: fmt.Sprintf(format, args...),
64+
Header: header,
65+
Lines: lines,
6566
}.String())
6667
}
6768

68-
// Success formats according to a format specifier and prints the resulting CLI message.
69-
func Success(format string, args ...interface{}) {
69+
// F formats according to a format specifier and resturns the resulting string.
70+
func F(format string, a ...interface{}) string {
71+
return fmt.Sprintf(format, a...)
72+
}
73+
74+
// LogSuccess prints the given info message to stderr.
75+
func LogSuccess(header string, lines ...string) {
7076
fmt.Fprintln(os.Stderr, RichCLIMessage{
7177
Level: "success",
7278
Color: color.FgGreen,
73-
Header: fmt.Sprintf(format, args...),
79+
Header: header,
80+
Lines: lines,
7481
}.String())
7582
}
7683

@@ -117,3 +124,18 @@ func Fatal(header string, lines ...string) RichCLIError {
117124
func Bold(a string) string {
118125
return color.New(color.Bold).Sprint(a)
119126
}
127+
128+
// Tip formats according to the given format specifier and prepends a bolded "tip: " header.
129+
func Tip(format string, a ...interface{}) string {
130+
return fmt.Sprintf("%s %s", Bold("tip:"), fmt.Sprintf(format, a...))
131+
}
132+
133+
// Hint formats according to the given format specifier and prepends a bolded "hint: " header.
134+
func Hint(format string, a ...interface{}) string {
135+
return fmt.Sprintf("%s %s", Bold("hint:"), fmt.Sprintf(format, a...))
136+
}
137+
138+
// Cause formats according to the given format specifier and prepends a bolded "Cause: " header.
139+
func Cause(format string, a ...interface{}) string {
140+
return fmt.Sprintf("%s %s", Bold("cause:"), fmt.Sprintf(format, a...))
141+
}

internal/cmd/ceapi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func findEnv(ctx context.Context, client *coder.Client, envName, userEmail strin
7878
"failed to find environment",
7979
fmt.Sprintf("environment %q not found in %q", envName, found),
8080
"",
81-
clog.Bold("tip: ")+"run \"coder envs ls\" to view your environments",
81+
clog.Tip("run \"coder envs ls\" to view your environments"),
8282
)
8383
}
8484

internal/cmd/envs.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func envsCommand() *cobra.Command {
3838
return err
3939
}
4040
if len(envs) < 1 {
41-
clog.Info("no environments found")
41+
clog.LogInfo("no environments found")
4242
return nil
4343
}
4444

@@ -106,10 +106,14 @@ coder envs --user charlie@coder.com ls -o json \
106106

107107
if err = client.StopEnvironment(cmd.Context(), env.ID); err != nil {
108108
atomic.AddInt32(&fails, 1)
109+
err = clog.Error(fmt.Sprintf("stop environment %q", env.Name),
110+
clog.Cause(err.Error()), "",
111+
clog.Hint("current environment status is %q", env.LatestStat.ContainerStatus),
112+
)
109113
clog.Log(err)
110-
return clog.Wrap(err, "stop environment")
114+
return err
111115
}
112-
clog.Success("successfully stopped environment %q", envName)
116+
clog.LogSuccess(fmt.Sprintf("successfully stopped environment %q", envName))
113117
return nil
114118
})
115119
}

internal/cmd/login.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func login(cmd *cobra.Command, envURL *url.URL, urlCfg, sessionCfg config.File)
139139
return xerrors.Errorf("store config: %w", err)
140140
}
141141

142-
clog.Success("logged in")
142+
clog.LogSuccess("logged in")
143143

144144
return nil
145145
}

internal/cmd/logout.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"cdr.dev/coder-cli/internal/config"
88
"github.com/spf13/cobra"
99
"golang.org/x/xerrors"
10-
11-
"go.coder.com/flog"
1210
)
1311

1412
func makeLogoutCmd() *cobra.Command {
@@ -23,11 +21,11 @@ func logout(_ *cobra.Command, _ []string) error {
2321
err := config.Session.Delete()
2422
if err != nil {
2523
if os.IsNotExist(err) {
26-
flog.Info("no active session")
24+
clog.LogInfo("no active session")
2725
return nil
2826
}
2927
return xerrors.Errorf("delete session: %w", err)
3028
}
31-
clog.Success("logged out")
29+
clog.LogSuccess("logged out")
3230
return nil
3331
}

internal/cmd/resourcemanager.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import (
88
"text/tabwriter"
99

1010
"cdr.dev/coder-cli/coder-sdk"
11+
"cdr.dev/coder-cli/internal/clog"
1112
"github.com/spf13/cobra"
12-
"go.coder.com/flog"
1313
"golang.org/x/xerrors"
1414
)
1515

@@ -212,8 +212,10 @@ func printResourceTop(writer io.Writer, groups []groupable, labeler envLabeler,
212212
_, _ = fmt.Fprint(tabwriter, "\n")
213213
}
214214
if len(userResources) == 0 {
215-
flog.Info("No groups for the given filters exist with active environments.")
216-
flog.Info("Use \"--show-empty\" to see groups with no resources.")
215+
clog.LogInfo(
216+
"no groups for the given filters exist with active environments",
217+
clog.Tip("run \"--show-empty\" to see groups with no resources."),
218+
)
217219
}
218220
return nil
219221
}

internal/cmd/secrets.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,12 @@ func makeRemoveSecrets(userEmail *string) func(c *cobra.Command, args []string)
213213
for _, n := range args {
214214
err := client.DeleteSecretByName(cmd.Context(), n, user.ID)
215215
if err != nil {
216-
flog.Error("failed to delete secret %q: %v", n, err)
216+
clog.Log(clog.Error(
217+
fmt.Sprintf("failed to delete secret %q: %v", n, err),
218+
))
217219
errorSeen = true
218220
} else {
219-
clog.Success("successfully deleted secret: %q", n)
221+
clog.LogSuccess("successfully deleted secret: %q", n)
220222
}
221223
}
222224
if errorSeen {

internal/cmd/shell.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ func networkErr(client *coder.Client, env *coder.Environment) error {
203203
fmt.Sprintf("environment %q is not running", env.Name),
204204
fmt.Sprintf("its current status is %q", env.LatestStat.ContainerStatus),
205205
"",
206-
clog.Bold("tip: ")+fmt.Sprintf("run \"coder envs rebuild %s --follow\" to start the environment", env.Name),
206+
clog.Tip("run \"coder envs rebuild %s --follow\" to start the environment", env.Name),
207207
)
208208
}
209209
return xerrors.Errorf("network error, is %q online?", env.Name)

internal/cmd/urls.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"golang.org/x/xerrors"
1515

1616
"cdr.dev/coder-cli/coder-sdk"
17+
"cdr.dev/coder-cli/internal/clog"
1718
"cdr.dev/coder-cli/internal/x/xtabwriter"
1819

1920
"go.coder.com/flog"
@@ -70,7 +71,7 @@ var urlAccessLevel = map[string]string{
7071
func validatePort(port string) (int, error) {
7172
p, err := strconv.ParseUint(port, 10, 16)
7273
if err != nil {
73-
flog.Error("Invalid port")
74+
clog.Log(clog.Error("invalid port"))
7475
return 0, err
7576
}
7677
if p < 1 {
@@ -83,7 +84,7 @@ func validatePort(port string) (int, error) {
8384
func accessLevelIsValid(level string) bool {
8485
_, ok := urlAccessLevel[level]
8586
if !ok {
86-
flog.Error("Invalid access level")
87+
clog.Log(clog.Error("invalid access level"))
8788
}
8889
return ok
8990
}

internal/sync/sync.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ func (s Sync) initSync() error {
129129
if err := s.syncPaths(true, s.LocalDir+"/.", s.RemoteDir); err != nil {
130130
return err
131131
}
132-
clog.Success("finished initial sync (%s)", time.Since(start).Truncate(time.Millisecond))
132+
clog.LogSuccess(
133+
fmt.Sprintf("finished initial sync (%s)", time.Since(start).Truncate(time.Millisecond)),
134+
)
133135
return nil
134136
}
135137

@@ -201,9 +203,9 @@ func (s Sync) work(ev timedEvent) {
201203
ev.Event(), filepath.Base(localPath), time.Since(ev.CreatedAt).Truncate(time.Millisecond*10),
202204
)
203205
if err != nil {
204-
flog.Error(log+": %s", err)
206+
clog.Log(clog.Error(fmt.Sprintf("%s: %s", log, err)))
205207
} else {
206-
clog.Success(log)
208+
clog.LogSuccess(log)
207209
}
208210
}
209211

0 commit comments

Comments
 (0)