-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
64 lines (55 loc) · 1.48 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
package main
import (
"fmt"
"gopkg.in/ini.v1"
"log"
"os"
)
type Config struct {
Region string
AccessKey string
SecretKey string
SessionToken string
PathToStageKey string
PathToProdKey string
}
const credentialsFile = ".aws/credentials"
func NewConfig() (*Config, error) {
path := getConfigPath()
cfg, err := ini.Load(path)
if err != nil {
return nil, err
}
pathToStageKey := cfg.Section("certs").Key("stg").String()
if pathToStageKey == "" {
pathToStageKey = inputField("path to stg key")
cfg.Section("certs").Key("stg").SetValue(pathToStageKey)
cfg.SaveTo(path)
}
pathToProdKey := cfg.Section("certs").Key("prod").String()
if pathToProdKey == "" {
pathToProdKey = inputField("path to prod key")
cfg.Section("certs").Key("prod").SetValue(pathToProdKey)
cfg.SaveTo(path)
}
return &Config{
Region: cfg.Section("default").Key("region").String(),
AccessKey: cfg.Section("default").Key("aws_access_key_id").String(),
SecretKey: cfg.Section("default").Key("aws_secret_access_key").String(),
SessionToken: cfg.Section("default").Key("aws_session_token").String(),
PathToStageKey: pathToStageKey,
PathToProdKey: pathToProdKey,
}, nil
}
func getConfigPath() string {
homedir, _ := os.UserHomeDir()
return homedir + "/" + credentialsFile
}
func inputField(fieldName string) string {
var result string
fmt.Printf("Enter " + fieldName + ": ")
if _, err := fmt.Scanln(&result); err != nil {
log.Fatal(err)
}
return result
}