/
env.go
107 lines (103 loc) · 2.79 KB
/
env.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
package deplactive
import (
"fmt"
"io"
"os"
"strings"
"github.com/containerum/chkit/pkg/model/container"
"github.com/containerum/chkit/pkg/util/activekit"
"github.com/containerum/chkit/pkg/util/text"
"github.com/containerum/kube-client/pkg/model"
)
func componentEnvs(cont *container.Container) activekit.MenuItems {
var items = make(activekit.MenuItems, 0, len(cont.Env))
for _, env := range cont.Env {
items = items.Append(componentEditEnv(&env))
}
return items.Append(&activekit.MenuItem{
Label: "Add env",
Action: func() error {
var env model.Env
if componentEditEnv(&env).Action() == nil {
cont.Env = append(cont.Env, env)
}
return nil
},
})
}
func componentEditEnv(oldEnv *model.Env) *activekit.MenuItem {
var env = *oldEnv
return &activekit.MenuItem{
Label: fmt.Sprintf("Edit env %s:%q", oldEnv.Name, text.Crop(oldEnv.Value, 64)),
Action: func() error {
for exit := false; !exit; {
_, err := (&activekit.Menu{
Title: "Deployment -> Container -> Env",
Items: activekit.MenuItems{
{
Label: fmt.Sprintf("Edit name : %s",
activekit.OrString(env.Name, "undefined, required")),
Action: func() error {
var name = activekit.Promt("Type env name (hit Enter to leave %s, if has '$' prefix\n"+
"then try to get value from host env, escape '\\$' to use strictly): ",
activekit.OrString(env.Name, "empty"))
name = strings.TrimSpace(name)
if name != "" {
switch {
case strings.HasPrefix(name, "$"):
env.Name = strings.TrimPrefix(name, "$")
env.Value = os.Getenv(env.Name)
case strings.HasPrefix(name, "\\$"):
env.Name = strings.TrimPrefix(name, "\\")
default:
env.Name = name
}
}
return nil
},
},
{
Label: fmt.Sprintf("Edit value : %q",
activekit.OrString(env.Value, "undefined, required")),
Action: func() error {
var value = activekit.Promt("Type env value (hit Enter to leave %s): ",
activekit.OrString(env.Value, "empty"))
value = strings.TrimSpace(value)
if value != "" {
env.Value = value
}
return nil
},
},
{
Label: "Confirm",
Action: func() error {
if env.Name == "" {
fmt.Println("name can't be empty!")
}
if env.Value == "" {
fmt.Println("value can't be empty!")
}
if env.Name != "" && env.Value != "" {
*oldEnv = env
exit = true
}
return nil
},
},
{
Label: "Drop all changes and return to previous menu",
Action: func() error {
return io.EOF
},
},
},
}).Run()
if err != nil {
return err
}
}
return nil
},
}
}