-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
128 lines (113 loc) · 2.27 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
package main
import (
"encoding/json"
"log"
"io/ioutil"
"path/filepath"
"github.com/blang/semver"
"gopkg.in/ini.v1"
)
type config struct {
SerialDir string `json:"boxidDir"`
HostFile string `json:"hostsFile"`
LocalPlaybookDir string `json:"localPlaybookDir"`
RemotePlaybookDir string `json:"remotePlaybookDir"`
hosts map[string]hostProp
plays playList
}
type hostProp struct {
Name string
Serial string
version string
}
type playList []string
func (p playList) Len() int { return len(p) }
func (p playList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p playList) Less(i, j int) bool {
vi, err := semver.Make(p[i])
if err != nil {
log.Fatal(err)
}
vj, err := semver.Make(p[j])
if err != nil {
log.Fatal(err)
}
return vi.LT(vj)
}
func newConfig(src []byte) (*config, error) {
c := &config{hosts: make(map[string]hostProp)}
err := json.Unmarshal(src, c)
if err != nil {
return nil, err
}
err = c.loadHosts()
if err != nil {
return nil, err
}
err = c.loadPlays()
if err != nil {
return nil, err
}
return c, nil
}
func (c *config) loadHosts() error {
b, err := ioutil.ReadFile(c.HostFile)
if err != nil {
return err
}
return c.load(b)
}
func (c *config) loadPlays() error {
dirs, err := ioutil.ReadDir(c.LocalPlaybookDir)
if err != nil {
return err
}
var p playList
for _, dir := range dirs {
if dir.IsDir() {
b := filepath.Base(dir.Name())
// _, err = semver.Make(b)
// if err != nil {
// return err
// }
p = append(p, b)
}
}
// sort.Sort(p)
c.plays = p
return nil
}
func (c *config) hasPlay(ver string) bool {
for _, v := range c.plays {
if v == ver {
return true
}
}
// if ver == "latest" && len(c.plays) > 0 {
// return true
// }
return false
}
// func (c *config) latestPlay() string {
// return c.plays[len(c.plays)-1]
// }
func (c *config) load(b []byte) error {
f, err := ini.Load(b)
if err != nil {
return err
}
for _, n := range f.SectionStrings() {
s, err := f.GetSection(n)
if err != nil {
return err
}
for _, kn := range s.KeyStrings() {
c.hosts[kn] = hostProp{Name: kn, version: n, Serial: s.Key(kn).String()}
}
}
return nil
}
func (c *config) Get(host string) (hostProp, bool) {
v, ok := c.hosts[host]
return v, ok
}