From e2ec575af07c9c707dda56fd83cccb734c9de98b Mon Sep 17 00:00:00 2001 From: Hau Nguyen Date: Tue, 16 Aug 2022 10:34:55 +0700 Subject: [PATCH] fix: remove default value of deps file --- Makefile | 1 + internal/cli/action.go | 18 +++++++++++++---- internal/cli/action_run.go | 20 +++++++++---------- internal/cli/app.go | 41 +++++++++++++++++++------------------- 4 files changed, 45 insertions(+), 35 deletions(-) diff --git a/Makefile b/Makefile index c042289..91139c0 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,7 @@ .PHONY: all test-color lint all: test-color lint + go mod tidy test-color: go install github.com/haunt98/go-test-color@latest diff --git a/internal/cli/action.go b/internal/cli/action.go index 02daf89..d2c472d 100644 --- a/internal/cli/action.go +++ b/internal/cli/action.go @@ -6,6 +6,10 @@ import ( "github.com/urfave/cli/v2" ) +const ( + defaultDepsFile = ".deps" +) + type action struct { flags struct { depsFile string @@ -20,12 +24,18 @@ func (a *action) RunHelp(c *cli.Context) error { } func (a *action) getFlags(c *cli.Context) { - a.flags.verbose = c.Bool(flagVerbose) - a.flags.depsFile = c.String(flagDepsFile) - a.flags.depsURL = c.String(flagDepsURL) + a.flags.verbose = c.Bool(flagVerboseName) + + a.flags.depsFile = c.String(flagDepsFileName) + if a.flags.depsFile == "" { + a.log("Fallback to default deps file [%s]\n", defaultDepsFile) + a.flags.depsFile = defaultDepsFile + } + + a.flags.depsURL = c.String(flagDepsURLName) a.flags.dryRun = c.Bool(flagDryRun) - a.log("flags %+v", a.flags) + a.log("Flags %+v", a.flags) } func (a *action) log(format string, v ...interface{}) { diff --git a/internal/cli/action_run.go b/internal/cli/action_run.go index ed0a937..92b2d64 100644 --- a/internal/cli/action_run.go +++ b/internal/cli/action_run.go @@ -96,13 +96,13 @@ func (a *action) runGetImportedModules(c *cli.Context) (map[string]struct{}, err if err := json.Unmarshal([]byte(goOutputStr), &importedModules); err != nil { return nil, fmt.Errorf("failed to json unmarshal: %w", err) } - a.log("go output: %s", string(goOutput)) + a.log("Go output: %s", string(goOutput)) mapImportedModules := make(map[string]struct{}) for _, importedModule := range importedModules { mapImportedModules[importedModule.Path] = struct{}{} } - a.log("imported modules: %+v\n", importedModules) + a.log("Imported modules: %+v\n", importedModules) return mapImportedModules, nil } @@ -157,7 +157,7 @@ func (a *action) runUpgradeModule( if modulePath == "" { return successUpgradedModules, nil } - a.log("line: %s", modulePath) + a.log("Line: %s", modulePath) // Check if modulePath is imported module, otherwise skip if _, ok := mapImportedModules[modulePath]; !ok { @@ -171,13 +171,13 @@ func (a *action) runUpgradeModule( if err != nil { return successUpgradedModules, fmt.Errorf("failed to run go %+v: %w", strings.Join(goListArgs, " "), err) } - a.log("go output: %s", string(goOutput)) + a.log("Go output: %s", string(goOutput)) module := Module{} if err := json.Unmarshal(goOutput, &module); err != nil { return successUpgradedModules, fmt.Errorf("failed to json unmarshal: %w", err) } - a.log("module: %+v", module) + a.log("Module: %+v", module) if module.Update == nil { color.PrintAppOK(name, fmt.Sprintf("You already have latest [%s] version [%s]", module.Path, module.Version)) @@ -195,7 +195,7 @@ func (a *action) runUpgradeModule( if err != nil { return successUpgradedModules, fmt.Errorf("failed to run go %+v: %w", strings.Join(goGetArgs, " "), err) } - a.log("go output: %s", string(goOutput)) + a.log("Go output: %s", string(goOutput)) successUpgradedModules = append(successUpgradedModules, module) @@ -215,7 +215,7 @@ func (a *action) runGoMod(c *cli.Context, existVendor bool) error { if err != nil { return fmt.Errorf("failed to run go %+v: %w", strings.Join(goModArgs, " "), err) } - a.log("go output: %s", string(goOutput)) + a.log("Go output: %s", string(goOutput)) if existVendor { // go mod vendor @@ -224,7 +224,7 @@ func (a *action) runGoMod(c *cli.Context, existVendor bool) error { if err != nil { return fmt.Errorf("failed to run go %+v: %w", strings.Join(goModArgs, " "), err) } - a.log("go output: %s", string(goOutput)) + a.log("Go output: %s", string(goOutput)) } return nil @@ -258,7 +258,7 @@ func (a *action) runGitCommit(c *cli.Context, successUpgradedModules []Module, e if err != nil { return fmt.Errorf("failed to run git %+v: %w", strings.Join(gitAddArgs, " "), err) } - a.log("git output: %s", string(gitOutput)) + a.log("Git output: %s", string(gitOutput)) // git commit gitCommitMessage := "build: upgrade modules\n" @@ -270,7 +270,7 @@ func (a *action) runGitCommit(c *cli.Context, successUpgradedModules []Module, e if err != nil { return fmt.Errorf("failed to run git %+v: %w", strings.Join(gitCommitArgs, " "), err) } - a.log("git output: %s", string(gitOutput)) + a.log("Git output: %s", string(gitOutput)) return nil } diff --git a/internal/cli/app.go b/internal/cli/app.go index 94f57e8..39816b3 100644 --- a/internal/cli/app.go +++ b/internal/cli/app.go @@ -11,20 +11,20 @@ const ( name = "update-go-mod" usage = "excatly like the name says" - flagVerbose = "verbose" - flagDepsFile = "deps-file" - flagDepsURL = "deps-url" - flagDryRun = "dry-run" + commandRunName = "run" + commandRunUsage = "run the program" - commandRun = "run" + flagVerboseName = "verbose" + flagVerboseUsage = "show what is going on" - usageCommandRun = "run the program" - usageFlagVerbose = "show what is going on" - usageDepsFile = "file which show what deps need to upgrade" - usageDepsURL = "url which show what deps need to upgrade" - usageDryRun = "demo what would be done" + flagDepsFileName = "deps-file" + flagDepsFileUsage = "file which show what deps need to upgrade" - defaultDepsFile = ".deps" + flagDepsURLName = "deps-url" + flagDepsURLUsage = "url which show what deps need to upgrade" + + flagDryRun = "dry-run" + flagDryRunName = "demo what would be done" ) var aliasFlagVerbose = []string{"v"} @@ -41,26 +41,25 @@ func NewApp() *App { Usage: usage, Commands: []*cli.Command{ { - Name: commandRun, - Usage: usageCommandRun, + Name: commandRunName, + Usage: commandRunUsage, Flags: []cli.Flag{ &cli.BoolFlag{ - Name: flagVerbose, + Name: flagVerboseName, Aliases: aliasFlagVerbose, - Usage: usageFlagVerbose, + Usage: flagVerboseUsage, }, &cli.StringFlag{ - Name: flagDepsFile, - Usage: usageDepsFile, - Value: defaultDepsFile, + Name: flagDepsFileName, + Usage: flagDepsFileUsage, }, &cli.StringFlag{ - Name: flagDepsURL, - Usage: usageDepsURL, + Name: flagDepsURLName, + Usage: flagDepsURLUsage, }, &cli.BoolFlag{ Name: flagDryRun, - Usage: usageDryRun, + Usage: flagDryRunName, }, }, Action: a.Run,