...
1 package api
2
3 import (
4 "sort"
5 "strings"
6
7 "golang.org/x/text/cases"
8 "golang.org/x/text/language"
9 )
10
11 type RunningMode string
12 type RunningModes map[RunningMode]bool
13
14 const (
15 RunningModeDev RunningMode = "dev"
16 RunningModeDeploy RunningMode = "deploy"
17 )
18
19 func NewRunningModes() RunningModes {
20 return RunningModes{
21 RunningModeDev: false,
22 RunningModeDeploy: false,
23 }
24 }
25
26
27 func (o RunningModes) AddRunningMode(mode RunningMode) {
28 o[mode] = true
29 }
30
31 func (o RunningModes) String() string {
32 strs := make([]string, 0, len(o))
33 caser := cases.Title(language.Und)
34 for s, v := range o {
35 if v {
36 strs = append(strs, caser.String(string(s)))
37 }
38 }
39 if len(strs) == 0 {
40 return "None"
41 }
42 sort.Sort(sort.Reverse(sort.StringSlice(strs)))
43 return strings.Join(strs, ", ")
44 }
45
46
47 func MergeRunningModes(m map[string]RunningModes) RunningModes {
48 if m == nil {
49 return nil
50 }
51
52 rm := NewRunningModes()
53
54 getMergedValueForMode := func(runningMode RunningMode) bool {
55 for _, modeMap := range m {
56 for mode, val := range modeMap {
57 if mode != runningMode {
58 continue
59 }
60 if val {
61 return val
62 }
63 }
64 }
65 return false
66 }
67
68
69 if getMergedValueForMode(RunningModeDev) {
70 rm.AddRunningMode(RunningModeDev)
71 }
72
73
74 if getMergedValueForMode(RunningModeDeploy) {
75 rm.AddRunningMode(RunningModeDeploy)
76 }
77
78 return rm
79 }
80
View as plain text