forked from buger/cloud-ssh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
68 lines (56 loc) · 1.43 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
package main
import (
"fmt"
"gopkg.in/yaml.v1"
"io/ioutil"
"log"
"os"
"runtime"
)
type Config map[string]StrMap
type StrMap map[string]string
func userHomeDir() string {
if runtime.GOOS == "windows" {
home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
return home
}
return os.Getenv("HOME")
}
func readConfig() (config Config) {
config = make(Config)
prefferedPaths := []string{
"./cloud-ssh.yaml",
userHomeDir() + "/.ssh/cloud-ssh.yaml",
"/etc/cloud-ssh.yaml",
}
var content []byte
for _, path := range prefferedPaths {
if _, err := os.Stat(path); err == nil {
fmt.Println("Found config:", path)
content, err = ioutil.ReadFile(path)
if err != nil {
log.Fatal("Error while reading config: ", err)
}
break
}
}
if os.Getenv("AWS_ACCESS_KEY_ID") != "" && os.Getenv("AWS_SECRET_ACCESS_KEY") != "" {
config["default"] = make(StrMap)
config["default"]["access_key"] = os.Getenv("AWS_ACCESS_KEY_ID")
config["default"]["secret_key"] = os.Getenv("AWS_SECRET_ACCESS_KEY")
config["default"]["region"] = os.Getenv("AWS_REGION")
config["default"]["provider"] = "aws"
}
if len(content) == 0 {
if len(config) == 0 {
fmt.Println("Can't find any configuration or ENV variables. Check https://github.com/buger/cloud-ssh for documentation.")
}
return
} else if err := yaml.Unmarshal(content, &config); err != nil {
log.Fatal(err)
}
return
}