From 9e4d9353fb24919327d2a35e6bcfe84f441235b2 Mon Sep 17 00:00:00 2001 From: Zied Yousfi Date: Sat, 14 Mar 2026 09:57:41 +0100 Subject: [PATCH] refactor(ctx.go, main.go, npm.go): add dry-run and verbose flags - Introduce DryRun and Verbose flags in Context. - Parse --dry-run (-d) and --verbose (-v) CLI flags in main.go. - Adjust logging level when Verbose mode is enabled. - Make npm.go respect DryRun: skip writes and log purposefully if flagged. --- ctx.go | 6 ++++++ main.go | 17 +++++++++++++++-- npm.go | 10 +++++++--- 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/ctx.go b/ctx.go index 35111bc..dca734d 100644 --- a/ctx.go +++ b/ctx.go @@ -17,10 +17,16 @@ type Context struct { ProjectType TypeOfProject ProjectFilePath string HTTPClient *http.Client + + // Flags + DryRun bool + Verbose bool } var Ctx = Context{ ProjectType: NotSupported, ProjectFilePath: "", HTTPClient: &http.Client{Timeout: 5 * time.Second}, + DryRun: false, + Verbose: false, } diff --git a/main.go b/main.go index be1f926..aebbdd3 100644 --- a/main.go +++ b/main.go @@ -1,9 +1,11 @@ package main import ( + "flag" "fmt" - "github.com/charmbracelet/log" "os" + + "github.com/charmbracelet/log" ) // Do not change it (maps in go can't be constants) @@ -57,7 +59,18 @@ func scanProjectFiles(entries []os.DirEntry, process func(TypeOfProject, string) } func main() { - log.SetLevel(log.InfoLevel) + log.SetLevel(log.WarnLevel) + + flag.BoolVar(&Ctx.DryRun, "dry-run", false, "Perform a dry run without making any changes") + flag.BoolVar(&Ctx.DryRun, "d", false, "Perform a dry run without making any changes (shorthand)") + flag.BoolVar(&Ctx.Verbose, "verbose", false, "Enable verbose logging") + flag.BoolVar(&Ctx.Verbose, "v", false, "Enable verbose logging (shorthand)") + + flag.Parse() + + if Ctx.Verbose { + log.SetLevel(log.DebugLevel) + } dir, err := os.Getwd() if err != nil { diff --git a/npm.go b/npm.go index d81c70f..c901419 100644 --- a/npm.go +++ b/npm.go @@ -151,10 +151,14 @@ func processNPMPackage(packagePath string) error { return err } - if err := os.WriteFile(packagePath, append(finalJSON, '\n'), 0644); err != nil { - return err + if Ctx.DryRun == false { + if err := os.WriteFile(packagePath, append(finalJSON, '\n'), 0644); err != nil { + return err + } + log.Infof("Updated %v with final JSON", packagePath) + } else { + log.Infof("Dry run enabled, not writing changes to %v", packagePath) } - log.Infof("Updated %v with final JSON", packagePath) return nil }