forked from monzo/orchestra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
82 lines (76 loc) · 1.76 KB
/
utils.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
package commands
import (
"fmt"
"math"
"os"
"strings"
log "github.com/cihub/seelog"
"github.com/codegangsta/cli"
"github.com/vinceprignano/orchestra/config"
"github.com/vinceprignano/orchestra/services"
)
// This is temporary, very very alpha and may change soon
func FilterServices(c *cli.Context) map[string]*services.Service {
excludeMode := 0
args := c.Args()
for _, s := range args {
serv := s
if strings.HasPrefix(s, "~") {
serv = strings.Replace(s, "~", "", 1)
}
if _, ok := services.Registry[serv]; ok {
if strings.HasPrefix(s, "~") {
excludeMode += 1
delete(services.Registry, serv)
} else {
excludeMode -= 1
}
} else {
log.Errorf("Service %s not found", s)
return nil
}
}
if math.Abs(float64(excludeMode)) != float64(len(args)) {
log.Critical("You can't exclude and include services at the same time")
os.Exit(1)
}
if excludeMode < 0 {
for name := range services.Registry {
included := false
for _, s := range args {
if name == s {
included = true
break
}
}
if !included {
delete(services.Registry, name)
}
}
}
return services.Registry
}
func ServicesBashComplete(c *cli.Context) {
for name := range services.Registry {
fmt.Println(name)
fmt.Println("~" + name)
}
}
func BeforeAfterWrapper(f func(c *cli.Context)) func(c *cli.Context) {
return func(c *cli.Context) {
err := config.GetBeforeFunc()(c)
if err != nil {
appendError(err)
}
f(c)
err = config.GetAfterFunc()(c)
if err != nil {
appendError(err)
}
}
}
// GetEnvForService returns all the environment variables for a given service
// including the ones specified in the global config
func GetEnvForService(c *cli.Context, service *services.Service) []string {
return config.GetEnvForCommand(c)
}