diff --git a/cmd/remove.go b/cmd/remove.go index d7df9e3..91fb69e 100644 --- a/cmd/remove.go +++ b/cmd/remove.go @@ -4,12 +4,10 @@ import ( "fmt" "os" "path/filepath" - "runtime" - "strings" "github.com/spf13/cobra" - "github.com/Akimon658/gup/internal/file" + "github.com/Akimon658/gup/file" "github.com/Akimon658/gup/internal/goutil" "github.com/Akimon658/gup/internal/print" ) @@ -44,14 +42,18 @@ func remove(args []string, force bool) int { code := 0 for _, v := range args { - if runtime.GOOS == "windows" && !strings.HasSuffix(v, ".exe") { - v += ".exe" - } + v = file.Extension(v) target := filepath.Join(gobin, v) + stat, err := os.Stat(target) + if err != nil { + print.Err(err) + code = 1 + continue + } - if !file.IsFile(target) { - print.Err(fmt.Errorf("no such file or directory: %s", target)) + if stat.IsDir() { + print.Err(fmt.Errorf("no such file: %s", target)) code = 1 continue } diff --git a/cmd/update.go b/cmd/update.go index 7c39303..f8b5b4c 100644 --- a/cmd/update.go +++ b/cmd/update.go @@ -10,6 +10,7 @@ import ( "github.com/spf13/cobra" + "github.com/Akimon658/gup/file" "github.com/Akimon658/gup/internal/goutil" "github.com/Akimon658/gup/internal/print" "github.com/Akimon658/gup/internal/slice" @@ -164,10 +165,14 @@ func extractUserSpecifyPkg(pkgs []goutil.Package, targets []string) []goutil.Pac if len(targets) == 0 { return pkgs } - for _, v := range pkgs { - if slice.Contains(targets, v.Name) { - result = append(result, v) - tmp = append(tmp, v.Name) + + for _, v := range targets { + v = file.Extension(v) + for _, w := range pkgs { + if v == w.Name { + result = append(result, w) + tmp = append(tmp, w.Name) + } } } @@ -178,5 +183,6 @@ func extractUserSpecifyPkg(pkgs []goutil.Package, targets []string) []goutil.Pac } } } + return result } diff --git a/file/extension.go b/file/extension.go new file mode 100644 index 0000000..ea2d253 --- /dev/null +++ b/file/extension.go @@ -0,0 +1,16 @@ +package file + +import ( + "runtime" + "strings" +) + +const extWin = ".exe" + +func Extension(name string) string { + if runtime.GOOS == "windows" && !strings.HasSuffix(name, extWin) { + name += extWin + } + + return name +} diff --git a/file/extension_test.go b/file/extension_test.go new file mode 100644 index 0000000..2d0e44c --- /dev/null +++ b/file/extension_test.go @@ -0,0 +1,44 @@ +package file + +import ( + "runtime" + "testing" + + "github.com/jaswdr/faker" +) + +func TestExtension(t *testing.T) { + fileName := faker.New().Lorem().Word() + fileNameWithExtension := fileName + extWin + testCases := []struct { + name string + input string + expected string + }{ + { + name: "without extension", + input: fileName, + expected: fileNameWithExtension, + }, + { + name: "with extension", + input: fileNameWithExtension, + expected: fileNameWithExtension, + }, + } + + for _, v := range testCases { + t.Run(v.name, func(t *testing.T) { + result := Extension(v.input) + if runtime.GOOS == "windows" { + if result != v.expected { + t.Errorf("expected %s, got %s", v.expected, result) + } + } else { + if result != v.input { + t.Errorf("expected %s, got %s", v.input, result) + } + } + }) + } +} diff --git a/internal/file/file.go b/internal/file/file.go deleted file mode 100644 index d079e8b..0000000 --- a/internal/file/file.go +++ /dev/null @@ -1,42 +0,0 @@ -package file - -import ( - "bufio" - "io" - "os" -) - -// ReadFileToList convert file content to string list. -func ReadFileToList(path string) ([]string, error) { - var strList []string - f, err := os.Open(path) - if err != nil { - return nil, err - } - defer f.Close() - - r := bufio.NewReader(f) - for { - line, err := r.ReadString('\n') - if err != nil && err != io.EOF { - return nil, err - } - if err == io.EOF && len(line) == 0 { - break - } - strList = append(strList, line) - } - return strList, nil -} - -// IsFile reports whether the path exists and is a file. -func IsFile(path string) bool { - stat, err := os.Stat(path) - return (err == nil) && (!stat.IsDir()) -} - -// IsDir reports whether the path exists and is a directory. -func IsDir(path string) bool { - stat, err := os.Stat(path) - return (err == nil) && (stat.IsDir()) -}