Skip to content

Commit 1fb4527

Browse files
authored
Support to search a tool by the alias name (#223)
* Support to search a tool by the alias name * Fix the wrong alias name parse
1 parent d72756d commit 1fb4527

File tree

17 files changed

+522
-69
lines changed

17 files changed

+522
-69
lines changed

cmd/fetch.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ func newFetchCmd(context.Context) (cmd *cobra.Command) {
2929
}
3030

3131
func (o *fetchOption) preRunE(_ *cobra.Command, _ []string) (err error) {
32+
fetcher := &installer.DefaultFetcher{}
3233
if o.reset {
3334
var configDir string
34-
if configDir, err = installer.GetConfigDir(); err == nil {
35+
if configDir, err = fetcher.GetConfigDir(); err == nil {
3536
if err = os.RemoveAll(configDir); err != nil {
3637
err = fmt.Errorf("failed to remove directory: %s, error %v", configDir, err)
3738
return
@@ -45,7 +46,8 @@ func (o *fetchOption) preRunE(_ *cobra.Command, _ []string) (err error) {
4546
}
4647

4748
func (o *fetchOption) runE(cmd *cobra.Command, _ []string) (err error) {
48-
return installer.FetchLatestRepo(o.Provider, o.branch, cmd.OutOrStdout())
49+
fetcher := &installer.DefaultFetcher{}
50+
return fetcher.FetchLatestRepo(o.Provider, o.branch, cmd.OutOrStdout())
4951
}
5052

5153
type fetchOption struct {

cmd/fetch_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"github.com/stretchr/testify/assert"
6+
"testing"
7+
)
8+
9+
func Test_newFetchCmd(t *testing.T) {
10+
cmd := newFetchCmd(context.Background())
11+
assert.Equal(t, "fetch", cmd.Name())
12+
13+
flags := []struct {
14+
name string
15+
shorthand string
16+
}{{
17+
name: "branch",
18+
shorthand: "b",
19+
}, {
20+
name: "reset",
21+
}}
22+
for i := range flags {
23+
tt := flags[i]
24+
t.Run(tt.name, func(t *testing.T) {
25+
flag := cmd.Flag(tt.name)
26+
assert.NotNil(t, flag)
27+
assert.NotEmpty(t, flag.Usage)
28+
assert.Equal(t, tt.shorthand, flag.Shorthand)
29+
})
30+
}
31+
}

cmd/get.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ func (o *downloadOption) fetch() (err error) {
111111

112112
// fetch the latest config
113113
fmt.Println("start to fetch the config")
114-
if err = installer.FetchLatestRepo(o.Provider, installer.ConfigBranch, sysos.Stdout); err != nil {
114+
fetcher := &installer.DefaultFetcher{}
115+
if err = fetcher.FetchLatestRepo(o.Provider, installer.ConfigBranch, sysos.Stdout); err != nil {
115116
err = fmt.Errorf("unable to fetch the latest config, error: %v", err)
116117
return
117118
}

cmd/get_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"github.com/stretchr/testify/assert"
6+
"testing"
7+
)
8+
9+
func Test_newGetCmd(t *testing.T) {
10+
cmd := newGetCmd(context.Background())
11+
assert.Equal(t, "get", cmd.Name())
12+
13+
flags := []struct {
14+
name string
15+
shorthand string
16+
}{{
17+
name: "output",
18+
shorthand: "o",
19+
}, {
20+
name: "pre",
21+
}, {
22+
name: "time",
23+
}, {
24+
name: "max-attempts",
25+
}, {
26+
name: "show-progress",
27+
}, {
28+
name: "continue-at",
29+
}, {
30+
name: "keep-part",
31+
}, {
32+
name: "os",
33+
}, {
34+
name: "arch",
35+
}, {
36+
name: "print-schema",
37+
}, {
38+
name: "print-version",
39+
}, {
40+
name: "print-categories",
41+
}, {
42+
name: "print-version-count",
43+
}}
44+
for i := range flags {
45+
tt := flags[i]
46+
t.Run(tt.name, func(t *testing.T) {
47+
flag := cmd.Flag(tt.name)
48+
assert.NotNil(t, flag)
49+
assert.NotEmpty(t, flag.Usage)
50+
assert.Equal(t, tt.shorthand, flag.Shorthand)
51+
})
52+
}
53+
}

cmd/install_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"github.com/stretchr/testify/assert"
6+
"testing"
7+
)
8+
9+
func Test_newInstallCmd(t *testing.T) {
10+
cmd := newInstallCmd(context.Background())
11+
assert.Equal(t, "install", cmd.Name())
12+
13+
flags := []struct {
14+
name string
15+
shorthand string
16+
}{{
17+
name: "category",
18+
shorthand: "c",
19+
}, {
20+
name: "show-progress",
21+
}, {
22+
name: "accept-preRelease",
23+
}, {
24+
name: "pre",
25+
}, {
26+
name: "from-source",
27+
}, {
28+
name: "from-branch",
29+
}, {
30+
name: "goget",
31+
}, {
32+
name: "download",
33+
}, {
34+
name: "force",
35+
shorthand: "f",
36+
}, {
37+
name: "clean-package",
38+
}, {
39+
name: "thread",
40+
shorthand: "t",
41+
}, {
42+
name: "keep-part",
43+
}, {
44+
name: "os",
45+
}, {
46+
name: "arch",
47+
}}
48+
for i := range flags {
49+
tt := flags[i]
50+
t.Run(tt.name, func(t *testing.T) {
51+
flag := cmd.Flag(tt.name)
52+
assert.NotNil(t, flag)
53+
assert.NotEmpty(t, flag.Usage)
54+
assert.Equal(t, tt.shorthand, flag.Shorthand)
55+
})
56+
}
57+
}

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func NewRoot(cxt context.Context) (cmd *cobra.Command) {
2323
}
2424

2525
cmd.AddCommand(
26-
newGetCmd(cxt), newInstallCmd(cxt), newFetchCmd(cxt), newSearchCmd(cxt), newTestCmd(), newSetupCommand(),
26+
newGetCmd(cxt), newInstallCmd(cxt), newFetchCmd(cxt), newSearchCmd(cxt), newSetupCommand(),
2727
extver.NewVersionCmd("linuxsuren", "http-downloader", "hd", nil),
2828
extpkg.NewCompletionCmd(cmd))
2929
return

cmd/root_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"github.com/stretchr/testify/assert"
6+
"testing"
7+
)
8+
9+
func TestNewRoot(t *testing.T) {
10+
cmd := NewRoot(context.Background())
11+
assert.Equal(t, "hd", cmd.Name())
12+
}

cmd/search.go

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ import (
88
"github.com/spf13/pflag"
99
"github.com/spf13/viper"
1010
sysos "os"
11-
"path"
12-
"path/filepath"
13-
"strings"
1411
)
1512

1613
func newSearchCmd(context.Context) (cmd *cobra.Command) {
@@ -43,34 +40,25 @@ Thanks to https://github.com/hunshcn/gh-proxy`)
4340
}
4441

4542
func (s *searchOption) runE(_ *cobra.Command, args []string) (err error) {
46-
err = search(args[0])
43+
err = search(args[0], s.Fetch, &installer.DefaultFetcher{})
4744
return
4845
}
4946

50-
func search(keyword string) (err error) {
51-
if err = installer.FetchLatestRepo("", "", sysos.Stdout); err != nil {
52-
return
47+
func search(keyword string, fetch bool, fetcher installer.Fetcher) (err error) {
48+
if fetch {
49+
if err = fetcher.FetchLatestRepo("", "", sysos.Stdout); err != nil {
50+
return
51+
}
5352
}
5453

5554
var configDir string
56-
if configDir, err = installer.GetConfigDir(); err != nil {
55+
if configDir, err = fetcher.GetConfigDir(); err != nil {
5756
return
5857
}
5958

60-
var files []string
61-
if files, err = filepath.Glob(path.Join(configDir, "config/**/*.yml")); err == nil {
62-
for _, metaFile := range files {
63-
ext := path.Ext(metaFile)
64-
fileName := path.Base(metaFile)
65-
org := path.Base(path.Dir(metaFile))
66-
repo := strings.TrimSuffix(fileName, ext)
67-
68-
if !strings.Contains(repo, keyword) {
69-
continue
70-
}
71-
72-
fmt.Println(path.Join(org, repo))
73-
}
59+
result := installer.FindByKeyword(keyword, configDir)
60+
for _, item := range result {
61+
fmt.Println(item)
7462
}
7563
return
7664
}

cmd/search_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package cmd
2+
3+
import (
4+
"context"
5+
"errors"
6+
"github.com/linuxsuren/http-downloader/pkg/installer"
7+
"github.com/stretchr/testify/assert"
8+
"os"
9+
"path"
10+
"testing"
11+
)
12+
13+
func Test_search(t *testing.T) {
14+
err := search("keyword", true, &installer.FakeFetcher{})
15+
assert.Nil(t, err)
16+
17+
// expect an error with GetConfigDir
18+
err = search("", true, &installer.FakeFetcher{GetConfigDirErr: errors.New("fake")})
19+
assert.NotNil(t, err)
20+
21+
// expect an error with FetchLatestRepo
22+
err = search("", true, &installer.FakeFetcher{FetchLatestRepoErr: errors.New("fake")})
23+
assert.NotNil(t, err)
24+
25+
tempDir, err := os.MkdirTemp("", "config")
26+
assert.Nil(t, err)
27+
defer func() {
28+
_ = os.RemoveAll(tempDir)
29+
}()
30+
31+
configDir := path.Join(tempDir, "config")
32+
orgDir := path.Join(configDir, "org")
33+
err = os.MkdirAll(orgDir, 0755)
34+
assert.Nil(t, err)
35+
err = os.WriteFile(path.Join(orgDir, "repo.yml"), []byte("x=x"), os.ModeAppend)
36+
assert.Nil(t, err)
37+
err = os.WriteFile(path.Join(orgDir, "fake.yml"), []byte{}, os.ModeAppend)
38+
assert.Nil(t, err)
39+
40+
err = search("repo", true, &installer.FakeFetcher{ConfigDir: tempDir})
41+
assert.Nil(t, err)
42+
}
43+
44+
func Test_newSearchCmd(t *testing.T) {
45+
cmd := newSearchCmd(context.Background())
46+
assert.Equal(t, "search", cmd.Name())
47+
48+
flags := []struct {
49+
name string
50+
shorthand string
51+
}{{
52+
name: "fetch",
53+
}, {
54+
name: "provider",
55+
}, {
56+
name: "proxy-github",
57+
}}
58+
for i := range flags {
59+
tt := flags[i]
60+
t.Run(tt.name, func(t *testing.T) {
61+
flag := cmd.Flag(tt.name)
62+
assert.NotNil(t, flag)
63+
assert.NotEmpty(t, flag.Usage)
64+
assert.Equal(t, tt.shorthand, flag.Shorthand)
65+
})
66+
}
67+
}

cmd/setup_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package cmd
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func Test_newSetupCommand(t *testing.T) {
9+
cmd := newSetupCommand()
10+
assert.Equal(t, "setup", cmd.Name())
11+
}

0 commit comments

Comments
 (0)