Skip to content

Commit d669e7f

Browse files
authored
feat: support to search native packages (#353)
1 parent ab09413 commit d669e7f

File tree

7 files changed

+70
-27
lines changed

7 files changed

+70
-27
lines changed

cmd/get.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"bytes"
55
"context"
66
"fmt"
7-
"github.com/linuxsuren/http-downloader/pkg/common"
8-
"github.com/linuxsuren/http-downloader/pkg/log"
97
"io"
108
"io/fs"
119
"net/http"
@@ -16,6 +14,9 @@ import (
1614
"strings"
1715
"sync"
1816

17+
"github.com/linuxsuren/http-downloader/pkg/common"
18+
"github.com/linuxsuren/http-downloader/pkg/log"
19+
1920
"github.com/AlecAivazis/survey/v2"
2021
"github.com/linuxsuren/http-downloader/pkg/exec"
2122
"golang.org/x/net/html"
@@ -25,6 +26,7 @@ import (
2526
"github.com/linuxsuren/http-downloader/pkg/installer"
2627
"github.com/linuxsuren/http-downloader/pkg/net"
2728
"github.com/spf13/cobra"
29+
"github.com/spf13/pflag"
2830
"github.com/spf13/viper"
2931
"gopkg.in/yaml.v3"
3032
)
@@ -43,6 +45,7 @@ func newGetCmd(ctx context.Context) (cmd *cobra.Command) {
4345
// set flags
4446
flags := cmd.Flags()
4547
opt.addFlags(flags)
48+
opt.addPlatformFlags(flags)
4649
flags.StringVarP(&opt.Output, "output", "o", "", "Write output to <file> instead of stdout.")
4750
flags.BoolVarP(&opt.AcceptPreRelease, "accept-preRelease", "", false,
4851
"If you accept preRelease as the binary asset from GitHub")
@@ -63,8 +66,6 @@ func newGetCmd(ctx context.Context) (cmd *cobra.Command) {
6366
`Download file with multi-threads. It only works when its value is bigger than 1`)
6467
flags.BoolVarP(&opt.KeepPart, "keep-part", "", false,
6568
"If you want to keep the part files instead of deleting them")
66-
flags.StringVarP(&opt.OS, "os", "", runtime.GOOS, "The OS of target binary file")
67-
flags.StringVarP(&opt.Arch, "arch", "", runtime.GOARCH, "The arch of target binary file")
6869
flags.BoolVarP(&opt.PrintSchema, "print-schema", "", false,
6970
"Print the schema of HDConfig if the flag is true without other function")
7071
flags.BoolVarP(&opt.PrintVersion, "print-version", "", false,
@@ -226,6 +227,11 @@ func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error)
226227
return
227228
}
228229

230+
func (o *downloadOption) addPlatformFlags(flags *pflag.FlagSet) {
231+
flags.StringVarP(&o.OS, "os", "", runtime.GOOS, "The OS of target binary file")
232+
flags.StringVarP(&o.Arch, "arch", "", runtime.GOARCH, "The arch of target binary file")
233+
}
234+
229235
func findAnchor(n *html.Node) (items []string) {
230236
if n.Type == html.ElementNode && n.Data == "a" {
231237
for _, a := range n.Attr {
@@ -295,8 +301,8 @@ func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
295301

296302
targetURL := o.URL
297303
if o.ProxyGitHub != "" {
298-
targetURL = strings.Replace(targetURL, "github.com", fmt.Sprintf("%s/github.com", o.ProxyGitHub), 1)
299-
targetURL = strings.Replace(targetURL, "raw.githubusercontent.com", fmt.Sprintf("%s/https://raw.githubusercontent.com", o.ProxyGitHub), 1)
304+
targetURL = strings.Replace(targetURL, "https://github.com", fmt.Sprintf("https://%s/github.com", o.ProxyGitHub), 1)
305+
targetURL = strings.Replace(targetURL, "https://raw.githubusercontent.com", fmt.Sprintf("https://%s/https://raw.githubusercontent.com", o.ProxyGitHub), 1)
300306
}
301307
logger.Printf("start to download from %s\n", targetURL)
302308
var suggestedFilenameAware net.SuggestedFilenameAware

cmd/install.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@ package cmd
33
import (
44
"context"
55
"fmt"
6+
"log"
7+
sysos "os"
8+
"path"
9+
"strings"
10+
611
"github.com/AlecAivazis/survey/v2"
712
"github.com/linuxsuren/http-downloader/pkg/common"
813
"github.com/linuxsuren/http-downloader/pkg/exec"
@@ -11,11 +16,6 @@ import (
1116
"github.com/linuxsuren/http-downloader/pkg/version"
1217
"github.com/spf13/cobra"
1318
"github.com/spf13/viper"
14-
"log"
15-
sysos "os"
16-
"path"
17-
"runtime"
18-
"strings"
1919
)
2020

2121
// newInstallCmd returns the install command
@@ -37,6 +37,7 @@ Cannot find your desired package? Please run command: hd fetch --reset, then try
3737

3838
flags := cmd.Flags()
3939
opt.addFlags(flags)
40+
opt.addPlatformFlags(flags)
4041
flags.StringVarP(&opt.Category, "category", "c", "",
4142
"The category of the potentials packages")
4243
flags.BoolVarP(&opt.ShowProgress, "show-progress", "", true, "If show the progress of download")
@@ -63,8 +64,6 @@ Cannot find your desired package? Please run command: hd fetch --reset, then try
6364
flags.BoolVarP(&opt.NoProxy, "no-proxy", "", viper.GetBool("no-proxy"), "Indicate no HTTP proxy taken")
6465
flags.BoolVarP(&opt.KeepPart, "keep-part", "", false,
6566
"If you want to keep the part files instead of deleting them")
66-
flags.StringVarP(&opt.OS, "os", "", runtime.GOOS, "The OS of target binary file")
67-
flags.StringVarP(&opt.Arch, "arch", "", runtime.GOARCH, "The arch of target binary file")
6867

6968
_ = cmd.RegisterFlagCompletionFunc("provider", ArrayCompletion(ProviderGitHub, ProviderGitee))
7069
return
@@ -151,8 +150,8 @@ func (o *installOption) install(cmd *cobra.Command, args []string) (err error) {
151150
var proxy map[string]string
152151
if o.ProxyGitHub != "" {
153152
proxy = map[string]string{
154-
"raw.githubusercontent.com": fmt.Sprintf("%s/https://raw.githubusercontent.com", o.ProxyGitHub),
155-
"github.com": fmt.Sprintf("%s/https://github.com", o.ProxyGitHub),
153+
"https://raw.githubusercontent.com": fmt.Sprintf("https://%s/https://raw.githubusercontent.com", o.ProxyGitHub),
154+
"https://github.com": fmt.Sprintf("https://%s/https://github.com", o.ProxyGitHub),
156155
}
157156
}
158157
err = os.InstallWithProxy(args[0], proxy)

cmd/search.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package cmd
33
import (
44
"context"
55
"fmt"
6+
"io"
7+
68
"github.com/linuxsuren/http-downloader/pkg/installer"
79
"github.com/spf13/cobra"
810
"github.com/spf13/pflag"
911
"github.com/spf13/viper"
10-
sysos "os"
1112
)
1213

1314
func newSearchCmd(context.Context) (cmd *cobra.Command) {
@@ -43,14 +44,14 @@ Available proxy: gh.api.99988866.xyz, ghproxy.com
4344
Thanks to https://github.com/hunshcn/gh-proxy`)
4445
}
4546

46-
func (s *searchOption) runE(_ *cobra.Command, args []string) (err error) {
47-
err = search(args[0], s.Fetch, s.fetcher)
47+
func (s *searchOption) runE(c *cobra.Command, args []string) (err error) {
48+
err = search(args[0], s.Fetch, s.fetcher, c.OutOrStdout())
4849
return
4950
}
5051

51-
func search(keyword string, fetch bool, fetcher installer.Fetcher) (err error) {
52+
func search(keyword string, fetch bool, fetcher installer.Fetcher, writer io.Writer) (err error) {
5253
if fetch {
53-
if err = fetcher.FetchLatestRepo("", "", sysos.Stdout); err != nil {
54+
if err = fetcher.FetchLatestRepo("", "", writer); err != nil {
5455
return
5556
}
5657
}
@@ -62,7 +63,7 @@ func search(keyword string, fetch bool, fetcher installer.Fetcher) (err error) {
6263

6364
result := installer.FindByKeyword(keyword, configDir)
6465
for _, item := range result {
65-
fmt.Println(item)
66+
fmt.Fprintln(writer, item)
6667
}
6768
return
6869
}

cmd/search_test.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
package cmd
22

33
import (
4+
"bytes"
45
"context"
56
"errors"
6-
"github.com/linuxsuren/http-downloader/pkg/installer"
7-
"github.com/stretchr/testify/assert"
87
"os"
98
"path"
109
"testing"
10+
11+
"github.com/linuxsuren/http-downloader/pkg/installer"
12+
"github.com/stretchr/testify/assert"
1113
)
1214

1315
func Test_search(t *testing.T) {
14-
err := search("keyword", true, &installer.FakeFetcher{})
16+
buf := &bytes.Buffer{}
17+
err := search("keyword", true, &installer.FakeFetcher{}, buf)
1518
assert.Nil(t, err)
19+
assert.Empty(t, buf.String())
1620

1721
// expect an error with GetConfigDir
18-
err = search("", true, &installer.FakeFetcher{GetConfigDirErr: errors.New("fake")})
22+
err = search("", true, &installer.FakeFetcher{GetConfigDirErr: errors.New("fake")}, buf)
1923
assert.NotNil(t, err)
2024

2125
// expect an error with FetchLatestRepo
22-
err = search("", true, &installer.FakeFetcher{FetchLatestRepoErr: errors.New("fake")})
26+
err = search("", true, &installer.FakeFetcher{FetchLatestRepoErr: errors.New("fake")}, buf)
2327
assert.NotNil(t, err)
2428

2529
tempDir, err := os.MkdirTemp("", "config")
@@ -37,7 +41,7 @@ func Test_search(t *testing.T) {
3741
err = os.WriteFile(path.Join(orgDir, "fake.yml"), []byte{}, os.ModeAppend)
3842
assert.Nil(t, err)
3943

40-
err = search("repo", true, &installer.FakeFetcher{ConfigDir: tempDir})
44+
err = search("repo", true, &installer.FakeFetcher{ConfigDir: tempDir}, buf)
4145
assert.Nil(t, err)
4246
}
4347

pkg/installer/check.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,9 @@ func FindByKeyword(keyword, configDir string) (result []string) {
439439
result = append(result, path.Join(org, repo))
440440
}
441441
}
442+
443+
// find in the generic packages
444+
result = append(result, os.SearchPackages(keyword)...)
442445
return
443446
}
444447

pkg/os/installer.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"path"
66
"path/filepath"
7+
"strings"
78

89
"github.com/linuxsuren/http-downloader/pkg/exec"
910
"github.com/linuxsuren/http-downloader/pkg/os/apt"
@@ -69,6 +70,26 @@ func HasPackage(name string) bool {
6970
return false
7071
}
7172

73+
// SearchPackages searches the packages by keyword
74+
func SearchPackages(keyword string) (pkgs []string) {
75+
for key, pms := range defaultInstallerRegistry.installerMap {
76+
if strings.Contains(key, keyword) {
77+
var available bool
78+
for _, pm := range pms {
79+
if pm.Available() {
80+
available = true
81+
break
82+
}
83+
}
84+
85+
if available {
86+
pkgs = append(pkgs, key)
87+
}
88+
}
89+
}
90+
return
91+
}
92+
7293
// InstallWithProxy installs a package with name
7394
func InstallWithProxy(name string, proxy map[string]string) (err error) {
7495
var installer core.Installer

pkg/os/installer_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,12 @@ func TestWithFakeInstaller(t *testing.T) {
7777
err = Uninstall("fake-with-err")
7878
assert.NotNil(t, err)
7979
}
80+
81+
func TestSearchPackages(t *testing.T) {
82+
// currently, this function only support Linux
83+
if runtime.GOOS != "linux" {
84+
return
85+
}
86+
pkgs := SearchPackages("vim")
87+
assert.NotEmpty(t, pkgs)
88+
}

0 commit comments

Comments
 (0)