/
config.go
95 lines (80 loc) · 1.73 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
package main
import (
"errors"
"io/ioutil"
"os"
"github.com/BurntSushi/toml"
)
var path = ".dm.toml"
type DeploymentConfig struct {
Deployments map[string]Deployment
}
type Deployment struct {
Id string
Project string
Config string
Vars string
Fingerprint string
Commit string
Deployed string
Updated string
Branch string
}
func ReadDeploymentConfig() (DeploymentConfig, error) {
f, err := os.Open(path)
if err != nil {
return DeploymentConfig{}, err
}
defer f.Close()
buf, err := ioutil.ReadAll(f)
if err != nil {
return DeploymentConfig{}, err
}
var config DeploymentConfig
if _, err := toml.Decode(string(buf), &config); err != nil {
return DeploymentConfig{}, err
}
return config, nil
}
func AppendDeployment(d Deployment, initIfMissing bool) (DeploymentConfig, error) {
f, err := os.Open(path)
if err != nil && initIfMissing {
f, err = os.Create(path)
}
f.Close()
config, err := ReadDeploymentConfig()
if err != nil {
return DeploymentConfig{}, err
}
if config.Deployments == nil {
config.Deployments = make(map[string]Deployment)
}
config.Deployments[d.Id] = d
f, _ = os.Create(path)
encoder := toml.NewEncoder(f)
err = encoder.Encode(config)
if err != nil {
return DeploymentConfig{}, err
}
return config, nil
}
func RemoveDeployment(id string) error {
config, err := ReadDeploymentConfig()
if err != nil {
return err
}
if _, ok := config.Deployments[id]; ok == false {
return errors.New("Deployment not found")
}
delete(config.Deployments, id)
if len(config.Deployments) == 0 {
return os.Remove(path)
}
f, _ := os.Create(path)
encoder := toml.NewEncoder(f)
err = encoder.Encode(config)
if err != nil {
return err
}
return nil
}