/
config.go
72 lines (64 loc) · 1.48 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
package dsdl
import (
"crypto/rand"
"encoding/json"
"fmt"
"io/ioutil"
"strings"
)
// Config is a set of targets
type Config struct {
Targets map[string]*Target
}
// Target is a combination of an alias name to deploy,
// a provider service,
// and a list of glob patterns
type Target struct {
Name string `json:"-"`
Service string
Patterns []string
}
// AddTarget loads the config from the default path, adds the new target, and saves the new config file
func AddTarget(target Target) error {
conf, _ := LoadConfig()
conf.Targets[target.Name] = &target
return SaveConfig(conf)
}
func (t Target) String() string {
var patterns []string
for _, p := range t.Patterns {
patterns = append(patterns, `"`+p+`"`)
}
return fmt.Sprintf("\"%s\" (%s) {%s}", t.Name, t.Service, strings.Join(patterns, ", "))
}
// LoadConfig loads the config from the default path
func LoadConfig() (Config, error) {
var conf Config
conf.Targets = make(map[string]*Target)
buffer, err := ioutil.ReadFile(".dsd.json")
if err != nil {
return conf, err
}
err = json.Unmarshal(buffer, &conf)
for k := range conf.Targets {
conf.Targets[k].Name = k
}
if err != nil {
return conf, err
}
return conf, nil
}
// SaveConfig saves conf on the default path
func SaveConfig(conf Config) error {
buffer, err := json.MarshalIndent(conf, "", "\t")
if err != nil {
return err
}
ioutil.WriteFile(".dsd.json", buffer, 0660)
return nil
}
func uid() []byte {
buff := make([]byte, 8)
rand.Read(buff)
return buff
}