-
Notifications
You must be signed in to change notification settings - Fork 3
/
appset.go
73 lines (68 loc) · 1.59 KB
/
appset.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
package sellsword
import (
"fmt"
"github.com/fatih/color"
"io/ioutil"
"os"
"strings"
)
type AppSet struct {
Apps []*App
Home string
}
func NewAppSet(home string) (*AppSet, error) {
as := new(AppSet)
if _, err := os.Stat(home); os.IsNotExist(err) {
return as, err
}
as.Home = home
return as, nil
}
func (as *AppSet) FindApps(appNames ...string) error {
if _, err := os.Stat(as.Home); os.IsNotExist(err) {
red := GetTermPrinterF(color.FgRed)
Logger.Errorln(red("The Home directory that you have specified, %s, does not exist.", as.Home))
} else {
if appNames[0] == "all" {
di, _ := ioutil.ReadDir(as.Home)
for i := range di {
if di[i].Name() != "config" {
name := strings.Split(di[i].Name(), ".ssw")[0]
a, _ := NewApp(name, as.Home)
as.Apps = append(as.Apps, a)
}
}
} else {
for i := range appNames {
a, _ := NewApp(appNames[i], as.Home)
as.Apps = append(as.Apps, a)
}
}
}
return nil
}
func (as *AppSet) ListApps(appNames []string) {
if len(appNames) == 0 {
as.FindApps("all")
} else {
as.FindApps(appNames...)
}
for i := range as.Apps {
cyan := GetTermPrinter(color.FgCyan)
red := GetTermPrinter(color.FgRed)
green := GetTermPrinter(color.FgGreen)
fmt.Printf("%s:\n", cyan(as.Apps[i].Name))
current, err := as.Apps[i].Current()
if err != nil {
fmt.Printf("%s\n", red("No environment currently in use"))
} else {
fmt.Printf("\t%s\t%s\n", green(current.Name), green("CURRENT"))
}
envs := as.Apps[i].ListEnvs()
for i := range envs {
if envs[i].Name != current.Name {
fmt.Printf("\t%s\n", envs[i].Name)
}
}
}
}