-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.go
277 lines (258 loc) · 6.82 KB
/
app.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package sellsword
import (
"errors"
"fmt"
"github.com/fatih/color"
"gopkg.in/yaml.v2"
"io/ioutil"
"os"
"os/exec"
"path"
"sort"
"strings"
)
type App struct {
Name string
EnvType string `yaml:"type"`
Path string
Root string
Target string
Definition string
Variables []string
VariableNames []string
ExportVariables map[string]string
LoadAction string `yaml:"load"`
UnloadAction string `yaml:"unload"`
}
// NewApp is the constructor for New Apps
func NewApp(name string, sswHome string) (*App, error) {
a := new(App)
a.Name = name
a.Definition = path.Join(sswHome, "config", name+".ssw")
a.Path = path.Join(sswHome, name)
Logger.Debugf("Parsing application found at %s", a.Path)
if data, err := ioutil.ReadFile(a.Definition); err != nil {
Logger.Errorln(err.Error())
return a, err
} else {
if err := yaml.Unmarshal(data, a); err != nil {
return a, err
}
if a.EnvType == "directory" {
Logger.Debugf("Target for %s is currently %s", a.Name, a.Target)
if newTarget, err := expandPath(a.Target); err != nil {
Logger.Debugf(err.Error())
return a, err
} else {
Logger.Debugf("New target for %s is %s", a.Name, newTarget)
a.Target = newTarget
return a, err
}
} else {
if err := a.ParseExportVars(); err != nil {
return a, err
}
}
return a, nil
}
return a, nil
}
func (a *App) ParseExportVars() error {
a.VariableNames = make([]string, 0)
a.ExportVariables = make(map[string]string, len(a.Variables))
for i := range a.Variables {
keyValue := strings.Split(a.Variables[i], "=")
a.VariableNames = appendIfMissing(a.VariableNames, keyValue[0])
a.ExportVariables[keyValue[1]] = keyValue[0]
}
return nil
}
func (a *App) Current() (*Env, error) {
var e *Env
if realPath, err := resolveSymlink(path.Join(a.Path, "current")); err != nil {
return e, err
} else {
envName := path.Base(realPath)
if a.EnvType == "environment" {
return NewEnvironmentEnv(envName, a.Path, a.ExportVariables, a.VariableNames)
} else {
return NewDirectoryEnv(envName, a.Path)
}
}
}
func (a *App) ListEnvs() []*Env {
envs := make([]*Env, 0)
di, _ := ioutil.ReadDir(a.Path)
for i := range di {
name := di[i].Name()
if name != "current" {
var e *Env
if a.EnvType == "environment" {
e, _ = NewEnvironmentEnv(name, a.Path, a.ExportVariables, a.VariableNames)
} else {
e, _ = NewDirectoryEnv(name, a.Path)
}
envs = append(envs, e)
}
}
return envs
}
func (a *App) runAction(actionName string) error {
var action string
if current, err := a.Current(); err != nil {
return err
} else {
currentPath := current.Path
if actionName == "load" {
action = a.LoadAction
} else if actionName == "unload" {
Logger.Debugf("Unload action is %s\n", a.UnloadAction)
action = a.UnloadAction
} else {
return errors.New("Only actions load and unload are valid.")
}
shell := os.Getenv("SHELL")
cmd := exec.Command(shell, "-c", action)
envVar := fmt.Sprintf("SSW_CURRENT=%s", currentPath)
cmd.Env = append(os.Environ(), envVar)
return cmd.Run()
}
}
func (a *App) Load() error {
if err := a.runAction("load"); err != nil {
return err
} else {
if a.EnvType == "environment" {
Logger.Debugf("Exporting environment variables for application %s\n", a.Name)
if env, err := a.Current(); err == nil {
env.Load()
return nil
} else {
Logger.Error(err.Error())
return err
}
} else {
Logger.Debugf("Application %s has no environment variables to export, nothing to do\n", a.Name)
return nil
}
}
}
func (a *App) Unload() error {
if err := a.runAction("unload"); err != nil {
return err
} else {
if a.EnvType == "environment" {
Logger.Debugf("Exporting environment variables for application %s\n", a.Name)
a.UnsetExportVars()
if env, err := a.Current(); err == nil {
env.Load()
return nil
} else {
Logger.Error(err.Error())
return err
}
} else {
Logger.Debugf("Application %s has no environment variables to export, nothing to do\n", a.Name)
return nil
}
}
}
func (a *App) MakeCurrent(envName string) error {
red := GetTermPrinterF(color.FgRed)
envPath := path.Join(a.Path, envName)
currentEnv, currentErr := a.Current()
Logger.Debugf("Current env is %s", currentEnv.Name)
if _, err := os.Stat(envPath); os.IsNotExist(err) {
Logger.Error(err.Error())
return err
} else if currentErr == nil && envName == currentEnv.Name {
Logger.Warn(red("%s is already set as the default environment for application %s. Nothing to do.",
envName, a.Name))
return nil
} else {
var newEnv *Env
if a.EnvType == "environment" {
newEnv, err = NewEnvironmentEnv(envName, a.Path, a.ExportVariables, a.VariableNames)
newEnv.PopulateExportVars()
a.Unload()
newEnv.PrintExports()
} else {
newEnv, err = NewDirectoryEnv(envName, a.Path)
}
Logger.Debugf("Unloading %s", a.Name)
if err := a.Unload(); err != nil {
Logger.Debugf("Unloading hit error %s\n", err.Error())
return err
} else {
Logger.Debugf("Unlinking %s", currentEnv)
if err := a.Unlink(); err != nil {
Logger.Debugf("Encountered error when unlinking current for %s", a.Name)
return err
} else {
if err := a.Link(newEnv.Name); err != nil {
return err
} else {
return a.Load()
}
}
}
}
}
func (a *App) EnumerateExportVars() []string {
vars := make([]string, len(a.ExportVariables))
i := 0
for k, _ := range a.ExportVariables {
vars[i] = k
i++
}
return vars
}
func (a *App) MakeUnsetExportVars() string {
vars := a.EnumerateExportVars()
statements := make([]string, 0)
for i := range vars {
statements = append(statements, fmt.Sprintf("unset %s", vars[i]))
}
sort.Strings(statements)
return strings.Join(statements, "\n")
}
func (a *App) UnsetExportVars() {
fmt.Printf(a.MakeUnsetExportVars())
}
func (a *App) Unlink() error {
current := path.Join(a.Path, "current")
if _, err := os.Lstat(current); os.IsNotExist(err) {
Logger.Debugf("Current symlink %s does not exist, nothing to do", current)
return nil
} else {
if a.EnvType == "directory" {
Logger.Debugf("Removing Target symlink for %s at %s", a.Name, a.Target)
if err := os.Remove(a.Target); err != nil {
return err
}
}
return os.Remove(path.Join(a.Path, "current"))
}
}
func (a *App) Link(envName string) error {
source := path.Join(a.Path, envName)
target := path.Join(a.Path, "current")
if err := os.Symlink(source, target); err != nil {
Logger.Debugf(err.Error())
return err
}
if a.EnvType == "directory" {
if err := os.Symlink(source, a.Target); err != nil {
Logger.Debugf(err.Error())
return err
}
}
return nil
}
func (a *App) NewEnv(envName string) (*Env, error) {
if a.EnvType == "environment" {
return NewEnvironmentEnv(envName, a.Path, a.ExportVariables, a.VariableNames)
} else {
return NewDirectoryEnv(envName, a.Path)
}
}