This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
config.go
146 lines (121 loc) · 3.5 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package cli
import (
"encoding/json"
"io/ioutil"
"os"
"errors"
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/hortonworks/cb-cli/cli/utils"
"github.com/mitchellh/go-homedir"
"github.com/urfave/cli"
"gopkg.in/yaml.v2"
)
const (
Config_dir = ".cb"
Config_file = "config"
)
type Config struct {
Username string `json:"username" yaml:"username"`
Password string `json:"password,omitempty" yaml:"password,omitempty"`
Server string `json:"server" yaml:"server"`
AuthType string `json:"authType,omitempty" yaml:"authType,omitempty"`
Organization string `json:"organization,omitempty" yaml:"organization,omitempty"`
Output string `json:"output,omitempty" yaml:"output,omitempty"`
}
type ConfigList map[string]Config
func (c Config) Yaml() string {
j, _ := yaml.Marshal(c)
return string(j)
}
func (c ConfigList) Json() string {
j, _ := json.MarshalIndent(c, "", " ")
return string(j)
}
func (c ConfigList) Yaml() string {
j, _ := yaml.Marshal(c)
return string(j)
}
func Configure(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
err := WriteConfigToFile(GetHomeDirectory(), c.String(FlServerOptional.Name),
c.String(FlUsername.Name), c.String(FlPassword.Name),
c.String(FlOutputOptional.Name), c.String(FlProfileOptional.Name),
c.String(FlAuthTypeOptional.Name), c.String(FlOrganizationOptional.Name))
if err != nil {
utils.LogErrorAndExit(err)
}
}
func GetHomeDirectory() string {
homeDir, err := homedir.Dir()
if err != nil || len(homeDir) == 0 {
utils.LogErrorMessageAndExit("failed to determine the home directory")
}
return homeDir
}
func ReadConfig(baseDir string, profile string) (*Config, error) {
configDir := baseDir + "/" + Config_dir
configFile := configDir + "/" + Config_file
if _, err := os.Stat(configFile); os.IsNotExist(err) {
return new(Config), nil
}
log.Infof("[ReadConfig] found config file: %s", configFile)
var configList ConfigList
err := readConfigToList(configFile, &configList)
if err != nil {
return nil, err
}
if config, valid := configList[profile]; valid {
log.Infof("[ReadConfig] selected profile: %s", profile)
return &config, nil
} else {
return nil, errors.New(fmt.Sprintf("Invalid profile selected: %s", profile))
}
}
func WriteConfigToFile(baseDir, server, username, password, output, profile, authType, organization string) error {
configDir := baseDir + "/" + Config_dir
configFile := configDir + "/" + Config_file
if len(profile) == 0 {
profile = "default"
}
if _, err := os.Stat(configDir); os.IsNotExist(err) {
log.Infof("[writeConfigToFile] create dir: %s", configDir)
err = os.MkdirAll(configDir, 0700)
if err != nil {
return err
}
} else {
log.Infof("[writeConfigToFile] dir already exists: %s", configDir)
}
log.Infof("[writeConfigToFile] writing credentials to file: %s", configFile)
var configList = make(ConfigList)
if _, err := os.Stat(configFile); !os.IsNotExist(err) {
err = readConfigToList(configFile, &configList)
if err != nil {
return err
}
}
configList[profile] = Config{Server: server,
Organization: organization,
Username: username,
Password: password,
Output: output,
AuthType: authType,
}
err := ioutil.WriteFile(configFile, []byte(configList.Yaml()), 0600)
if err != nil {
return err
}
return nil
}
func readConfigToList(configPath string, configList *ConfigList) error {
content, err := ioutil.ReadFile(configPath)
if err != nil {
return err
}
err = yaml.Unmarshal(content, configList)
if err != nil {
return err
}
return nil
}