Skip to content

Commit

Permalink
Merge pull request #87 from nao1215/refactor-exclude-option
Browse files Browse the repository at this point in the history
Refactor exclude option
  • Loading branch information
nao1215 authored May 2, 2023
2 parents 4d40b63 + 6ad20f1 commit 491522b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 42 deletions.
2 changes: 1 addition & 1 deletion cmd/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Test_CheckOption(t *testing.T) {
stderr []string
}{
{
name: "paser --jobs argument error",
name: "parser --jobs argument error",
args: args{
cmd: &cobra.Command{},
args: []string{},
Expand Down
8 changes: 4 additions & 4 deletions cmd/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"testing"
Expand All @@ -31,7 +30,7 @@ func Test_ExportOption(t *testing.T) {
stderr []string
}{
{
name: "paser --output argument error",
name: "parser --output argument error",
args: args{
cmd: &cobra.Command{},
args: []string{},
Expand Down Expand Up @@ -105,8 +104,9 @@ func Test_validPkgInfo(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := validPkgInfo(tt.args.pkgs); !reflect.DeepEqual(got, tt.want) {
t.Errorf("validPkgInfo() = %v, want %v", got, tt.want)
got := validPkgInfo(tt.args.pkgs)
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("value is mismatch (-want +got):\n%s", diff)
}
})
}
Expand Down
26 changes: 12 additions & 14 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os/signal"
"runtime"
"strconv"
"strings"
"syscall"
"time"

Expand All @@ -33,7 +32,7 @@ under $GOPATH/bin and automatically updates commands to the latest version.`,
}
cmd.Flags().BoolP("dry-run", "n", false, "perform the trial update with no changes")
cmd.Flags().BoolP("notify", "N", false, "enable desktop notifications")
cmd.Flags().StringP("exclude", "e", "", "specify binaries which should not be updated separated using ',' without spaces as a delimiter")
cmd.Flags().StringSliceP("exclude", "e", []string{}, "specify binaries which should not be updated separated using ',' without spaces as a delimiter")
cmd.Flags().IntP("jobs", "j", runtime.NumCPU(), "Specify the number of CPU cores to use")

return cmd
Expand Down Expand Up @@ -71,33 +70,32 @@ func gup(cmd *cobra.Command, args []string) int {
return 1
}

exclude, err := cmd.Flags().GetString("exclude")
excludePkgList, err := cmd.Flags().GetStringSlice("exclude")
if err != nil {
print.Err(fmt.Errorf("%s: %w", "can not parse command line argument (--exclude)", err))
return 1
}

pkgs = extractUserSpecifyPkg(pkgs, args)

pkgs = excludePkgs(exclude, pkgs)
pkgs = excludePkgs(excludePkgList, pkgs)

if len(pkgs) == 0 {
print.Err("unable to update package: no package information")
print.Err("unable to update package: no package information or no package under $GOBIN")
return 1
}
return update(pkgs, dryRun, notify, cpus)
}

func excludePkgs(exclude string, pkgs []goutil.Package) []goutil.Package {
excludelist := strings.Split(exclude, ",")

for i := len(pkgs) - 1; i >= 0; i-- {
if slice.Contains(excludelist, pkgs[i].Name) {
pkgs = append(pkgs[:i], pkgs[i+1:]...)
func excludePkgs(excludePkgList []string, pkgs []goutil.Package) []goutil.Package {
packageList := []goutil.Package{}
for _, v := range pkgs {
if slice.Contains(excludePkgList, v.Name) {
print.Info(fmt.Sprintf("Exclude '%s' from the update target", v.Name))
continue
}
packageList = append(packageList, v)
}

return pkgs
return packageList
}

type updateResult struct {
Expand Down
46 changes: 23 additions & 23 deletions cmd/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"io"
"os"
"reflect"
"runtime"
"strings"
"testing"
Expand All @@ -27,7 +26,7 @@ func Test_gup(t *testing.T) {
stderr []string
}{
{
name: "paser --dry-run argument error",
name: "parser --dry-run argument error",
args: args{
cmd: &cobra.Command{},
args: []string{},
Expand All @@ -39,7 +38,7 @@ func Test_gup(t *testing.T) {
},
},
{
name: "paser --notify argument error",
name: "parser --notify argument error",
args: args{
cmd: &cobra.Command{},
args: []string{},
Expand All @@ -51,7 +50,7 @@ func Test_gup(t *testing.T) {
},
},
{
name: "paser --jobs argument error",
name: "parser --jobs argument error",
args: args{
cmd: &cobra.Command{},
args: []string{},
Expand All @@ -65,13 +64,13 @@ func Test_gup(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.name == "paser --dry-run argument error" {
if tt.name == "parser --dry-run argument error" {
tt.args.cmd.Flags().BoolP("notify", "N", false, "enable desktop notifications")
tt.args.cmd.Flags().BoolP("jobs", "j", false, "Specify the number of CPU cores to use")
} else if tt.name == "paser --notify argument error" {
} else if tt.name == "parser --notify argument error" {
tt.args.cmd.Flags().BoolP("dry-run", "n", false, "perform the trial update with no changes")
tt.args.cmd.Flags().BoolP("jobs", "j", false, "Specify the number of CPU cores to use")
} else if tt.name == "paser --jobs argument error" {
} else if tt.name == "parser --jobs argument error" {
tt.args.cmd.Flags().BoolP("dry-run", "n", false, "perform the trial update with no changes")
tt.args.cmd.Flags().BoolP("notify", "N", false, "enable desktop notifications")
}
Expand Down Expand Up @@ -165,17 +164,18 @@ func Test_extractUserSpecifyPkg(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := extractUserSpecifyPkg(tt.args.pkgs, tt.args.targets); !reflect.DeepEqual(got, tt.want) {
t.Errorf("extractUserSpecifyPkg() = %v, want %v", got, tt.want)
got := extractUserSpecifyPkg(tt.args.pkgs, tt.args.targets)
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("value is mismatch (-want +got):\n%s", diff)
}
})
}
}

func Test_excludeUserSpecifiedPkg(t *testing.T) {
type args struct {
pkgs []goutil.Package
exclude string
pkgs []goutil.Package
excludePkgList []string
}
tests := []struct {
name string
Expand All @@ -196,7 +196,7 @@ func Test_excludeUserSpecifiedPkg(t *testing.T) {
Name: "pkg3",
},
},
exclude: "pkg1,pkg3",
excludePkgList: []string{"pkg1", "pkg3"},
},
want: []goutil.Package{
{
Expand All @@ -205,7 +205,7 @@ func Test_excludeUserSpecifiedPkg(t *testing.T) {
},
},
{
name: "find user specify package",
name: "find user specify package (exclude all package)",
args: args{
pkgs: []goutil.Package{
{
Expand All @@ -218,16 +218,12 @@ func Test_excludeUserSpecifiedPkg(t *testing.T) {
Name: "pkg3",
},
},
exclude: "pkg1,pkg2",
},
want: []goutil.Package{
{
Name: "pkg3",
},
excludePkgList: []string{"pkg1", "pkg2", "pkg3"},
},
want: []goutil.Package{},
},
{
name: "find user specify package",
name: "If the excluded package does not exist",
args: args{
pkgs: []goutil.Package{
{
Expand All @@ -240,9 +236,12 @@ func Test_excludeUserSpecifiedPkg(t *testing.T) {
Name: "pkg3",
},
},
exclude: "pkg1",
excludePkgList: []string{"pkg4"},
},
want: []goutil.Package{
{
Name: "pkg1",
},
{
Name: "pkg2",
},
Expand All @@ -254,8 +253,9 @@ func Test_excludeUserSpecifiedPkg(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := excludePkgs(tt.args.exclude, tt.args.pkgs); !reflect.DeepEqual(got, tt.want) {
t.Errorf("extractUserSpecifyPkg() = %v, want %v", got, tt.want)
got := excludePkgs(tt.args.excludePkgList, tt.args.pkgs)
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("value is mismatch (-want +got):\n%s", diff)
}
})
}
Expand Down

0 comments on commit 491522b

Please sign in to comment.