Skip to content

Commit 597e7b2

Browse files
authored
Add setup command to init the config (#184)
1 parent f315e41 commit 597e7b2

File tree

5 files changed

+48
-5
lines changed

5 files changed

+48
-5
lines changed

cmd/get.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ func newGetCmd(ctx context.Context) (cmd *cobra.Command) {
6565
type downloadOption struct {
6666
searchOption
6767

68-
URL string
69-
Output string
70-
ShowProgress bool
68+
URL string
69+
Output string
70+
ShowProgress bool
7171
Timeout int
7272
MaxAttempts int
7373
AcceptPreRelease bool

cmd/root.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package cmd
22

33
import (
44
"context"
5+
"fmt"
56
extpkg "github.com/linuxsuren/cobra-extension/pkg"
67
extver "github.com/linuxsuren/cobra-extension/version"
78
"github.com/spf13/cobra"
89
"github.com/spf13/viper"
10+
"os"
911
"runtime"
1012
)
1113

@@ -21,7 +23,7 @@ func NewRoot(cxt context.Context) (cmd *cobra.Command) {
2123
}
2224

2325
cmd.AddCommand(
24-
newGetCmd(cxt), newInstallCmd(cxt), newFetchCmd(cxt), newSearchCmd(cxt), newTestCmd(),
26+
newGetCmd(cxt), newInstallCmd(cxt), newFetchCmd(cxt), newSearchCmd(cxt), newTestCmd(), newSetupCommand(),
2527
extver.NewVersionCmd("linuxsuren", "http-downloader", "hd", nil),
2628
extpkg.NewCompletionCmd(cmd))
2729
return
@@ -36,6 +38,8 @@ func loadConfig() (err error) {
3638
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
3739
// Config file not found; ignore error if desired
3840
err = nil
41+
} else {
42+
err = fmt.Errorf("failed to load config: %s, error: %v", os.ExpandEnv("$HOME/.config/hd.yaml"), err)
3943
}
4044
}
4145
viper.SetDefault("provider", ProviderGitHub)

cmd/setup.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package cmd
2+
3+
import (
4+
"github.com/AlecAivazis/survey/v2"
5+
"github.com/spf13/cobra"
6+
"github.com/spf13/viper"
7+
)
8+
9+
func newSetupCommand() (cmd *cobra.Command) {
10+
opt := &setupOption{}
11+
cmd = &cobra.Command{
12+
Use: "setup",
13+
Short: "Init the configuration of hd",
14+
RunE: opt.runE,
15+
}
16+
return
17+
}
18+
19+
type setupOption struct {
20+
}
21+
22+
func (o *setupOption) runE(cmd *cobra.Command, args []string) (err error) {
23+
selector := &survey.Select{
24+
Message: "Select proxy-github",
25+
Options: []string{"gh.api.99988866.xyz", "ghproxy.com", "mirror.ghproxy.com", ""},
26+
}
27+
28+
var choose string
29+
if err = survey.AskOne(selector, &choose); err == nil {
30+
viper.Set("proxy-github", choose)
31+
} else {
32+
return
33+
}
34+
35+
err = viper.WriteConfig()
36+
return
37+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/linuxsuren/http-downloader
33
go 1.16
44

55
require (
6-
github.com/AlecAivazis/survey/v2 v2.3.1
6+
github.com/AlecAivazis/survey/v2 v2.3.2
77
github.com/go-git/go-git/v5 v5.4.2
88
github.com/golang/mock v1.6.0
99
github.com/google/go-github/v29 v29.0.3

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
4646
github.com/AlecAivazis/survey/v2 v2.2.2/go.mod h1:9FJRdMdDm8rnT+zHVbvQT2RTSTLq0Ttd6q3Vl2fahjk=
4747
github.com/AlecAivazis/survey/v2 v2.3.1 h1:lzkuHA60pER7L4eYL8qQJor4bUWlJe4V0gqAT19tdOA=
4848
github.com/AlecAivazis/survey/v2 v2.3.1/go.mod h1:TH2kPCDU3Kqq7pLbnCWwZXDBjnhZtmsCle5EiYDJ2fg=
49+
github.com/AlecAivazis/survey/v2 v2.3.2 h1:TqTB+aDDCLYhf9/bD2TwSO8u8jDSmMUd2SUVO4gCnU8=
50+
github.com/AlecAivazis/survey/v2 v2.3.2/go.mod h1:TH2kPCDU3Kqq7pLbnCWwZXDBjnhZtmsCle5EiYDJ2fg=
4951
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
5052
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
5153
github.com/Microsoft/go-winio v0.4.14/go.mod h1:qXqCSQ3Xa7+6tgxaGTIe4Kpcdsi+P8jBhyzoq1bpyYA=

0 commit comments

Comments
 (0)