Skip to content

Commit 03f7962

Browse files
authored
Merge pull request #159 from shelltime/refactor/replace-logrus-with-slog-and-fix-typos
refactor(logging): replace logrus with stdlib slog and add path helpers
2 parents 0e1cc16 + b6e8bf6 commit 03f7962

30 files changed

Lines changed: 274 additions & 220 deletions

cmd/cli/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"log/slog"
67
"os"
78
"time"
89

910
"github.com/malamtime/cli/commands"
1011
"github.com/malamtime/cli/model"
11-
"github.com/sirupsen/logrus"
1212
"github.com/uptrace/uptrace-go/uptrace"
1313
"github.com/urfave/cli/v2"
1414
"go.opentelemetry.io/otel/attribute"
@@ -110,7 +110,7 @@ func main() {
110110
}
111111
err = app.Run(os.Args)
112112
if err != nil {
113-
logrus.Errorln(err)
113+
slog.Error("CLI error", slog.Any("err", err))
114114
}
115115
commands.CloseLogger()
116116
}

cmd/daemon/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func main() {
8888
defer syncCircuitBreakerService.Stop()
8989
}
9090

91-
go daemon.SocketTopicProccessor(msg)
91+
go daemon.SocketTopicProcessor(msg)
9292

9393
// Start CCUsage service if enabled (v1 - ccusage CLI based)
9494
if cfg.CCUsage != nil && cfg.CCUsage.Enabled != nil && *cfg.CCUsage.Enabled {

commands/alias.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package commands
22

33
import (
44
"context"
5+
"log/slog"
56
"os"
67
"strings"
78

89
"github.com/malamtime/cli/model"
9-
"github.com/sirupsen/logrus"
1010
"github.com/urfave/cli/v2"
1111
"go.opentelemetry.io/otel/attribute"
1212
"go.opentelemetry.io/otel/trace"
@@ -51,25 +51,24 @@ func importAliases(c *cli.Context) error {
5151
ctx, span := commandTracer.Start(c.Context, "alias-import", trace.WithSpanKind(trace.SpanKindClient))
5252
defer span.End()
5353
SetupLogger(os.ExpandEnv("$HOME/" + model.COMMAND_BASE_STORAGE_FOLDER))
54-
logrus.SetLevel(logrus.TraceLevel)
5554

5655
isFullyRefresh := c.Bool("fully-refresh")
5756
span.SetAttributes(attribute.Bool("fully-refresh", isFullyRefresh))
5857

5958
zshConfigFile, err := expandPath(c.String("zsh-config"))
6059
if err != nil {
61-
logrus.Errorln(err)
60+
slog.Error("failed to expand zsh config path", slog.Any("err", err))
6261
return err
6362
}
6463
fishConfigFile, err := expandPath(c.String("fish-config"))
6564
if err != nil {
66-
logrus.Errorln(err)
65+
slog.Error("failed to expand fish config path", slog.Any("err", err))
6766
return err
6867
}
6968

7069
config, err := configService.ReadConfigFile(ctx)
7170
if err != nil {
72-
logrus.Errorln(err)
71+
slog.Error("failed to read config file", slog.Any("err", err))
7372
return err
7473
}
7574

@@ -81,10 +80,10 @@ func importAliases(c *cli.Context) error {
8180
if _, err := os.Stat(zshConfigFile); err == nil {
8281
aliases, err := parseZshAliases(ctx, zshConfigFile)
8382
if err != nil {
84-
logrus.Errorln("Failed to parse zsh aliases:", err)
83+
slog.Error("Failed to parse zsh aliases", slog.Any("err", err))
8584
return err
8685
}
87-
logrus.Traceln("Found aliases in zsh configuration", len(aliases))
86+
slog.Debug("Found aliases in zsh configuration", slog.Int("count", len(aliases)))
8887
err = model.SendAliasesToServer(
8988
ctx,
9089
mainEndpoint,
@@ -94,18 +93,18 @@ func importAliases(c *cli.Context) error {
9493
zshConfigFile,
9594
)
9695
if err != nil {
97-
logrus.Errorln("Failed to send aliases to server:", err)
96+
slog.Error("Failed to send aliases to server", slog.Any("err", err))
9897
return err
9998
}
10099
}
101100

102101
if _, err := os.Stat(fishConfigFile); err == nil {
103102
aliases, err := parseFishAliases(ctx, fishConfigFile)
104103
if err != nil {
105-
logrus.Errorln("Failed to parse fish aliases:", err)
104+
slog.Error("Failed to parse fish aliases", slog.Any("err", err))
106105
return err
107106
}
108-
logrus.Traceln("Found aliases in fish configuration", len(aliases))
107+
slog.Debug("Found aliases in fish configuration", slog.Int("count", len(aliases)))
109108
err = model.SendAliasesToServer(
110109
ctx,
111110
mainEndpoint,
@@ -115,12 +114,12 @@ func importAliases(c *cli.Context) error {
115114
fishConfigFile,
116115
)
117116
if err != nil {
118-
logrus.Errorln("Failed to send aliases to server:", err)
117+
slog.Error("Failed to send aliases to server", slog.Any("err", err))
119118
return err
120119
}
121120
}
122121

123-
logrus.Infoln("Successfully imported aliases")
122+
slog.Info("Successfully imported aliases")
124123
return nil
125124
}
126125

commands/auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package commands
33
import (
44
"context"
55
"fmt"
6+
"log/slog"
67
"os"
78
"time"
89

@@ -11,7 +12,6 @@ import (
1112
"github.com/malamtime/cli/model"
1213
"github.com/pelletier/go-toml/v2"
1314
"github.com/pkg/browser"
14-
"github.com/sirupsen/logrus"
1515
"github.com/urfave/cli/v2"
1616
"go.opentelemetry.io/otel/trace"
1717
)
@@ -102,7 +102,7 @@ func ApplyTokenByHandshake(_ctx context.Context, config model.ShellTimeConfig) (
102102
feLink := fmt.Sprintf("%s/cli/integration?hid=%s", config.WebEndpoint, hid)
103103

104104
if err := browser.OpenURL(feLink); err != nil {
105-
logrus.Errorln(err)
105+
slog.Error("failed to open browser", slog.Any("err", err))
106106
}
107107

108108
color.Green.Println(fmt.Sprintf("Open %s to continue", feLink))

commands/dotfiles_pull.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package commands
22

33
import (
44
"fmt"
5+
"log/slog"
56
"os"
67

78
"github.com/malamtime/cli/model"
89
"github.com/pterm/pterm"
9-
"github.com/sirupsen/logrus"
1010
"github.com/urfave/cli/v2"
1111
"go.opentelemetry.io/otel/attribute"
1212
"go.opentelemetry.io/otel/trace"
@@ -37,7 +37,7 @@ func printPullResults(result map[model.DotfileAppName][]dotfilePullFileResult, d
3737

3838
// No files to process
3939
if totalProcessed == 0 && totalFailed == 0 && totalSkipped == 0 {
40-
logrus.Infoln("No dotfiles found to process")
40+
slog.Info("No dotfiles found to process")
4141
pterm.Info.Println("No dotfiles to process")
4242
return
4343
}
@@ -117,7 +117,7 @@ func printPullResults(result map[model.DotfileAppName][]dotfilePullFileResult, d
117117
}
118118

119119
// Log for debugging
120-
logrus.Infof("Pull complete - Processed: %d, Skipped: %d, Failed: %d", totalProcessed, totalSkipped, totalFailed)
120+
slog.Info("Pull complete", slog.Int("processed", totalProcessed), slog.Int("skipped", totalSkipped), slog.Int("failed", totalFailed))
121121
}
122122

123123
func pullDotfiles(c *cli.Context) error {
@@ -131,7 +131,7 @@ func pullDotfiles(c *cli.Context) error {
131131

132132
config, err := configService.ReadConfigFile(ctx)
133133
if err != nil {
134-
logrus.Errorln(err)
134+
slog.Error("failed to read config file", slog.Any("err", err))
135135
return err
136136
}
137137

@@ -169,15 +169,15 @@ func pullDotfiles(c *cli.Context) error {
169169
}
170170

171171
// Fetch dotfiles from server
172-
logrus.Infof("Fetching dotfiles from server...")
172+
slog.Info("Fetching dotfiles from server...")
173173
resp, err := model.FetchDotfilesFromServer(ctx, mainEndpoint, filter)
174174
if err != nil {
175-
logrus.Errorln("Failed to fetch dotfiles from server:", err)
175+
slog.Error("Failed to fetch dotfiles from server", slog.Any("err", err))
176176
return err
177177
}
178178

179179
if resp == nil || len(resp.Data.FetchUser.Dotfiles.Apps) == 0 {
180-
logrus.Infoln("No dotfiles found on server")
180+
slog.Info("No dotfiles found on server")
181181
fmt.Println("\n📭 No dotfiles found on server")
182182
return nil
183183
}
@@ -188,18 +188,18 @@ func pullDotfiles(c *cli.Context) error {
188188
appName := model.DotfileAppName(appData.App)
189189
app, exists := appHandlers[appName]
190190
if !exists {
191-
logrus.Warnf("Unknown app type: %s", appData.App)
191+
slog.Warn("Unknown app type", slog.String("app", appData.App))
192192
continue
193193
}
194194

195-
logrus.Infof("Processing %s dotfiles...", appData.App)
195+
slog.Info("Processing dotfiles...", slog.String("app", appData.App))
196196

197197
// Collect files to process for this app
198198
filesToProcess := make(map[string]string)
199199

200200
for _, file := range appData.Files {
201201
if len(file.Records) == 0 {
202-
logrus.Debugf("No records found for %s", file.Path)
202+
slog.Debug("No records found", slog.String("path", file.Path))
203203
continue
204204
}
205205

@@ -246,7 +246,7 @@ func pullDotfiles(c *cli.Context) error {
246246
// Check which files are different
247247
equalityMap, err := app.IsEqual(ctx, filesToProcess)
248248
if err != nil {
249-
logrus.Warnf("Failed to check file equality for %s: %v", appData.App, err)
249+
slog.Warn("Failed to check file equality", slog.String("app", appData.App), slog.Any("err", err))
250250
}
251251

252252
// Filter out files that are already equal
@@ -255,7 +255,7 @@ func pullDotfiles(c *cli.Context) error {
255255

256256
for path, content := range filesToProcess {
257257
if isEqual, exists := equalityMap[path]; exists && isEqual {
258-
logrus.Debugf("Skipping %s - content is identical", path)
258+
slog.Debug("Skipping - content is identical", slog.String("path", path))
259259
result[appName] = append(result[appName], dotfilePullFileResult{
260260
path: path,
261261
isSkipped: true,
@@ -267,20 +267,20 @@ func pullDotfiles(c *cli.Context) error {
267267
}
268268

269269
if len(filesToUpdate) == 0 {
270-
logrus.Infof("All %s files are up to date", appData.App)
270+
slog.Info("All files are up to date", slog.String("app", appData.App))
271271
continue
272272
}
273273

274274
results := make([]dotfilePullFileResult, 0)
275275

276276
// Backup files that will be modified (handles dry-run internally)
277277
if err := app.Backup(ctx, pathsToActuallyBackup, dryRun); err != nil {
278-
logrus.Warnf("Failed to backup files for %s: %v", appData.App, err)
278+
slog.Warn("Failed to backup files", slog.String("app", appData.App), slog.Any("err", err))
279279
}
280280

281281
// Save the updated files (handles dry-run internally)
282282
if err := app.Save(ctx, filesToUpdate, dryRun); err != nil {
283-
logrus.Errorf("Failed to save files for %s: %v", appData.App, err)
283+
slog.Error("Failed to save files", slog.String("app", appData.App), slog.Any("err", err))
284284
for f := range filesToUpdate {
285285
results = append(results, dotfilePullFileResult{
286286
path: f,

commands/dotfiles_push.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package commands
22

33
import (
44
"fmt"
5+
"log/slog"
56
"os"
67

78
"github.com/malamtime/cli/model"
8-
"github.com/sirupsen/logrus"
99
"github.com/urfave/cli/v2"
1010
"go.opentelemetry.io/otel/attribute"
1111
"go.opentelemetry.io/otel/trace"
@@ -21,7 +21,7 @@ func pushDotfiles(c *cli.Context) error {
2121

2222
config, err := configService.ReadConfigFile(ctx)
2323
if err != nil {
24-
logrus.Errorln(err)
24+
slog.Error("failed to read config file", slog.Any("err", err))
2525
return err
2626
}
2727

@@ -66,39 +66,39 @@ func pushDotfiles(c *cli.Context) error {
6666
if app, ok := appMap[appName]; ok {
6767
selectedApps = append(selectedApps, app)
6868
} else {
69-
logrus.Warnf("Unknown app: %s", appName)
69+
slog.Warn("Unknown app", slog.String("app", appName))
7070
}
7171
}
7272
}
7373

7474
// Collect all dotfiles
7575
var allDotfiles []model.DotfileItem
7676
for _, app := range selectedApps {
77-
logrus.Infof("Collecting dotfiles for %s", app.Name())
77+
slog.Info("Collecting dotfiles", slog.String("app", app.Name()))
7878
dotfiles, err := app.CollectDotfiles(ctx)
7979
if err != nil {
80-
logrus.Errorf("Failed to collect dotfiles for %s: %v", app.Name(), err)
80+
slog.Error("Failed to collect dotfiles", slog.String("app", app.Name()), slog.Any("err", err))
8181
continue
8282
}
8383
allDotfiles = append(allDotfiles, dotfiles...)
8484
}
8585

8686
if len(allDotfiles) == 0 {
87-
logrus.Infoln("No dotfiles found to push")
87+
slog.Info("No dotfiles found to push")
8888
return nil
8989
}
9090

9191
// Send to server
92-
logrus.Infof("Pushing %d dotfiles to server", len(allDotfiles))
92+
slog.Info("Pushing dotfiles to server", slog.Int("count", len(allDotfiles)))
9393
userID, err := model.SendDotfilesToServer(ctx, mainEndpoint, allDotfiles)
9494
if err != nil {
95-
logrus.Errorln("Failed to send dotfiles to server:", err)
95+
slog.Error("Failed to send dotfiles to server", slog.Any("err", err))
9696
return err
9797
}
9898

9999
// Generate web link for managing dotfiles
100100
webLink := fmt.Sprintf("%s/users/%d/settings/dotfiles", config.WebEndpoint, userID)
101-
logrus.Infof("Successfully pushed dotfiles. Manage them at: %s", webLink)
101+
slog.Info("Successfully pushed dotfiles", slog.String("webLink", webLink))
102102
fmt.Printf("\n✅ Successfully pushed %d dotfiles to server\n", len(allDotfiles))
103103
fmt.Printf("📁 Manage your dotfiles at: %s\n", webLink)
104104

0 commit comments

Comments
 (0)