Skip to content

Commit

Permalink
Merge pull request #5 from nao1215/dry-run
Browse files Browse the repository at this point in the history
Add --dry-run option.
  • Loading branch information
nao1215 authored Feb 22, 2022
2 parents 2a31af4 + 360dc5f commit 0a9ab23
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 15 deletions.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
12 changes: 9 additions & 3 deletions cmd/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
Expand All @@ -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))
Expand Down
33 changes: 22 additions & 11 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
}
Expand All @@ -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))
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion internal/cmdinfo/cmdinfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

const (
name = "gup"
version = "0.2.1"
version = "0.3.0"
)

// Version return gup command version.
Expand Down

0 comments on commit 0a9ab23

Please sign in to comment.