-
Notifications
You must be signed in to change notification settings - Fork 0
/
conf.go
216 lines (178 loc) · 4.63 KB
/
conf.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package main
/*
Application to start multiple ssh or alike connections to servers in
a separate tmux window.
The MIT License (MIT)
Copyright (c) 2016 Nelo-T. wallus <nelo@wallus.de>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import (
"errors"
"io/ioutil"
"os"
"regexp"
"sort"
"gopkg.in/yaml.v2"
)
var (
// ErrNoValidConfigPath is returned by confPath when none of the
// default locations are accessible.
ErrNoValidConfigPath = errors.New("No configuration path found.")
)
type config struct {
Layout string
Layouts map[string]string
Hosts []string
Title string
}
func newConfig(init config) config {
cfg := config{
Layout: "5",
Layouts: map[string]string{
"1": "even-horizontal",
"2": "even-vertical",
"3": "main-horizontal",
"4": "main-vertical",
"5": "tiled",
},
Hosts: []string{},
Title: "",
}
if init.Layout != "" {
cfg.Layout = init.Layout
}
if init.Title != "" {
cfg.Title = init.Title
}
// TODO ugly
for i := range init.Hosts {
cfg.Hosts = append(cfg.Hosts, init.Hosts[i])
}
for key := range init.Layouts {
cfg.Layouts[key] = init.Layouts[key]
}
return cfg
}
// Parse given filepath into config struct. If the path is a directory
// all yaml files within will be recursively parsed.
func (c *config) parseFile(path string) error {
log.Debugf("Parsing from path '%s'", path)
fileinfo, err := os.Stat(path)
if err != nil {
return err
}
if fileinfo.IsDir() {
log.Debugf("'%s' is a directory, parsing recursively", path)
dir, err := os.Open(path)
if err != nil {
return err
}
files, err := dir.Readdir(0)
if err != nil {
return err
}
for i := range files {
err = c.parseFile(path + "/" + files[i].Name())
if err != nil {
return err
}
}
return nil
}
file, err := ioutil.ReadFile(path)
if err != nil {
return err
}
// TODO expand host with globs or similar
// host{01..05}
// to
// host01
// host02
// host03
// host04
// host05
return yaml.Unmarshal(file, c)
}
func (c *config) matchHosts(expressions []string) []string {
hosts := map[string]bool{}
// matching input against defined hosts
for _, host := range c.Hosts {
log.Debug("Checking host", host)
for _, expr := range expressions {
log.Debug("\tagainst expression", expr)
// checking for a leading minus at the beginning
if expr[0] == '-' {
isMatch, _ := regexp.MatchString(expr[1:], host)
if isMatch {
log.Debugf("Hosts '%s' matched against '%s', removing from host list", host, expr)
hosts[host] = false
}
continue
}
isMatch, _ := regexp.MatchString(expr, host)
if isMatch {
log.Debug("\t\tmatched")
hosts[host] = true
}
}
}
// transfer keys from created map to list
list := make([]string, 0)
for key, value := range hosts {
if value {
list = append(list, key)
}
}
// sort it
sort.Strings(list)
return list
}
// Retrieve a valid configuration path:
// env TCLUSTER_CONF
// ~/.tcluster.d
// ~/.tcluster.yml
// ~/.tcluster.yaml
// The first valid path is returned.
func confPath() (string, error) {
// TODO: allow multiple colon-delimited directories
path := os.ExpandEnv(os.Getenv("TCLUSTER_CONF"))
if path != "" {
_, err := os.Stat(path)
if err == nil {
log.Debugf("Configuration path: %s", path)
return path, nil
}
}
paths := []string{
"$HOME/.tcluster.d",
"$HOME/.tcluster.yml",
"$HOME/.tcluster.yaml",
}
for i := range paths {
path := os.ExpandEnv(paths[i])
log.Debugf("Trying path '%s'", path)
if path != "" {
_, err := os.Stat(path)
if err == nil {
log.Debugf("Configuration path: %s", path)
return path, nil
}
}
}
log.Debug("No configuration file found")
return "", ErrNoValidConfigPath
}