/
config.go
118 lines (100 loc) · 2.96 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
package config
import (
"os"
"github.com/drodil/envssh/util"
)
// EnvVariables that should be set on remote.
type EnvVariables struct {
Static map[string]string `yaml:"static"`
Moved []string `yaml:"moved"`
}
// File presents a single file that is moved to remote.
type File struct {
Local string `yaml:"local"`
Remote string `yaml:"remote"`
}
// ServerConfig presents a single server configuration
// identified by the host name.
type ServerConfig struct {
Host string `yaml:"host"`
Port uint16 `yaml:"port"`
Aliases []string `yaml:"aliases"`
Env EnvVariables `yaml:"env"`
Files []File `yaml:"files"`
Commands []string `yaml:"commands"`
}
// GlobalConfig is base for all server configurations
// and is used for all remotes.
type GlobalConfig struct {
Env EnvVariables `yaml:"env"`
Files []File `yaml:"files"`
Commands []string `yaml:"commands"`
}
// Config contains global and server specific configurations.
type Config struct {
Global GlobalConfig `yaml:"global"`
Servers []ServerConfig `yaml:"servers"`
}
func aliasMatches(host string, config *ServerConfig) bool {
for _, alias := range config.Aliases {
if host == alias {
return true
}
}
return false
}
// GetServerConfig returns server specific config based on hostname if it exists in the Config struct.
func (config *Config) GetServerConfig(remote *util.Remote) *ServerConfig {
for _, conf := range config.Servers {
if conf.Host == remote.Hostname || aliasMatches(remote.Hostname, &conf) {
return &conf
}
}
return nil
}
// GetCommandsForRemote returns a list of commands to run on remote.
func (config *Config) GetCommandsForRemote(remote *util.Remote) []string {
ret := make([]string, len(config.Global.Commands))
copy(ret, config.Global.Commands)
serverConf := config.GetServerConfig(remote)
if serverConf != nil {
ret = append(ret, serverConf.Commands...)
}
return ret
}
// GetFilesForRemote returns a list of files that are transferred to remote.
func (config *Config) GetFilesForRemote(remote *util.Remote) []File {
ret := make([]File, len(config.Global.Files))
copy(ret, config.Global.Files)
serverConf := config.GetServerConfig(remote)
if serverConf != nil {
ret = append(ret, serverConf.Files...)
}
return ret
}
// GetEnvironmentVariablesForRemote returns a list of environment variables that are set to remote.
func (config *Config) GetEnvironmentVariablesForRemote(remote *util.Remote) map[string]string {
ret := make(map[string]string)
for name, value := range config.Global.Env.Static {
ret[name] = value
}
for _, name := range config.Global.Env.Moved {
value, ok := os.LookupEnv(name)
if ok {
ret[name] = value
}
}
serverConf := config.GetServerConfig(remote)
if serverConf != nil {
for name, value := range serverConf.Env.Static {
ret[name] = value
}
for _, name := range serverConf.Env.Moved {
value, ok := os.LookupEnv(name)
if ok {
ret[name] = value
}
}
}
return ret
}