-
Notifications
You must be signed in to change notification settings - Fork 5
/
config.go
96 lines (83 loc) · 2.7 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
package masl
import (
"os"
"os/user"
"strings"
"github.com/BurntSushi/toml"
)
// Accounts represents the accounts section of the masl config file
type Accounts []struct {
ID string `toml:"ID"`
Name string `toml:"Name"`
EnvironmentIndependent bool `toml:"EnvironmentIndependent"`
}
// Config represents the masl config file
type Config struct {
BaseURL string `toml:"BaseURL"`
ClientID string `toml:"ClientID"`
ClientSecret string `toml:"ClientSecret"`
AppID string `toml:"AppID"`
Subdomain string `toml:"Subdomain"`
Username string `toml:"Username"`
Duration int `toml:"Duration"`
Profile string `toml:"Profile"`
DefaultRole string `toml:"DefaultRole"`
LegacyToken bool `toml:"LegacyToken"`
Debug bool `toml:"Debug"`
DefaulMFADevice string `toml:"DefaulMFADevice"`
Environments []struct {
Name string `toml:"Name"`
Accounts []string `toml:"Accounts"`
} `toml:"Environments"`
Accounts Accounts `toml:"Accounts"`
}
// TODO: best way to make this global? Make the level dynamic here as well.
var logger = GetLogger("info")
// GetConfig reads the .masl/config.toml configuration file for initialization.
func GetConfig() Config {
usr, err := user.Current()
if err != nil {
logger.Fatal(err.Error())
}
// Read .masl/config.toml config file for initialization
conf := Config{Profile: "masl", LegacyToken: false, Debug: false, Duration: 3600} // Set default values
if _, err := toml.DecodeFile(usr.HomeDir+string(os.PathSeparator)+".masl"+string(os.PathSeparator)+"config.toml", &conf); err != nil {
logger.Fatal(err.Error())
}
return conf
}
// SearchAccounts search an account name for a given acount id
func SearchAccounts(accountInfo Accounts, accountID string) (string, bool) {
for _, account := range accountInfo {
if account.ID == accountID {
return account.Name, account.EnvironmentIndependent
}
}
return "untitled", false
}
// GetAccountID get the account id for a given acount name (alias)
func GetAccountID(conf Config, name string) string {
var id string
for _, account := range conf.Accounts {
if strings.EqualFold(account.Name, name) {
id = account.ID
}
}
return id
}
// GetAccountsForEnvironment search an environment's detail for a given environment name
func GetAccountsForEnvironment(conf Config, environment string) []string {
var accounts []string
for _, env := range conf.Environments {
if strings.EqualFold(env.Name, environment) {
accounts = append(accounts, env.Accounts...)
break
}
}
for _, account := range conf.Accounts {
if account.EnvironmentIndependent {
accounts = append(accounts, account.ID)
}
}
return accounts
}