This repository has been archived by the owner on Aug 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
137 lines (119 loc) · 2.78 KB
/
api.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
package main
import (
"fmt"
"io/ioutil"
"log"
"os/user"
"path/filepath"
"strings"
)
var ConfigFile = GetHomePath(".webbynode")
func UserHome() string {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
return usr.HomeDir
}
func GetHomePath(file string) string {
return filepath.Join(UserHome(), file)
}
func ReadConfig(file string) []string {
b, err := ioutil.ReadFile(file)
if err != nil {
panic(err)
}
return strings.Split(string(b), "\n")
}
func GetCredentials(inCfg *WebbynodeCfg, overwrite bool) *WebbynodeCfg {
var config *WebbynodeCfg
if inCfg == nil {
config = &WebbynodeCfg{}
} else {
config = inCfg
}
config.configFile = GetHomePath(".webbynode")
if config.Exists() {
config.Load()
}
if !config.Exists() || overwrite {
if overwrite || config.system == "" {
system, err := Ask("What's the end point you're using - manager or manager2? ")
if err != nil {
panic(err)
}
config.system = system
}
if overwrite || config.email == "" {
email, err := Ask("Login email: ")
if err != nil {
panic(err)
}
config.email = email
}
if overwrite || config.token == "" {
token, err := Ask("API token: ")
if err != nil {
panic(err)
}
config.token = token
}
config.Save()
}
return config
}
func (cfg *WebbynodeCfg) Load() (bool, error) {
if !cfg.Exists() {
return false, nil
}
for _, line := range ReadConfig(cfg.configFile) {
if strings.TrimSpace(line) != "" {
parts := strings.SplitN(line, "=", 2)
switch parts[0] {
case "email":
cfg.email = parts[1]
case "token":
cfg.token = parts[1]
case "system":
cfg.system = parts[1]
case "aws_key":
cfg.awsKey = parts[1]
case "aws_secret":
cfg.awsSecret = parts[1]
}
}
}
return true, nil
}
func (cfg *WebbynodeCfg) Save() error {
var lines []string
if cfg.email != "" {
lines = append(lines, fmt.Sprintf("email=%s", cfg.email))
}
if cfg.token != "" {
lines = append(lines, fmt.Sprintf("token=%s", cfg.token))
}
if cfg.system != "" {
lines = append(lines, fmt.Sprintf("system=%s", cfg.system))
}
if cfg.awsKey != "" {
lines = append(lines, fmt.Sprintf("aws_key=%s", cfg.awsKey))
}
if cfg.awsSecret != "" {
lines = append(lines, fmt.Sprintf("aws_secret=%s", cfg.awsSecret))
}
lines = append(lines, "")
contents := []byte(strings.Join(lines, "\n"))
return ioutil.WriteFile(cfg.configFile, contents, 0644)
}
func (cfg *WebbynodeCfg) Exists() bool {
return FileExists(cfg.configFile)
}
type WebbynodeCfg struct {
configFile string
email string
token string
system string
awsKey string
awsSecret string
}