-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
66 lines (57 loc) · 1.32 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
package main
import (
"flag"
"io/ioutil"
"os"
"path"
"gopkg.in/yaml.v1"
)
const (
CONFIG_SUBDIR = "mcmd"
DEFAULT_KNOWN_HOSTS_FILE = "$HOME/.ssh/known_hosts"
)
type HostConfig struct {
User string
Privatekey string
Hosts []string
}
func loadConfig() HostConfig {
rawYaml := readHostfile()
var result HostConfig
err := yaml.Unmarshal(rawYaml, &result)
if err != nil {
errLogger.Fatalln("Error parsing hostfile:", err)
}
return result
}
func readHostfile() []byte {
configFileParam := flag.Arg(0)
for _, file := range getConfigLocationCandidates(configFileParam) {
if exists(file) {
return getContents(file)
}
}
errLogger.Fatalf("hostfile %s not found", configFileParam)
return nil
}
func getConfigLocationCandidates(configFileParam string) []string {
configRoot, err := os.UserConfigDir()
if err != nil {
errLogger.Fatalf("no user config dir found")
}
configDir := path.Join(configRoot, CONFIG_SUBDIR)
return []string{configFileParam,
path.Join(configDir, configFileParam+".yml"),
path.Join(configDir, configFileParam+".yaml")}
}
func exists(filename string) bool {
_, err := os.Stat(filename)
return !os.IsNotExist(err)
}
func getContents(filename string) []byte {
fileContents, err := ioutil.ReadFile(filename)
if err != nil {
errLogger.Fatalln(err)
}
return fileContents
}