mirror of https://github.com/docker/docs.git
use template for shell config; more powershell and cmd fixes
Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
This commit is contained in:
parent
7585680668
commit
e13e2b61c6
|
@ -1,6 +1,7 @@
|
||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
@ -33,6 +34,10 @@ import (
|
||||||
"github.com/docker/machine/utils"
|
"github.com/docker/machine/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
ErrUnknownShell = errors.New("unknown shell")
|
||||||
|
)
|
||||||
|
|
||||||
type machineConfig struct {
|
type machineConfig struct {
|
||||||
machineName string
|
machineName string
|
||||||
machineDir string
|
machineDir string
|
||||||
|
@ -648,3 +653,19 @@ func getCertPathInfo(c *cli.Context) libmachine.CertPathInfo {
|
||||||
ClientKeyPath: clientKeyPath,
|
ClientKeyPath: clientKeyPath,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func detectShell() (string, error) {
|
||||||
|
// attempt to get the SHELL env var
|
||||||
|
shell := filepath.Base(os.Getenv("SHELL"))
|
||||||
|
// none detected; check for windows env
|
||||||
|
if shell == "." && os.Getenv("windir") != "" {
|
||||||
|
log.Printf("On Windows, please specify either cmd or powershell with the --shell flag.\n\n")
|
||||||
|
return "", ErrUnknownShell
|
||||||
|
}
|
||||||
|
|
||||||
|
if shell == "" {
|
||||||
|
return "", ErrUnknownShell
|
||||||
|
}
|
||||||
|
|
||||||
|
return shell, nil
|
||||||
|
}
|
||||||
|
|
112
commands/env.go
112
commands/env.go
|
@ -4,8 +4,8 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
|
|
||||||
|
@ -13,19 +13,72 @@ import (
|
||||||
"github.com/docker/machine/utils"
|
"github.com/docker/machine/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
envTmpl = `{{ .Prefix }}DOCKER_TLS_VERIFY{{ .Delimiter }}{{ .DockerTLSVerify }}{{ .Suffix }}{{ .Prefix }}DOCKER_HOST{{ .Delimiter }}{{ .DockerHost }}{{ .Suffix }}{{ .Prefix }}DOCKER_CERT_PATH{{ .Delimiter }}{{ .DockerCertPath }}{{ .Suffix }}{{ .UsageHint }}`
|
||||||
|
)
|
||||||
|
|
||||||
|
type ShellConfig struct {
|
||||||
|
Prefix string
|
||||||
|
Delimiter string
|
||||||
|
Suffix string
|
||||||
|
DockerCertPath string
|
||||||
|
DockerHost string
|
||||||
|
DockerTLSVerify string
|
||||||
|
UsageHint string
|
||||||
|
}
|
||||||
|
|
||||||
func cmdEnv(c *cli.Context) {
|
func cmdEnv(c *cli.Context) {
|
||||||
userShell := c.String("shell")
|
userShell := c.String("shell")
|
||||||
if userShell == "" {
|
if userShell == "" {
|
||||||
userShell = filepath.Base(os.Getenv("SHELL"))
|
shell, err := detectShell()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
userShell = shell
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t := template.New("envConfig")
|
||||||
|
|
||||||
|
usageHint := generateUsageHint(c.Args().First(), userShell)
|
||||||
|
|
||||||
|
shellCfg := ShellConfig{
|
||||||
|
DockerCertPath: "",
|
||||||
|
DockerHost: "",
|
||||||
|
DockerTLSVerify: "",
|
||||||
|
}
|
||||||
|
|
||||||
|
// unset vars
|
||||||
if c.Bool("unset") {
|
if c.Bool("unset") {
|
||||||
switch userShell {
|
switch userShell {
|
||||||
case "fish":
|
case "fish":
|
||||||
fmt.Printf("set -e DOCKER_TLS_VERIFY;\nset -e DOCKER_CERT_PATH;\nset -e DOCKER_HOST;\n")
|
shellCfg.Prefix = "set -e "
|
||||||
|
shellCfg.Delimiter = ""
|
||||||
|
shellCfg.Suffix = ";\n"
|
||||||
case "powershell":
|
case "powershell":
|
||||||
fmt.Printf("Remove-Item Env:\\DOCKER_TLS_VERIFY\nRemove-Item Env:\\DOCKER_CERT_PATH\nRemove-Item Env:\\DOCKER_HOST\n")
|
shellCfg.Prefix = "Remove-Item Env:\\\\"
|
||||||
|
shellCfg.Delimiter = ""
|
||||||
|
shellCfg.Suffix = "\n"
|
||||||
|
case "cmd":
|
||||||
|
// since there is no way to unset vars in cmd just reset to empty
|
||||||
|
shellCfg.DockerCertPath = ""
|
||||||
|
shellCfg.DockerHost = ""
|
||||||
|
shellCfg.DockerTLSVerify = ""
|
||||||
|
shellCfg.Prefix = "set "
|
||||||
|
shellCfg.Delimiter = "="
|
||||||
|
shellCfg.Suffix = "\n"
|
||||||
default:
|
default:
|
||||||
fmt.Println("unset DOCKER_TLS_VERIFY DOCKER_CERT_PATH DOCKER_HOST")
|
shellCfg.Prefix = "unset "
|
||||||
|
shellCfg.Delimiter = " "
|
||||||
|
shellCfg.Suffix = "\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpl, err := t.Parse(envTmpl)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tmpl.Execute(os.Stdout, shellCfg); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -88,36 +141,59 @@ func cmdEnv(c *cli.Context) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
usageHint := generateUsageHint(c.Args().First(), userShell)
|
shellCfg = ShellConfig{
|
||||||
|
DockerCertPath: cfg.machineDir,
|
||||||
|
DockerHost: dockerHost,
|
||||||
|
DockerTLSVerify: "1",
|
||||||
|
UsageHint: usageHint,
|
||||||
|
}
|
||||||
|
|
||||||
switch userShell {
|
switch userShell {
|
||||||
case "fish":
|
case "fish":
|
||||||
fmt.Printf("set -x DOCKER_TLS_VERIFY 1;\nset -x DOCKER_CERT_PATH %q;\nset -x DOCKER_HOST %s;\n\n%s\n",
|
shellCfg.Prefix = "set -x "
|
||||||
cfg.machineDir, dockerHost, usageHint)
|
shellCfg.Suffix = ";\n"
|
||||||
|
shellCfg.Delimiter = " "
|
||||||
case "powershell":
|
case "powershell":
|
||||||
fmt.Printf("$Env:DOCKER_TLS_VERIFY = 1\n$Env:DOCKER_CERT_PATH = \"%s\"\n$Env:DOCKER_HOST = \"%s\"\n",
|
shellCfg.Prefix = "$Env:"
|
||||||
cfg.machineDir, dockerHost)
|
shellCfg.Suffix = "\"\n"
|
||||||
|
shellCfg.Delimiter = " = \""
|
||||||
|
case "cmd":
|
||||||
|
shellCfg.Prefix = "set "
|
||||||
|
shellCfg.Suffix = "\n"
|
||||||
|
shellCfg.Delimiter = "="
|
||||||
default:
|
default:
|
||||||
fmt.Printf("export DOCKER_TLS_VERIFY=1\nexport DOCKER_CERT_PATH=%q\nexport DOCKER_HOST=%s\n\n%s\n",
|
shellCfg.Prefix = "export "
|
||||||
cfg.machineDir, dockerHost, usageHint)
|
shellCfg.Suffix = "\n"
|
||||||
|
shellCfg.Delimiter = "="
|
||||||
|
}
|
||||||
|
|
||||||
|
tmpl, err := t.Parse(envTmpl)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := tmpl.Execute(os.Stdout, shellCfg); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateUsageHint(machineName string, userShell string) string {
|
func generateUsageHint(machineName string, userShell string) string {
|
||||||
cmd := ""
|
cmd := ""
|
||||||
switch userShell {
|
switch userShell {
|
||||||
case "powershell":
|
|
||||||
if machineName != "" {
|
|
||||||
cmd = fmt.Sprintf("Param(docker-machine env %s)", machineName)
|
|
||||||
} else {
|
|
||||||
cmd = "Param(docker-machine env)"
|
|
||||||
}
|
|
||||||
case "fish":
|
case "fish":
|
||||||
if machineName != "" {
|
if machineName != "" {
|
||||||
cmd = fmt.Sprintf("eval (docker-machine env %s)", machineName)
|
cmd = fmt.Sprintf("eval (docker-machine env %s)", machineName)
|
||||||
} else {
|
} else {
|
||||||
cmd = "eval (docker-machine env)"
|
cmd = "eval (docker-machine env)"
|
||||||
}
|
}
|
||||||
|
case "powershell":
|
||||||
|
if machineName != "" {
|
||||||
|
cmd = fmt.Sprintf("docker-machine env %s | Invoke-Expression", machineName)
|
||||||
|
} else {
|
||||||
|
cmd = "Param(docker-machine env)"
|
||||||
|
}
|
||||||
|
case "cmd":
|
||||||
|
cmd = "copy and paste the above values into your command prompt"
|
||||||
default:
|
default:
|
||||||
if machineName != "" {
|
if machineName != "" {
|
||||||
cmd = fmt.Sprintf("eval \"$(docker-machine env %s)\"", machineName)
|
cmd = fmt.Sprintf("eval \"$(docker-machine env %s)\"", machineName)
|
||||||
|
|
|
@ -12,7 +12,6 @@ import (
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"syscall"
|
|
||||||
|
|
||||||
gossh "golang.org/x/crypto/ssh"
|
gossh "golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
@ -83,9 +82,7 @@ func (kp *KeyPair) WriteToFile(privateKeyPath string, publicKeyPath string) erro
|
||||||
|
|
||||||
// windows does not support chmod
|
// windows does not support chmod
|
||||||
switch runtime.GOOS {
|
switch runtime.GOOS {
|
||||||
case "windows":
|
case "darwin", "linux":
|
||||||
syscall.Chmod(f.Name(), 0600)
|
|
||||||
default:
|
|
||||||
if err := f.Chmod(0600); err != nil {
|
if err := f.Chmod(0600); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue