/
functions.go
152 lines (128 loc) · 3.91 KB
/
functions.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
package config
import (
"encoding/base64"
"errors"
"fmt"
"os"
"strings"
"github.com/dokku/dokku/plugins/common"
)
func export(appName string, merged bool, format string) error {
env := getEnvironment(appName, merged)
exportType := ExportFormatExports
suffix := "\n"
exportTypes := map[string]ExportFormat{
"docker-args": ExportFormatDockerArgs,
"docker-args-keys": ExportFormatDockerArgsKeys,
"envfile": ExportFormatEnvfile,
"exports": ExportFormatExports,
"json": ExportFormatJSON,
"json-list": ExportFormatJSONList,
"pack-keys": ExportFormatPackArgKeys,
"pretty": ExportFormatPretty,
"shell": ExportFormatShell,
}
exportType, ok := exportTypes[format]
if !ok {
return fmt.Errorf("Unknown export format: %v", format)
}
if exportType == ExportFormatShell {
suffix = " "
}
exported := env.Export(exportType)
fmt.Print(exported + suffix)
return nil
}
// SubBundle implements the logic for config:bundle without app name validation
func SubBundle(appName string, merged bool) error {
env := getEnvironment(appName, merged)
return env.ExportBundle(os.Stdout)
}
// SubClear implements the logic for config:clear without app name validation
func SubClear(appName string, noRestart bool) error {
return UnsetAll(appName, !noRestart)
}
// SubExport implements the logic for config:export without app name validation
func SubExport(appName string, merged bool, format string) error {
return export(appName, merged, format)
}
// SubGet implements the logic for config:get without app name validation
func SubGet(appName string, keys []string, quoted bool) error {
if len(keys) == 0 {
return errors.New("Expected: key")
}
if len(keys) != 1 {
return fmt.Errorf("Unexpected argument(s): %v", keys[1:])
}
value, ok := Get(appName, keys[0])
if !ok {
os.Exit(1)
return nil
}
if quoted {
fmt.Printf("'%s'\n", singleQuoteEscape(value))
} else {
fmt.Printf("%s\n", value)
}
return nil
}
// SubKeys implements the logic for config:keys without app name validation
func SubKeys(appName string, merged bool) error {
env := getEnvironment(appName, merged)
for _, k := range env.Keys() {
fmt.Println(k)
}
return nil
}
// SubSet implements the logic for config:set without app name validation
func SubSet(appName string, pairs []string, noRestart bool, encoded bool) error {
if len(pairs) == 0 {
return errors.New("At least one env pair must be given")
}
updated := make(map[string]string)
for _, e := range pairs {
parts := strings.SplitN(e, "=", 2)
if len(parts) == 1 {
return fmt.Errorf("Invalid env pair: %v", e)
}
key, value := parts[0], parts[1]
if encoded {
decoded, err := base64.StdEncoding.DecodeString(value)
if err != nil {
return fmt.Errorf("%s for key '%s'", err.Error(), key)
}
value = string(decoded)
}
updated[key] = value
}
return SetMany(appName, updated, !noRestart)
}
// SubShow implements the logic for config:show without app name validation
func SubShow(appName string, merged bool, shell bool, export bool) error {
env := getEnvironment(appName, merged)
if shell && export {
return errors.New("Only one of --shell and --export can be given")
}
if shell {
common.LogWarn("Deprecated: Use 'config:export --format shell' instead")
fmt.Print(env.Export(ExportFormatShell))
} else if export {
common.LogWarn("Deprecated: Use 'config:export --format exports' instead")
fmt.Println(env.Export(ExportFormatExports))
} else {
contextName := "global"
if appName != "" {
contextName = appName
}
common.LogInfo2Quiet(contextName + " env vars")
fmt.Println(env.Export(ExportFormatPretty))
}
return nil
}
// SubUnset implements the logic for config:unset without app name validation
func SubUnset(appName string, keys []string, noRestart bool) error {
if len(keys) == 0 {
return fmt.Errorf("At least one key must be given")
}
return UnsetMany(appName, keys, !noRestart)
}