Skip to content

Commit 885acae

Browse files
authored
fix the incorrect dir reading (#370)
Co-authored-by: rick <[email protected]>
1 parent 579ac58 commit 885acae

File tree

8 files changed

+73
-31
lines changed

8 files changed

+73
-31
lines changed

.github/workflows/pull-request.yaml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,17 @@ jobs:
2525
args: release --skip-publish --rm-dist --snapshot
2626
- name: Test against the cmd
2727
run: |
28-
sudo ./release/http-downloader_linux_amd64_v1/hd install jenkins-zh/jenkins-cli/jcli
28+
sudo cp ./release/http-downloader_linux_amd64_v1/hd /usr/local/bin
29+
30+
# test with fullpath
31+
sudo hd install jenkins-zh/jenkins-cli/jcli
2932
jcli version
33+
34+
# test with simple path
35+
sudo hd i mde
36+
37+
# test with specific version
38+
sudo hd i [email protected] -f
3039
- name: Run Trivy vulnerability scanner
3140
uses: aquasecurity/[email protected]
3241
if: github.event_name == 'pull_request'

cmd/fetch.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"github.com/spf13/cobra"
1515
)
1616

17-
func newFetchCmd(context.Context) (cmd *cobra.Command) {
17+
func newFetchCmd(ctx context.Context) (cmd *cobra.Command) {
1818
opt := &fetchOption{
1919
fetcher: &installer.DefaultFetcher{},
2020
}
@@ -33,7 +33,7 @@ func newFetchCmd(context.Context) (cmd *cobra.Command) {
3333
flags.BoolVarP(&opt.reset, "reset", "", false,
3434
"If you want to reset the hd-config which means delete and clone it again")
3535
flags.IntVarP(&opt.retry, "retry", "", 6, "Retry times due to timeout error")
36-
flags.DurationVarP(&opt.timeout, "timeout", "", time.Second*10, "Timeout of fetching")
36+
flags.DurationVarP(&opt.timeout, "timeout", "", time.Second*60, "Timeout of fetching")
3737
return
3838
}
3939

@@ -70,7 +70,7 @@ func (o *fetchOption) runE(c *cobra.Command, _ []string) (err error) {
7070
break
7171
}
7272
o.setTimeout(c)
73-
logger.Print(".")
73+
c.Print(".")
7474
}
7575
if i >= 1 {
7676
logger.Println()

cmd/root.go

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ import (
44
"context"
55
"fmt"
66
"os"
7+
"path/filepath"
78
"runtime"
89

910
"github.com/linuxsuren/http-downloader/pkg/log"
11+
"github.com/mitchellh/go-homedir"
1012

1113
"github.com/AlecAivazis/survey/v2/terminal"
1214
extver "github.com/linuxsuren/cobra-extension/version"
@@ -30,11 +32,11 @@ func NewRoot(cxt context.Context) (cmd *cobra.Command) {
3032
}
3133
cmd.AddGroup(coreGroup, configGroup)
3234

33-
if err := loadConfig(); err != nil {
35+
v := viper.GetViper()
36+
if err := loadConfig(v); err != nil {
3437
panic(err)
3538
}
3639

37-
v := viper.GetViper()
3840
stdio := terminal.Stdio{
3941
Out: os.Stdout,
4042
In: os.Stdin,
@@ -65,27 +67,33 @@ func registerFlagCompletionFunc(cmd *cobra.Command, flag string, completionFunc
6567
}
6668
}
6769

68-
func loadConfig() (err error) {
69-
viper.SetConfigName("hd")
70-
viper.SetConfigType("yaml")
71-
viper.AddConfigPath("$HOME/.config")
72-
if err = viper.ReadInConfig(); err != nil {
70+
func loadConfig(v *viper.Viper) (err error) {
71+
var userHome string
72+
if userHome, err = homedir.Dir(); err != nil {
73+
return
74+
}
75+
76+
configDir := filepath.Join(userHome, ".config")
77+
v.SetConfigName("hd")
78+
v.SetConfigType("yaml")
79+
v.AddConfigPath(configDir)
80+
if err = v.ReadInConfig(); err != nil {
7381
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
7482
// Config file not found; ignore error if desired
7583
err = nil
7684
} else {
7785
err = fmt.Errorf("failed to load config: %s, error: %v", os.ExpandEnv("$HOME/.config/hd.yaml"), err)
7886
}
7987
}
80-
viper.SetDefault("provider", ProviderGitHub)
81-
viper.SetDefault("fetch", false)
82-
viper.SetDefault("goget", false)
83-
viper.SetDefault("no-proxy", false)
88+
v.SetDefault("provider", ProviderGitHub)
89+
v.SetDefault("fetch", false)
90+
v.SetDefault("goget", false)
91+
v.SetDefault("no-proxy", false)
8492

8593
thread := runtime.NumCPU()
8694
if thread > 4 {
8795
thread = thread / 2
8896
}
89-
viper.SetDefault("thread", thread)
97+
v.SetDefault("thread", thread)
9098
return
9199
}

cmd/setup.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package cmd
33
import (
44
"fmt"
55
"os"
6-
"path"
6+
"path/filepath"
77

88
"github.com/AlecAivazis/survey/v2"
99
"github.com/AlecAivazis/survey/v2/terminal"
1010
"github.com/linuxsuren/http-downloader/pkg/installer"
11+
"github.com/linuxsuren/http-downloader/pkg/log"
1112
"github.com/spf13/cobra"
1213
"github.com/spf13/viper"
1314
)
@@ -37,10 +38,10 @@ func (o *setupOption) runE(cmd *cobra.Command, args []string) (err error) {
3738
provider string
3839
)
3940

41+
logger := log.GetLoggerFromContextOrDefault(cmd)
4042
if proxyGitHub, err = selectFromList([]string{"", "ghproxy.com", "gh.api.99988866.xyz", "mirror.ghproxy.com"},
4143
o.v.GetString("proxy-github"),
4244
"Select proxy-github", o.stdio); err == nil {
43-
fmt.Println(proxyGitHub)
4445
o.v.Set("proxy-github", proxyGitHub)
4546
} else {
4647
return
@@ -55,16 +56,17 @@ func (o *setupOption) runE(cmd *cobra.Command, args []string) (err error) {
5556

5657
var configDir string
5758
fetcher := &installer.DefaultFetcher{}
58-
if configDir, err = fetcher.GetConfigDir(); err != nil {
59+
if configDir, err = fetcher.GetHomeDir(); err != nil {
5960
return
6061
}
6162
if err = os.MkdirAll(configDir, 0750); err != nil {
6263
err = fmt.Errorf("failed to create directory: %s, error: %v", configDir, err)
6364
return
6465
}
6566

66-
fmt.Println(o.v.GetString("proxy-github"))
67-
err = o.v.WriteConfigAs(path.Join(configDir, "hd.yaml"))
67+
configPath := filepath.Join(configDir, ".config", "hd.yaml")
68+
logger.Info("write config into:", configPath)
69+
err = o.v.WriteConfigAs(configPath)
6870
return
6971
}
7072

pkg/installer/fetch.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"io"
77
"os"
8-
"path"
8+
"path/filepath"
99
"strings"
1010

1111
"github.com/go-git/go-git/v5/plumbing/object"
@@ -32,6 +32,7 @@ var configRepos = map[string]string{
3232
// Fetcher is the interface of a fetcher which responses to fetch config files
3333
type Fetcher interface {
3434
GetConfigDir() (configDir string, err error)
35+
GetHomeDir() (homeDir string, err error)
3536
FetchLatestRepo(provider string, branch string,
3637
progress io.Writer) (err error)
3738
SetContext(ctx context.Context)
@@ -45,12 +46,18 @@ type DefaultFetcher struct {
4546
// GetConfigDir returns the directory of the config
4647
func (f *DefaultFetcher) GetConfigDir() (configDir string, err error) {
4748
var userHome string
48-
if userHome, err = homedir.Dir(); err == nil {
49-
configDir = path.Join(userHome, fmt.Sprintf(".config%shd-home", string(os.PathSeparator)))
49+
if userHome, err = f.GetHomeDir(); err == nil {
50+
configDir = filepath.Join(userHome, fmt.Sprintf(".config%shd-home", string(os.PathSeparator)))
5051
}
5152
return
5253
}
5354

55+
// GetHomeDir returns the user home directory
56+
func (f *DefaultFetcher) GetHomeDir() (homeDir string, err error) {
57+
homeDir, err = homedir.Dir()
58+
return
59+
}
60+
5461
// SetContext sets the context of the fetch
5562
func (f *DefaultFetcher) SetContext(ctx context.Context) {
5663
f.ctx = ctx
@@ -125,6 +132,7 @@ func (f *DefaultFetcher) FetchLatestRepo(provider string, branch string,
125132
RemoteName: remoteName,
126133
Progress: progress,
127134
Force: true,
135+
Depth: 1,
128136
}); err != nil && err != git.NoErrAlreadyUpToDate {
129137
err = fmt.Errorf("failed to fetch '%s', error: %v", remoteName, err)
130138
return
@@ -184,9 +192,10 @@ func (f *DefaultFetcher) FetchLatestRepo(provider string, branch string,
184192
_, _ = fmt.Fprintf(progress, "no local config exist, try to clone it\n")
185193

186194
if _, err = git.PlainCloneContext(f.ctx, configDir, false, &git.CloneOptions{
187-
RemoteName: remoteName,
188-
URL: repoAddr,
189-
Progress: progress,
195+
RemoteName: remoteName,
196+
URL: repoAddr,
197+
Progress: progress,
198+
SingleBranch: true,
190199
}); err != nil {
191200
err = fmt.Errorf("failed to clone git repository '%s' into '%s', error: %v", repoAddr, configDir, err)
192201
}
@@ -213,6 +222,7 @@ func makeSureRemote(name, repoAddr string, repo *git.Repository) (err error) {
213222
// FakeFetcher is a fake fetch. We expect to use it for unit test cases.
214223
type FakeFetcher struct {
215224
ConfigDir string
225+
HomeDir string
216226
GetConfigDirErr error
217227
FetchLatestRepoErr error
218228
}
@@ -224,6 +234,13 @@ func (f *FakeFetcher) GetConfigDir() (configDir string, err error) {
224234
return
225235
}
226236

237+
// GetHomeDir is a fake method
238+
func (f *FakeFetcher) GetHomeDir() (HomeDir string, err error) {
239+
HomeDir = f.HomeDir
240+
err = f.GetConfigDirErr
241+
return
242+
}
243+
227244
// FetchLatestRepo is a fake method
228245
func (f *FakeFetcher) FetchLatestRepo(provider string, branch string,
229246
progress io.Writer) (err error) {

pkg/installer/fetch_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package installer
33
import (
44
"context"
55
"fmt"
6-
"github.com/stretchr/testify/assert"
76
"os/user"
87
"testing"
8+
9+
"github.com/stretchr/testify/assert"
910
)
1011

1112
func TestGetConfigDir(t *testing.T) {
@@ -27,4 +28,8 @@ func TestGetConfigDir(t *testing.T) {
2728
err = fetcher.FetchLatestRepo("", "", nil)
2829
assert.Nil(t, err)
2930
fetcher.SetContext(context.TODO())
31+
32+
dir, err = fetcher.GetHomeDir()
33+
assert.Equal(t, "", dir)
34+
assert.Nil(t, err)
3035
}

pkg/log/level.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package log
22

33
import (
44
"context"
5+
"fmt"
56
syslog "log"
67
)
78

@@ -14,13 +15,14 @@ type LevelLog struct {
1415
// Info prints the info level message.
1516
// Info level means the level >= 3
1617
func (l *LevelLog) Info(v ...any) {
18+
fmt.Println(l.level)
1719
if l.level >= 3 {
1820
l.Println(v...)
1921
}
2022
}
2123

2224
// Debug prints the debug level message.
23-
// Debug level means the level >= 3
25+
// Debug level means the level >= 7
2426
func (l *LevelLog) Debug(v ...any) {
2527
if l.level >= 7 {
2628
l.Println(v...)

pkg/os/installer.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package os
22

33
import (
44
"fmt"
5-
"path"
65
"path/filepath"
76
"strings"
87

@@ -35,7 +34,7 @@ func init() {
3534
var userHome string
3635
var err error
3736
if userHome, err = homedir.Dir(); err == nil {
38-
configDir := path.Join(userHome, "/.config/hd-home")
37+
configDir := filepath.Join(userHome, "/.config/hd-home")
3938
if err = GenericInstallerRegistry(filepath.Join(configDir, "config/generic.yaml"), defaultInstallerRegistry); err != nil {
4039
fmt.Println(err)
4140
}

0 commit comments

Comments
 (0)