forked from aliyun/aliyun-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
configuration.go
212 lines (190 loc) · 4.98 KB
/
configuration.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (c) 2009-present, Alibaba Cloud All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"runtime"
"github.com/aliyun/aliyun-cli/cli"
)
const (
configPath = "/.aliyun"
configFile = "config.json"
DefaultConfigProfileName = "default"
)
type Configuration struct {
CurrentProfile string `json:"current"`
Profiles []Profile `json:"profiles"`
MetaPath string `json:"meta_path"`
//Plugins []Plugin `json:"plugin"`
}
var hookGetHomePath = func(fn func() string) func() string {
return fn
}
func NewConfiguration() *Configuration {
return &Configuration{
CurrentProfile: DefaultConfigProfileName,
Profiles: []Profile{
NewProfile(DefaultConfigProfileName),
},
}
}
func (c *Configuration) NewProfile(pn string) Profile {
p, ok := c.GetProfile(pn)
if !ok {
p = NewProfile(pn)
c.PutProfile(p)
}
return p
}
func (c *Configuration) GetProfile(pn string) (Profile, bool) {
for _, p := range c.Profiles {
if p.Name == pn {
return p, true
}
}
return Profile{Name: pn}, false
}
func (c *Configuration) GetCurrentProfile(ctx *cli.Context) Profile {
profileName := ProfileFlag(ctx.Flags()).GetStringOrDefault(c.CurrentProfile)
if profileName == "" || profileName == "default" {
switch {
case os.Getenv("ALIBABACLOUD_PROFILE") != "":
profileName = os.Getenv("ALIBABACLOUD_PROFILE")
case os.Getenv("ALIBABA_CLOUD_PROFILE") != "":
profileName = os.Getenv("ALIBABA_CLOUD_PROFILE")
case os.Getenv("ALICLOUD_PROFILE") != "":
profileName = os.Getenv("ALICLOUD_PROFILE")
}
}
p, _ := c.GetProfile(profileName)
p.OverwriteWithFlags(ctx)
return p
}
func (c *Configuration) PutProfile(profile Profile) {
for i, p := range c.Profiles {
if p.Name == profile.Name {
c.Profiles[i] = profile
return
}
}
c.Profiles = append(c.Profiles, profile)
}
func LoadCurrentProfile() (Profile, error) {
return LoadProfile(GetConfigPath()+"/"+configFile, "")
}
func LoadProfile(path string, name string) (Profile, error) {
var p Profile
config, err := hookLoadConfiguration(LoadConfiguration)(path)
if err != nil {
return p, fmt.Errorf("init config failed %v", err)
}
if name == "" {
name = config.CurrentProfile
}
p, ok := config.GetProfile(name)
p.parent = config
if !ok {
return p, fmt.Errorf("unknown profile %s, run configure to check", name)
}
return p, nil
}
func LoadProfileWithContext(ctx *cli.Context) (profile Profile, err error) {
if os.Getenv("ALIBABACLOUD_IGNORE_PROFILE") == "TRUE" {
profile = NewProfile("default")
profile.RegionId = "cn-hangzhou"
} else {
var currentPath string
if path, ok := ConfigurePathFlag(ctx.Flags()).GetValue(); ok {
currentPath = path
} else {
currentPath = GetConfigPath() + "/" + configFile
}
if name, ok := ProfileFlag(ctx.Flags()).GetValue(); ok {
profile, err = LoadProfile(currentPath, name)
} else {
profile, err = LoadProfile(currentPath, "")
}
if err != nil {
return
}
}
//Load from flags
profile.OverwriteWithFlags(ctx)
err = profile.Validate()
return
}
func LoadConfiguration(path string) (conf *Configuration, err error) {
_, statErr := os.Stat(path)
if os.IsNotExist(statErr) {
conf, err = MigrateLegacyConfiguration()
if err != nil {
return
}
if conf != nil {
err = SaveConfiguration(conf)
if err != nil {
err = fmt.Errorf("save failed %v", err)
return
}
return
}
conf = NewConfiguration()
return
}
bytes, err := ioutil.ReadFile(path)
if err != nil {
err = fmt.Errorf("reading config from '%s' failed %v", path, err)
return
}
conf, err = NewConfigFromBytes(bytes)
return
}
func SaveConfiguration(config *Configuration) (err error) {
// fmt.Printf("conf %v\n", config)
bytes, err := json.MarshalIndent(config, "", "\t")
if err != nil {
return
}
path := GetConfigPath() + "/" + configFile
err = ioutil.WriteFile(path, bytes, 0600)
return
}
func NewConfigFromBytes(bytes []byte) (conf *Configuration, err error) {
conf = NewConfiguration()
err = json.Unmarshal(bytes, conf)
return
}
func GetConfigPath() string {
path := hookGetHomePath(GetHomePath)() + configPath
if _, err := os.Stat(path); os.IsNotExist(err) {
err = os.MkdirAll(path, 0755)
if err != nil {
panic(err)
}
}
return path
}
func GetHomePath() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return os.Getenv("HOME")
}