-
Notifications
You must be signed in to change notification settings - Fork 14
/
config.go
168 lines (147 loc) · 3.1 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"regexp"
"strings"
)
func editConfig() error {
f, err := getConfigPath("hosts")
if err != nil {
return err
}
fname := f.Name()
writeExample(f)
defer f.Close()
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "vi"
}
cmd := exec.Command(editor, fname)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
return errors.New("Editor " + editor + " not found. Please update your $EDITOR environment variable")
}
err = cmd.Wait()
return err
}
func readHostConfig(group string) ([]string, error) {
var hosts []string
f, err := getConfigPath("hosts")
if err != nil {
return hosts, err
}
defer f.Close()
hosts, err = parseConfig(readFile(f), group)
if err != nil {
return hosts, err
}
if len(hosts) == 0 {
f.Close()
editConfig()
f, _ := getConfigPath("hosts")
hosts, err = parseConfig(readFile(f), group)
f.Close()
if err != nil {
return hosts, err
}
if len(hosts) == 0 {
return hosts, errors.New("Servers config can't be empty")
}
}
return hosts, err
}
var example = `# Put host config to be line separated. example:
#
# or you can group it. example:
# [server1]
#
# [other-server]
`
func writeExample(f *os.File) {
b, _ := ioutil.ReadAll(f)
if len(b) == 0 {
w := bufio.NewWriter(f)
_, err := w.WriteString(example)
err = w.Flush()
if err != nil {
log.Println(err)
}
}
}
var groupRegex = regexp.MustCompile(`\[([\w\-]+)]`)
func readFile(f *os.File) string {
byt, err := ioutil.ReadAll(f)
if err != nil {
log.Fatal(err)
}
return string(byt)
}
func parseConfig(rawConfig, groupConfig string) ([]string, error) {
var hosts []string
var groups = make(map[string][]string)
var currentGroup string
lines := strings.Split(rawConfig, "\n")
lines = trimLines(lines)
for _, l := range lines {
if bt := groupRegex.Find([]byte(l)); len(bt) > 0 {
group := groupRegex.ReplaceAllString(l, "$1") // remove [ and ]
currentGroup = group
continue
}
if currentGroup != "" {
groups[currentGroup] = append(groups[currentGroup], l)
continue
}
hosts = append(hosts, l)
}
if groupConfig != "" {
if hosts, ok := groups[groupConfig]; ok {
return hosts, nil
}
return nil, fmt.Errorf("Group '%s' not found", groupConfig)
}
if len(groups) > 0 {
return nil, fmt.Errorf("Config use group mode but no group specified")
}
return hosts, nil
}
func getConfigPath(file string) (*os.File, error) {
path := os.Getenv("HOME")
path += "/.config/rexec/"
err := os.MkdirAll(path, 0776)
if err != nil {
return nil, err
}
fpath := path + file
f, err := os.OpenFile(fpath, os.O_RDWR, os.ModeDevice)
if os.IsNotExist(err) {
f, err = os.Create(fpath)
}
return f, err
}
func trimLines(lines []string) []string {
newlines := []string{}
for _, l := range lines {
l = strings.TrimSpace(l)
if l == "" || strings.HasPrefix(l, "#") {
continue
}
newlines = append(newlines, l)
}
return newlines
}