Skip to content
This repository has been archived by the owner on Oct 3, 2022. It is now read-only.

Commit

Permalink
Merge pull request #21 from Akimon658/fix/file-extension
Browse files Browse the repository at this point in the history
Make all subcommand work without `.exe` on Windows
  • Loading branch information
akimon658 authored Jul 6, 2022
2 parents 9ec0d68 + 9e03dbb commit a37fcd8
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 54 deletions.
18 changes: 10 additions & 8 deletions cmd/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down
14 changes: 10 additions & 4 deletions cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}
}

Expand All @@ -178,5 +183,6 @@ func extractUserSpecifyPkg(pkgs []goutil.Package, targets []string) []goutil.Pac
}
}
}

return result
}
16 changes: 16 additions & 0 deletions file/extension.go
Original file line number Diff line number Diff line change
@@ -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
}
44 changes: 44 additions & 0 deletions file/extension_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
})
}
}
42 changes: 0 additions & 42 deletions internal/file/file.go

This file was deleted.

0 comments on commit a37fcd8

Please sign in to comment.