-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
119 lines (106 loc) · 2.55 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
package piaas
import (
"fmt"
"github.com/go-yaml/yaml"
"github.com/sohoffice/piaas/util"
"io/ioutil"
"os"
)
// The sync connection target
type SyncConnection struct {
Host string
User string
Destination string
}
// Profile controls how the project can sync to remote machine.
type Profile struct {
// profile name
Name string
// The connection information used by sync module
Connection SyncConnection
// The name of ignore file, default to .piaasignore
IgnoreFile string
}
type App struct {
Name string
Cmd string
Params []string
}
type Executable struct {
Cmd string
Params []string
}
type Config struct {
ApiVersion string
Executable
Profiles []Profile
Apps []App
}
func ReadConfig(file string) Config {
var config = Config{
Executable: Executable{
Cmd: "rsync",
Params: []string{},
},
}
data, err := ioutil.ReadFile(file)
if err != nil {
println("Error reading config file:", file)
os.Exit(1)
}
err = yaml.Unmarshal(data, &config)
util.CheckFatal("read yaml", err)
err = validateProfile(&config)
util.CheckFatal("validate config yaml", err)
return config
}
func validateProfile(configPtr *Config) error {
for i := range configPtr.Profiles {
prof := &configPtr.Profiles[i]
if prof.IgnoreFile == "" {
prof.IgnoreFile = ".piaasignore"
}
switch {
case prof.Connection == SyncConnection{}:
return fmt.Errorf("profile #%d (0 based) has no connection", i)
case prof.Name == "":
return fmt.Errorf("profile #%d (0 based) without name", i)
}
}
return nil
}
func (conf *Config) GetProfile(name string) (Profile, error) {
for _, p := range conf.Profiles {
if p.Name == name {
return p, nil
}
}
return Profile{}, fmt.Errorf("can not find profile named '%s'", name)
}
// Get app by name.
// Returned the only app, if name is empty string.
//
// Return error, if the app of the name can not be found. Or name is empty string but there're more than one app.
//
func (conf *Config) GetApp(name string) (App, error) {
if name == "" && len(conf.Apps) == 1 {
return conf.Apps[0], nil
} else {
for _, app := range conf.Apps {
if app.Name == name {
return app, nil
}
}
}
return App{}, fmt.Errorf("can not find app %s", name)
}
// Find matching profile and use the information to compose the rsync target string.
// In this format: ${user}@${host}:${target}
func (conf *Config) GetSyncTarget(name string) (string, error) {
prof, err := conf.GetProfile(name)
if err != nil {
return "", err
}
c := prof.Connection
return fmt.Sprintf("%s@%s:%s", c.User, c.Host, c.Destination), nil
}