generated from LinuxSuRen/github-go
/
setup.go
86 lines (75 loc) · 2.19 KB
/
setup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package cmd
import (
"fmt"
"os"
"path/filepath"
"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/linuxsuren/http-downloader/pkg/installer"
"github.com/linuxsuren/http-downloader/pkg/log"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func newSetupCommand(v *viper.Viper, stdio terminal.Stdio) (cmd *cobra.Command) {
opt := &setupOption{
stdio: stdio,
v: v,
}
cmd = &cobra.Command{
Use: "setup",
Short: "Init the configuration of hd",
RunE: opt.runE,
GroupID: configGroup.ID,
}
flags := cmd.Flags()
flags.StringVarP(&opt.proxy, "proxy", "p", "", "The proxy of GitHub")
flags.StringVarP(&opt.provider, "provider", "", "", "The provider of hd configuration")
return
}
type setupOption struct {
stdio terminal.Stdio
v *viper.Viper
proxy string
provider string
}
func (o *setupOption) runE(cmd *cobra.Command, args []string) (err error) {
logger := log.GetLoggerFromContextOrDefault(cmd)
proxyServers := []string{""}
proxyServers = append(proxyServers, installer.GetProxyServers()...)
if o.proxy == "" {
if o.proxy, err = selectFromList(proxyServers,
o.v.GetString("proxy-github"),
"Select proxy-github", o.stdio); err != nil {
return
}
}
o.v.Set("proxy-github", o.proxy)
if o.provider == "" {
if o.provider, err = selectFromList([]string{"github", "gitee"}, o.v.GetString("provider"),
"Select provider", o.stdio); err != nil {
return
}
}
o.v.Set("provider", o.provider)
var configDir string
fetcher := &installer.DefaultFetcher{}
if configDir, err = fetcher.GetHomeDir(); err == nil {
if err = os.MkdirAll(configDir, 0750); err != nil {
err = fmt.Errorf("failed to create directory: %s, error: %v", configDir, err)
return
}
configPath := filepath.Join(configDir, ".config", "hd.yaml")
logger.Info("write config into:", configPath)
err = o.v.WriteConfigAs(configPath)
}
return
}
func selectFromList(items []string, defaultItem, title string, stdio terminal.Stdio) (val string, err error) {
selector := &survey.Select{
Message: title,
Options: items,
Default: defaultItem,
}
err = survey.AskOne(selector, &val, survey.WithStdio(stdio.In, stdio.Out, stdio.Err))
return
}