-
Notifications
You must be signed in to change notification settings - Fork 52
/
global_config.go
123 lines (104 loc) · 2.77 KB
/
global_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
package globalconfig
import (
"bytes"
"io/ioutil"
"log"
"os"
"os/user"
"path"
"github.com/commitdev/zero/internal/constants"
"github.com/commitdev/zero/pkg/util/exit"
yaml "gopkg.in/yaml.v2"
)
var GetCredentialsPath = getCredentialsPath
type ProjectCredentials map[string]ProjectCredential
type ProjectCredential struct {
ProjectName string `yaml:"-"`
AWSResourceConfig `yaml:"aws,omitempty"`
GithubResourceConfig `yaml:"github,omitempty"`
CircleCiResourceConfig `yaml:"circleci,omitempty"`
}
type AWSResourceConfig struct {
AccessKeyId string `yaml:"accessKeyId,omitempty"`
SecretAccessKey string `yaml:"secretAccessKey,omitempty"`
}
type GithubResourceConfig struct {
AccessToken string `yaml:"accessToken,omitempty"`
}
type CircleCiResourceConfig struct {
ApiKey string `yaml:"apiKey,omitempty"`
}
func (p ProjectCredentials) Unmarshal(data []byte) error {
if len(data) == 0 {
return nil
}
err := yaml.NewDecoder(bytes.NewReader(data)).Decode(p)
if err != nil {
return err
}
for k, v := range p {
v.ProjectName = k
p[k] = v
}
return nil
}
func LoadUserCredentials() ProjectCredentials {
data := readOrCreateUserCredentialsFile()
projects := ProjectCredentials{}
err := projects.Unmarshal(data)
if err != nil {
exit.Fatal("Failed to parse configuration: %v", err)
}
return projects
}
func getCredentialsPath() string {
usr, err := user.Current()
if err != nil {
exit.Fatal("Failed to get user directory path: %v", err)
}
rootDir := path.Join(usr.HomeDir, constants.ZeroHomeDirectory)
os.MkdirAll(rootDir, os.ModePerm)
filePath := path.Join(rootDir, constants.UserCredentialsYml)
return filePath
}
func readOrCreateUserCredentialsFile() []byte {
credPath := GetCredentialsPath()
_, fileStateErr := os.Stat(credPath)
if os.IsNotExist(fileStateErr) {
var file, fileStateErr = os.Create(credPath)
if fileStateErr != nil {
exit.Fatal("Failed to create config file: %v", fileStateErr)
}
defer file.Close()
}
data, err := ioutil.ReadFile(credPath)
if err != nil {
exit.Fatal("Failed to read credentials file: %v", err)
}
return data
}
func GetProjectCredentials(targetProjectName string) ProjectCredential {
projects := LoadUserCredentials()
if val, ok := projects[targetProjectName]; ok {
return val
} else {
p := ProjectCredential{
ProjectName: targetProjectName,
}
projects[targetProjectName] = p
return p
}
}
func Save(project ProjectCredential) {
projects := LoadUserCredentials()
projects[project.ProjectName] = project
writeCredentialsFile(projects)
}
func writeCredentialsFile(projects ProjectCredentials) {
credsPath := GetCredentialsPath()
content, _ := yaml.Marshal(projects)
err := ioutil.WriteFile(credsPath, content, 0644)
if err != nil {
log.Panicf("failed to write config: %v", err)
}
}