Skip to content

Commit 4881378

Browse files
authored
Fix the install with category name when no config found (#192)
* Fix the install with category name when no config found * Add codecov config file
1 parent eaf4ad7 commit 4881378

File tree

6 files changed

+96
-20
lines changed

6 files changed

+96
-20
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,16 @@ hd get --pre ks
4242
## Install
4343
You can also install a package from GitHub:
4444

45-
```
45+
```shell
4646
hd install jenkins-zh/jenkins-cli/jcli -t 6
4747
```
4848

49+
or install by a category name:
50+
51+
```shell
52+
hd install --category security
53+
```
54+
4955
## Search
5056
hd can download or install via the format of `$org/$repo`. If you find that it's not working. It might because of there's
5157
no record in [hd-home](https://github.com/LinuxSuRen/hd-home). You're welcome to help us to maintain it.

cmd/get.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"gopkg.in/yaml.v2"
1111
"net/http"
1212
"net/url"
13+
sysos "os"
1314
"path"
1415
"runtime"
1516
"strings"
@@ -53,6 +54,8 @@ func newGetCmd(ctx context.Context) (cmd *cobra.Command) {
5354
"Print the schema of HDConfig if the flag is true without other function")
5455
flags.BoolVarP(&opt.PrintVersion, "print-version", "", false,
5556
"Print the version list")
57+
flags.BoolVarP(&opt.PrintCategories, "print-categories", "", false,
58+
"Print the category list")
5659
flags.IntVarP(&opt.PrintVersionCount, "print-version-count", "", 20,
5760
"The number of the version list")
5861

@@ -84,6 +87,7 @@ type downloadOption struct {
8487
PrintSchema bool
8588
PrintVersion bool
8689
PrintVersionCount int
90+
PrintCategories bool
8791

8892
// inner fields
8993
name string
@@ -100,12 +104,35 @@ const (
100104
ProviderGitee = "gitee"
101105
)
102106

107+
func (o *downloadOption) fetch() (err error) {
108+
if !o.Fetch {
109+
return
110+
}
111+
112+
// fetch the latest config
113+
fmt.Println("start to fetch the config")
114+
if err = installer.FetchLatestRepo(o.Provider, installer.ConfigBranch, sysos.Stdout); err != nil {
115+
err = fmt.Errorf("unable to fetch the latest config, error: %v", err)
116+
return
117+
}
118+
o.Fetch = false
119+
return
120+
}
121+
103122
func (o *downloadOption) preRunE(cmd *cobra.Command, args []string) (err error) {
104123
// this might not be the best way to print schema
105124
if o.PrintSchema {
106125
return
107126
}
108127

128+
if err = o.fetch(); err != nil {
129+
return
130+
}
131+
132+
if o.PrintCategories {
133+
return
134+
}
135+
109136
o.Tar = true
110137
if len(args) <= 0 {
111138
return fmt.Errorf("no URL provided")
@@ -177,6 +204,11 @@ func (o *downloadOption) runE(cmd *cobra.Command, args []string) (err error) {
177204
return
178205
}
179206

207+
if o.PrintCategories {
208+
cmd.Println(installer.FindCategories())
209+
return
210+
}
211+
180212
cmd.Printf("start to download from %s\n", o.URL)
181213
if o.Thread <= 1 {
182214
err = pkg.DownloadWithContinue(o.URL, o.Output, o.ContinueAt, -1, 0, o.ShowProgress)

cmd/install.go

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Cannot find your desired package? Please run command: hd fetch --reset, then try
3535

3636
flags := cmd.Flags()
3737
opt.addFlags(flags)
38-
flags.StringVarP(&opt.Category, "category", "", "",
38+
flags.StringVarP(&opt.Category, "category", "c", "",
3939
"The category of the potentials packages")
4040
flags.BoolVarP(&opt.ShowProgress, "show-progress", "", true, "If show the progress of download")
4141
flags.BoolVarP(&opt.AcceptPreRelease, "accept-preRelease", "", false,
@@ -100,14 +100,20 @@ func (o *installOption) preRunE(cmd *cobra.Command, args []string) (err error) {
100100

101101
// try to find if it's a native package
102102
o.nativePackage = os.HasPackage(o.tool)
103-
if !o.nativePackage && o.Category == "" {
104-
err = o.downloadOption.preRunE(cmd, args)
105-
106-
// try to find the real tool name
107-
if o.downloadOption.Package.TargetBinary != "" {
108-
o.tool = o.downloadOption.Package.TargetBinary
109-
} else if o.downloadOption.Package.Binary != "" {
110-
o.tool = o.downloadOption.Package.Binary
103+
if !o.nativePackage {
104+
if o.Category == "" {
105+
err = o.downloadOption.preRunE(cmd, args)
106+
107+
// try to find the real tool name
108+
if o.downloadOption.Package.TargetBinary != "" {
109+
o.tool = o.downloadOption.Package.TargetBinary
110+
} else if o.downloadOption.Package.Binary != "" {
111+
o.tool = o.downloadOption.Package.Binary
112+
} else {
113+
o.tool = o.downloadOption.Package.Repo
114+
}
115+
} else {
116+
err = o.downloadOption.fetch()
111117
}
112118
}
113119
return
@@ -195,7 +201,6 @@ func (o *installOption) runE(cmd *cobra.Command, args []string) (err error) {
195201
}
196202

197203
for _, item := range choose {
198-
o.tool = item
199204
if err = o.downloadOption.preRunE(cmd, []string{item}); err != nil {
200205
return
201206
}
@@ -205,6 +210,8 @@ func (o *installOption) runE(cmd *cobra.Command, args []string) (err error) {
205210
o.tool = o.downloadOption.Package.TargetBinary
206211
} else if o.downloadOption.Package.Binary != "" {
207212
o.tool = o.downloadOption.Package.Binary
213+
} else {
214+
o.tool = o.downloadOption.Package.Repo
208215
}
209216

210217
if err = o.install(cmd, []string{item}); err != nil {

codecov.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# https://docs.codecov.com/docs/commit-status
2+
# it's hard to have a coverage for some code lines
3+
coverage:
4+
status:
5+
project:
6+
default:
7+
target: auto
8+
threshold: 0.1%
9+
patch:
10+
default:
11+
target: auto
12+
threshold: 0%

pkg/compress/bzip2.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func (x *Bzip2) ExtractFiles(sourceFile, targetName string) (err error) {
4141

4242
var targetFile *os.File
4343
if targetFile, err = os.OpenFile(fmt.Sprintf("%s/%s", filepath.Dir(sourceFile), targetName),
44-
os.O_CREATE|os.O_RDWR, 0744); err != nil {
44+
os.O_CREATE|os.O_RDWR, 0600); err != nil {
4545
return
4646
}
4747
if _, err = io.Copy(targetFile, r); err != nil {

pkg/installer/check.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,14 +141,6 @@ func (o *Installer) GetVersion(path string) (version string, err error) {
141141
// ProviderURLParse parse the URL
142142
func (o *Installer) ProviderURLParse(path string, acceptPreRelease bool) (packageURL string, err error) {
143143
packageURL = path
144-
if o.Fetch {
145-
// fetch the latest config
146-
fmt.Println("start to fetch the config")
147-
if err = FetchLatestRepo(o.Provider, ConfigBranch, sysos.Stdout); err != nil {
148-
err = fmt.Errorf("unable to fetch the latest config, error: %v", err)
149-
return
150-
}
151-
}
152144

153145
version, err := o.GetVersion(packageURL)
154146
if err != nil {
@@ -185,6 +177,8 @@ func (o *Installer) ProviderURLParse(path string, acceptPreRelease bool) (packag
185177
AdditionBinaries: cfg.AdditionBinaries,
186178
VersionNum: strings.TrimPrefix(version, "v"),
187179
}
180+
cfg.Org = o.Org
181+
cfg.Repo = o.Repo
188182
o.Package = &cfg
189183
o.AdditionBinaries = cfg.AdditionBinaries
190184
o.Tar = cfg.Tar != "false"
@@ -394,6 +388,31 @@ func hasPackageSuffix(packageURL string) bool {
394388
return compress.IsSupport(path.Ext(packageURL))
395389
}
396390

391+
// FindCategories returns the whole supported categories
392+
func FindCategories() (result []string) {
393+
categories := make(map[string]string, 0)
394+
configDir := sysos.ExpandEnv("$HOME/.config/hd-home/config/")
395+
_ = filepath.Walk(configDir, func(basepath string, info fs.FileInfo, err error) error {
396+
if !strings.HasSuffix(basepath, ".yml") {
397+
return nil
398+
}
399+
400+
if data, err := ioutil.ReadFile(basepath); err == nil {
401+
hdCfg := &HDConfig{}
402+
if err := yaml.Unmarshal(data, hdCfg); err == nil {
403+
for _, category := range hdCfg.Categories {
404+
categories[category] = ""
405+
}
406+
}
407+
}
408+
return nil
409+
})
410+
for key := range categories {
411+
result = append(result, key)
412+
}
413+
return
414+
}
415+
397416
// FindPackagesByCategory returns the HDConfigs by category
398417
func FindPackagesByCategory(category string) (result []HDConfig) {
399418
configDir := sysos.ExpandEnv("$HOME/.config/hd-home/config/")

0 commit comments

Comments
 (0)