-
-
Notifications
You must be signed in to change notification settings - Fork 369
Expand file tree
/
Copy pathconfigure.go
More file actions
91 lines (74 loc) · 2.32 KB
/
configure.go
File metadata and controls
91 lines (74 loc) · 2.32 KB
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
87
88
89
90
91
package cmd
import (
"fmt"
"text/tabwriter"
"github.com/exercism/cli/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
viperUserConfig *viper.Viper
viperAPIConfig *viper.Viper
)
// configureCmd configures the command-line client with user-specific settings.
var configureCmd = &cobra.Command{
Use: "configure",
Aliases: []string{"c"},
Short: "Configure the command-line client.",
Long: `Configure the command-line client to customize it to your needs.
This lets you set up the CLI to talk to the API on your behalf,
and tells the CLI about your setup so it puts things in the right
places.
You can also override certain default settings to suit your preferences.
`,
RunE: func(cmd *cobra.Command, args []string) error {
usrCfg := config.NewEmptyUserConfig()
err := usrCfg.Load(viperUserConfig)
if err != nil {
return err
}
if usrCfg.Workspace == "" {
fmt.Println(usrCfg.Home + BinaryName)
}
apiCfg := config.NewEmptyAPIConfig()
err = apiCfg.Load(viperAPIConfig)
if err != nil {
return err
}
apiCfg.SetDefaults()
show, err := cmd.Flags().GetBool("show")
if err != nil {
return err
}
if show {
w := tabwriter.NewWriter(Out, 0, 0, 2, ' ', 0)
defer w.Flush()
fmt.Fprintln(w, "")
fmt.Fprintln(w, fmt.Sprintf("Config dir:\t%s", config.Dir()))
fmt.Fprintln(w, fmt.Sprintf("-t, --token\t%s", usrCfg.Token))
fmt.Fprintln(w, fmt.Sprintf("-w, --workspace\t%s", usrCfg.Workspace))
fmt.Fprintln(w, fmt.Sprintf("-a, --api\t%s", apiCfg.BaseURL))
return nil
}
err = usrCfg.Write()
if err != nil {
return err
}
return apiCfg.Write()
},
}
func initConfigureCmd() {
configureCmd.Flags().StringP("token", "t", "", "authentication token used to connect to the site")
configureCmd.Flags().StringP("workspace", "w", "", "directory for exercism exercises")
configureCmd.Flags().StringP("api", "a", "", "API base url")
configureCmd.Flags().BoolP("show", "s", false, "show the current configuration")
viperUserConfig = viper.New()
viperUserConfig.BindPFlag("token", configureCmd.Flags().Lookup("token"))
viperUserConfig.BindPFlag("workspace", configureCmd.Flags().Lookup("workspace"))
viperAPIConfig = viper.New()
viperAPIConfig.BindPFlag("baseurl", configureCmd.Flags().Lookup("api"))
}
func init() {
RootCmd.AddCommand(configureCmd)
initConfigureCmd()
}