This repository has been archived by the owner on Sep 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
env.go
275 lines (234 loc) · 6.72 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
package commands
import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"text/template"
"github.com/docker/machine/commands/mcndirs"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/check"
"github.com/docker/machine/libmachine/log"
"github.com/docker/machine/libmachine/shell"
)
const (
envTmpl = `{{ .Prefix }}DOCKER_TLS_VERIFY{{ .Delimiter }}{{ .DockerTLSVerify }}{{ .Suffix }}{{ .Prefix }}DOCKER_HOST{{ .Delimiter }}{{ .DockerHost }}{{ .Suffix }}{{ .Prefix }}DOCKER_CERT_PATH{{ .Delimiter }}{{ .DockerCertPath }}{{ .Suffix }}{{ .Prefix }}DOCKER_MACHINE_NAME{{ .Delimiter }}{{ .MachineName }}{{ .Suffix }}{{ if .ComposePathsVar }}{{ .Prefix }}COMPOSE_CONVERT_WINDOWS_PATHS{{ .Delimiter }}true{{ .Suffix }}{{end}}{{ if .NoProxyVar }}{{ .Prefix }}{{ .NoProxyVar }}{{ .Delimiter }}{{ .NoProxyValue }}{{ .Suffix }}{{end}}{{ .UsageHint }}`
)
var (
errImproperUnsetEnvArgs = errors.New("Error: Expected no machine name when the -u flag is present")
defaultUsageHinter UsageHintGenerator
runtimeOS = func() string { return runtime.GOOS }
)
func init() {
defaultUsageHinter = &EnvUsageHintGenerator{}
}
type ShellConfig struct {
Prefix string
Delimiter string
Suffix string
DockerCertPath string
DockerHost string
DockerTLSVerify string
UsageHint string
MachineName string
NoProxyVar string
NoProxyValue string
ComposePathsVar bool
}
func cmdEnv(c CommandLine, api libmachine.API) error {
var (
err error
shellCfg *ShellConfig
)
// Ensure that log messages always go to stderr when this command is
// being run (it is intended to be run in a subshell)
log.SetOutWriter(os.Stderr)
if c.Bool("unset") {
shellCfg, err = shellCfgUnset(c, api)
if err != nil {
return err
}
} else {
shellCfg, err = shellCfgSet(c, api)
if err != nil {
return err
}
}
return executeTemplateStdout(shellCfg)
}
func shellCfgSet(c CommandLine, api libmachine.API) (*ShellConfig, error) {
if len(c.Args()) > 1 {
return nil, ErrExpectedOneMachine
}
target, err := targetHost(c, api)
if err != nil {
return nil, err
}
host, err := api.Load(target)
if err != nil {
return nil, err
}
dockerHost, _, err := check.DefaultConnChecker.Check(host, c.Bool("swarm"))
if err != nil {
return nil, fmt.Errorf("Error checking TLS connection: %s", err)
}
userShell, err := getShell(c.String("shell"))
if err != nil {
return nil, err
}
shellCfg := &ShellConfig{
DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), host.Name),
DockerHost: dockerHost,
DockerTLSVerify: "1",
UsageHint: defaultUsageHinter.GenerateUsageHint(userShell, os.Args),
MachineName: host.Name,
}
if c.Bool("no-proxy") {
ip, err := host.Driver.GetIP()
if err != nil {
return nil, fmt.Errorf("Error getting host IP: %s", err)
}
noProxyVar, noProxyValue := findNoProxyFromEnv()
// add the docker host to the no_proxy list idempotently
switch {
case noProxyValue == "":
noProxyValue = ip
case strings.Contains(noProxyValue, ip):
//ip already in no_proxy list, nothing to do
default:
noProxyValue = fmt.Sprintf("%s,%s", noProxyValue, ip)
}
shellCfg.NoProxyVar = noProxyVar
shellCfg.NoProxyValue = noProxyValue
}
if runtimeOS() == "windows" {
shellCfg.ComposePathsVar = true
}
switch userShell {
case "fish":
shellCfg.Prefix = "set -gx "
shellCfg.Suffix = "\";\n"
shellCfg.Delimiter = " \""
case "powershell":
shellCfg.Prefix = "$Env:"
shellCfg.Suffix = "\"\n"
shellCfg.Delimiter = " = \""
case "cmd":
shellCfg.Prefix = "SET "
shellCfg.Suffix = "\n"
shellCfg.Delimiter = "="
case "tcsh":
shellCfg.Prefix = "setenv "
shellCfg.Suffix = "\";\n"
shellCfg.Delimiter = " \""
case "emacs":
shellCfg.Prefix = "(setenv \""
shellCfg.Suffix = "\")\n"
shellCfg.Delimiter = "\" \""
default:
shellCfg.Prefix = "export "
shellCfg.Suffix = "\"\n"
shellCfg.Delimiter = "=\""
}
return shellCfg, nil
}
func shellCfgUnset(c CommandLine, api libmachine.API) (*ShellConfig, error) {
if len(c.Args()) != 0 {
return nil, errImproperUnsetEnvArgs
}
userShell, err := getShell(c.String("shell"))
if err != nil {
return nil, err
}
shellCfg := &ShellConfig{
UsageHint: defaultUsageHinter.GenerateUsageHint(userShell, os.Args),
}
if c.Bool("no-proxy") {
shellCfg.NoProxyVar, shellCfg.NoProxyValue = findNoProxyFromEnv()
}
switch userShell {
case "fish":
shellCfg.Prefix = "set -e "
shellCfg.Suffix = ";\n"
shellCfg.Delimiter = ""
case "powershell":
shellCfg.Prefix = `Remove-Item Env:\\`
shellCfg.Suffix = "\n"
shellCfg.Delimiter = ""
case "cmd":
shellCfg.Prefix = "SET "
shellCfg.Suffix = "\n"
shellCfg.Delimiter = "="
case "emacs":
shellCfg.Prefix = "(setenv \""
shellCfg.Suffix = ")\n"
shellCfg.Delimiter = "\" nil"
case "tcsh":
shellCfg.Prefix = "unsetenv "
shellCfg.Suffix = ";\n"
shellCfg.Delimiter = ""
default:
shellCfg.Prefix = "unset "
shellCfg.Suffix = "\n"
shellCfg.Delimiter = ""
}
return shellCfg, nil
}
func executeTemplateStdout(shellCfg *ShellConfig) error {
t := template.New("envConfig")
tmpl, err := t.Parse(envTmpl)
if err != nil {
return err
}
return tmpl.Execute(os.Stdout, shellCfg)
}
func getShell(userShell string) (string, error) {
if userShell != "" {
return userShell, nil
}
return shell.Detect()
}
func findNoProxyFromEnv() (string, string) {
// first check for an existing lower case no_proxy var
noProxyVar := "no_proxy"
noProxyValue := os.Getenv("no_proxy")
// otherwise default to allcaps HTTP_PROXY
if noProxyValue == "" {
noProxyVar = "NO_PROXY"
noProxyValue = os.Getenv("NO_PROXY")
}
return noProxyVar, noProxyValue
}
type UsageHintGenerator interface {
GenerateUsageHint(string, []string) string
}
type EnvUsageHintGenerator struct{}
func (g *EnvUsageHintGenerator) GenerateUsageHint(userShell string, args []string) string {
cmd := ""
comment := "#"
dockerMachinePath := args[0]
if strings.Contains(dockerMachinePath, " ") || strings.Contains(dockerMachinePath, `\`) {
args[0] = fmt.Sprintf("\"%s\"", dockerMachinePath)
}
commandLine := strings.Join(args, " ")
switch userShell {
case "fish":
cmd = fmt.Sprintf("eval (%s)", commandLine)
case "powershell":
cmd = fmt.Sprintf("& %s | Invoke-Expression", commandLine)
case "cmd":
cmd = fmt.Sprintf("\t@FOR /f \"tokens=*\" %%i IN ('%s') DO @%%i", commandLine)
comment = "REM"
case "emacs":
cmd = fmt.Sprintf("(with-temp-buffer (shell-command \"%s\" (current-buffer)) (eval-buffer))", commandLine)
comment = ";;"
case "tcsh":
cmd = fmt.Sprintf("eval `%s`", commandLine)
comment = ":"
default:
cmd = fmt.Sprintf("eval $(%s)", commandLine)
}
return fmt.Sprintf("%s Run this command to configure your shell: \n%s %s\n", comment, comment, cmd)
}