diff --git a/Changelog.md b/Changelog.md index 71b9815..44992c7 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,3 +2,7 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + +# [0.3.0] - 2022-02-22 +## Added +- --dry-run option. \ No newline at end of file diff --git a/cmd/import.go b/cmd/import.go index 63de7f0..43ee9ae 100644 --- a/cmd/import.go +++ b/cmd/import.go @@ -13,16 +13,22 @@ import ( var importCmd = &cobra.Command{ Use: "import", Run: func(cmd *cobra.Command, args []string) { - os.Exit(runImport()) + os.Exit(runImport(cmd, args)) }, Short: "Install command according to gup.conf.", } func init() { + importCmd.Flags().BoolP("dry-run", "d", false, "perform the trial update with no changes") rootCmd.AddCommand(importCmd) } -func runImport() int { +func runImport(cmd *cobra.Command, args []string) int { + dryRun, err := cmd.Flags().GetBool("dry-run") + if err != nil { + print.Fatal(fmt.Errorf("%s: %w", "can not parse command line argument", err)) + } + if !file.IsFile(config.FilePath()) { print.Fatal(fmt.Errorf("%s is not found", config.FilePath())) } @@ -36,7 +42,7 @@ func runImport() int { print.Fatal("unable to update package: no package information") } - pkgs, result := update(pkgs) + pkgs, result := update(pkgs, dryRun) for k, v := range result { if v == "Failure" { print.Err(fmt.Errorf("update failure: %s ", k)) diff --git a/cmd/root.go b/cmd/root.go index ac591e0..2608a44 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -20,10 +20,14 @@ If you execute '$ gup', gup gets the package path of all commands under $GOPATH/bin and automatically updates commans to the latest version.`, Run: func(cmd *cobra.Command, args []string) { - os.Exit(gup(args)) + os.Exit(gup(cmd, args)) }, } +func init() { + rootCmd.Flags().BoolP("dry-run", "d", false, "perform the trial update with no changes") +} + // Execute run gup process. func Execute() { if err := rootCmd.Execute(); err != nil { @@ -33,7 +37,12 @@ func Execute() { // gup is main sequence. // All errors are handled in this function. -func gup(args []string) int { +func gup(cmd *cobra.Command, args []string) int { + dryRun, err := cmd.Flags().GetBool("dry-run") + if err != nil { + print.Fatal(fmt.Errorf("%s: %w", "can not parse command line argument", err)) + } + if err := goutil.CanUseGoCmd(); err != nil { print.Fatal(fmt.Errorf("%s: %w", "you didn't install golang", err)) } @@ -47,7 +56,7 @@ func gup(args []string) int { print.Fatal("unable to update package: no package information") } - pkgs, result := update(pkgs) + pkgs, result := update(pkgs, dryRun) for k, v := range result { if v == "Failure" { print.Err(fmt.Errorf("update failure: %s ", k)) @@ -64,20 +73,22 @@ func gup(args []string) int { return 0 } -func update(pkgs []goutil.Package) ([]goutil.Package, map[string]string) { +func update(pkgs []goutil.Package, dryRun bool) ([]goutil.Package, map[string]string) { tmp := []goutil.Package{} result := map[string]string{} bar := pb.Simple.Start(len(pkgs)) bar.SetMaxWidth(80) for _, v := range pkgs { bar.Increment() - if v.ImportPath == "" { - result[v.ImportPath] = "Failure" - continue - } - if err := goutil.Install(v.ImportPath); err != nil { - result[v.ImportPath] = "Failure" - continue + if !dryRun { + if v.ImportPath == "" { + result[v.ImportPath] = "Failure" + continue + } + if err := goutil.Install(v.ImportPath); err != nil { + result[v.ImportPath] = "Failure" + continue + } } tmp = append(tmp, goutil.Package{Name: v.Name, ImportPath: v.ImportPath}) result[v.ImportPath] = "Success" diff --git a/internal/cmdinfo/cmdinfo.go b/internal/cmdinfo/cmdinfo.go index 06cdae8..8befe0e 100644 --- a/internal/cmdinfo/cmdinfo.go +++ b/internal/cmdinfo/cmdinfo.go @@ -6,7 +6,7 @@ import ( const ( name = "gup" - version = "0.2.1" + version = "0.3.0" ) // Version return gup command version.