/
configure_helper.go
174 lines (136 loc) · 3.09 KB
/
configure_helper.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package cmd
import (
"bufio"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/BurntSushi/toml"
homedir "github.com/mitchellh/go-homedir"
"github.com/olekukonko/tablewriter"
)
var loadedConfig *config
type config struct {
Endpoint string
Profiles map[string]profile
}
type profile struct {
APIKey string
Endpoint string
}
func getConfig() (*config, error) {
if loadedConfig != nil {
return loadedConfig, nil
}
path, err := getConfigPath()
if err != nil {
return nil, err
}
config, err := loadConfig(path)
if err != nil {
return nil, err
}
loadedConfig = config
return loadedConfig, nil
}
func getProfile(keyName string) (*profile, error) {
configVal, err := getConfig()
if err != nil {
return nil, err
}
// set default
if keyName == "" {
keyName = getProfileName()
}
profileVal, ok := configVal.Profiles[keyName]
if !ok {
return nil, fmt.Errorf("The specified \"%s\" profile did not exist", keyName)
}
// set default
if profileVal.Endpoint == "" {
profileVal.Endpoint = configVal.Endpoint
}
return &profileVal, nil
}
func getConfigDir() (string, error) {
dir, err := homedir.Dir()
if err != nil {
return "", err
}
configDir := filepath.Join(dir, ".ca")
return configDir, nil
}
func getConfigFileName() string {
return "config"
}
func getProfileName() string {
return "default"
}
func getEndpoint() string {
return "https://manager.cloudautomator.com/api/v1"
}
func getConfigPath() (string, error) {
dir, err := getConfigDir()
if err != nil {
return "", err
}
path := filepath.Join(dir, getConfigFileName())
return path, nil
}
func maskedAPIKey(apiKey string) string {
prefix := apiKey[:3]
suffix := apiKey[len(apiKey)-3:]
length := len(apiKey) - (len(prefix) + len(suffix))
masked := strings.Repeat("*", length)
return prefix + masked + suffix
}
func drawConfigTable(keyName string, config *config) {
tabelData := make([][]string, len(config.Profiles))
// set table data
for key, value := range config.Profiles {
drawAPIKey := maskedAPIKey(value.APIKey)
drawEndpoint := value.Endpoint
if drawEndpoint == "" || drawEndpoint == config.Endpoint {
drawEndpoint = "default"
}
if keyName != "" {
if key == keyName {
tabelData = append(tabelData, []string{key, drawAPIKey, drawEndpoint})
break
}
} else {
tabelData = append(tabelData, []string{key, drawAPIKey, drawEndpoint})
}
}
if len(tabelData) > 0 {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Profile", "API Key", "Endpoint"})
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.AppendBulk(tabelData)
table.Render()
}
}
func loadConfig(path string) (*config, error) {
var config config
if _, err := toml.DecodeFile(path, &config); err != nil {
return nil, err
}
return &config, nil
}
func saveConfig(path string, config *config) error {
dir, _ := getConfigDir()
err := os.MkdirAll(dir, 0700)
if err != nil {
return err
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
writer := bufio.NewWriter(f)
if err := toml.NewEncoder(writer).Encode(config); err != nil {
return err
}
return nil
}