-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.go
More file actions
53 lines (45 loc) · 997 Bytes
/
utils.go
File metadata and controls
53 lines (45 loc) · 997 Bytes
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
package main
import (
"bufio"
"encoding/json"
"io/ioutil"
"os"
)
var Config *Conf
type Conf struct {
Addr string `json:"addr"`
User string `json:"user"`
SSHDirPath string `json:"ssh_dir_path"`
Password string `json:"password"`
PrivateKey string `json:"private_key"`
CommandChainPath string `json:"command_chain_path"`
Commands []string
}
func ReadConfig(path string) (*Conf, error) {
config := new(Conf)
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
err = json.Unmarshal(data, config)
if err != nil {
return nil, err
}
file, err := os.Open(config.CommandChainPath)
if err != nil {
return nil, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
var cmds []string
for scanner.Scan() {
cmds = append(cmds, scanner.Text())
}
config.Commands = cmds
return config, nil
}
func init() {
configPath := "./conf.json"
Config, _ = ReadConfig(configPath)
}