Skip to content

Commit 519ab02

Browse files
committed
add debugging statements
1 parent 3564536 commit 519ab02

14 files changed

Lines changed: 320 additions & 188 deletions

File tree

app/app.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@ func Get(reader io.Reader, writer io.Writer) (*cli.Command, error) {
6262
copy(origArgs, os.Args)
6363

6464
if optsEnv, exists := os.LookupEnv(EnvDefaultOpts); exists {
65-
tokens := strings.Fields(optsEnv)
65+
args := strings.Fields(optsEnv)
6666

67-
for _, token := range tokens {
67+
for _, token := range args {
6868
if strings.HasPrefix(token, "-") {
6969
if !supportedDefaultOpts[token] {
7070
return nil, errDefaultOptsParsing.Fmt(token)
7171
}
7272
}
7373
}
7474

75-
args := append(strings.Fields(optsEnv), os.Args[1:]...)
75+
args = append(args, os.Args[1:]...)
7676
os.Args = append(os.Args[:1], args...)
7777
}
7878

cmd/f2/main.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,25 @@ package main
22

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

78
"github.com/ayoisaiah/f2/v2"
9+
"github.com/ayoisaiah/f2/v2/internal/config"
810
"github.com/ayoisaiah/f2/v2/report"
911
)
1012

13+
func init() {
14+
_, exists := os.LookupEnv(config.EnvDebug)
15+
if exists {
16+
slog.SetDefault(
17+
slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{
18+
Level: slog.LevelDebug,
19+
})),
20+
)
21+
}
22+
}
23+
1124
func main() {
1225
renamer, err := f2.New(os.Stdin, os.Stdout)
1326
if err != nil {

f2.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package f2
33
import (
44
"context"
55
"io"
6+
"log/slog"
67
"os"
78

89
"github.com/urfave/cli/v3"
@@ -28,50 +29,62 @@ func isOutputToPipe() bool {
2829
}
2930

3031
// execute initiates a new renaming operation based on the provided CLI context.
31-
func execute(_ context.Context, cmd *cli.Command) error {
32+
func execute(ctx context.Context, cmd *cli.Command) error {
3233
appConfig, err := config.Init(cmd, isOutputToPipe())
3334
if err != nil {
3435
return err
3536
}
3637

37-
changes, err := find.Find(appConfig)
38+
slog.DebugContext(
39+
ctx,
40+
"working configuration",
41+
slog.Any("config", appConfig),
42+
)
43+
44+
matches, err := find.Find(appConfig)
3845
if err != nil {
3946
return err
4047
}
4148

42-
if len(changes) == 0 {
49+
slog.Debug(
50+
"find results",
51+
slog.Int("count", len(matches)),
52+
slog.Any("matches", matches),
53+
)
54+
55+
if len(matches) == 0 {
4356
report.NoMatches(appConfig)
4457

4558
return nil
4659
}
4760

4861
if !appConfig.Revert {
49-
changes, err = replace.Replace(appConfig, changes)
62+
matches, err = replace.Replace(appConfig, matches)
5063
if err != nil {
5164
return err
5265
}
5366
}
5467

5568
hasConflicts := validate.Validate(
56-
changes,
69+
matches,
5770
appConfig.AutoFixConflicts,
5871
appConfig.AllowOverwrites,
5972
)
6073

6174
if hasConflicts {
62-
report.Report(appConfig, changes, hasConflicts)
75+
report.Report(appConfig, matches, hasConflicts)
6376

6477
return errConflictDetected
6578
}
6679

6780
if !appConfig.Exec {
68-
report.Report(appConfig, changes, hasConflicts)
81+
report.Report(appConfig, matches, hasConflicts)
6982
return nil
7083
}
7184

72-
err = rename.Rename(appConfig, changes)
85+
err = rename.Rename(appConfig, matches)
7386

74-
rename.PostRename(appConfig, changes, err)
87+
rename.PostRename(appConfig, matches, err)
7588

7689
return err
7790
}

0 commit comments

Comments
 (0)