Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
17 changes: 15 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
10 changes: 7 additions & 3 deletions npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Loading