-
Notifications
You must be signed in to change notification settings - Fork 330
/
config.go
82 lines (75 loc) · 1.65 KB
/
config.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
package cmd
import (
"fmt"
"net/url"
"path/filepath"
"github.com/manifoldco/promptui"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// configCmd represents the config command
var configCmd = &cobra.Command{
Use: "config",
Short: "Create/update local lakeFS configuration",
Run: func(cmd *cobra.Command, args []string) {
if viper.ConfigFileUsed() == "" {
// Find home directory.
home, err := homedir.Dir()
if err != nil {
DieErr(err)
}
// Setup default config file
viper.SetConfigFile(filepath.Join(home, ".lakectl.yaml"))
}
fmt.Printf("Config file %s will be used\n", viper.ConfigFileUsed())
// get user input
questions := []struct {
Key string
Prompt promptui.Prompt
}{
{
Key: "credentials.access_key_id",
Prompt: promptui.Prompt{
Label: "Access key ID",
},
},
{
Key: "credentials.secret_access_key",
Prompt: promptui.Prompt{
Label: "Secret access key",
Mask: '*',
},
},
{
Key: "server.endpoint_url",
Prompt: promptui.Prompt{
Label: "Server endpoint URL (e.g. http://localhost:8000)",
Validate: func(rawURL string) error {
_, err := url.ParseRequestURI(rawURL)
return err
},
},
},
}
for _, question := range questions {
question.Prompt.Default = viper.GetString(question.Key)
val, err := question.Prompt.Run()
if err != nil {
DieErr(err)
}
viper.Set(question.Key, val)
}
err := viper.SafeWriteConfig()
if err != nil {
err = viper.WriteConfig()
}
if err != nil {
DieErr(err)
}
},
}
//nolint:gochecknoinits
func init() {
rootCmd.AddCommand(configCmd)
}