Unify code

pull/8334/head
Marcin Niemira 2020-05-31 16:29:55 +10:00
parent 366d6c370c
commit 08773015d5
No known key found for this signature in database
GPG Key ID: 053E25BDC33ED6A3
2 changed files with 76 additions and 78 deletions

View File

@ -28,24 +28,32 @@ import (
"github.com/docker/machine/libmachine/shell"
)
type shellData struct {
// Config represents the shell config
type Config struct {
Prefix string
Suffix string
Delimiter string
UnsetPrefix string
UnsetSuffix string
UnsetDelimiter string
Suffix string
UsageHint string
}
type shellData struct {
prefix string
suffix string
delimiter string
unsetPrefix string
unsetSuffix string
unsetDelimiter string
usageHint func(s ...interface{}) string
}
var shellConfigMap = map[string]shellData{
"fish": shellData{
Prefix: "set -gx ",
Suffix: "\";\n",
Delimiter: " \"",
UnsetPrefix: "set -e ",
UnsetSuffix: ";\n",
UnsetDelimiter: "",
prefix: "set -gx ",
suffix: "\";\n",
delimiter: " \"",
unsetPrefix: "set -e ",
unsetSuffix: ";\n",
unsetDelimiter: "",
usageHint: func(s ...interface{}) string {
return fmt.Sprintf(`
# %s
@ -54,52 +62,52 @@ var shellConfigMap = map[string]shellData{
},
},
"powershell": shellData{
"$Env:",
"\"\n",
" = \"",
`Remove-Item Env:\\`,
"\n",
"",
func(s ...interface{}) string {
prefix: "$Env:",
suffix: "\"\n",
delimiter: " = \"",
unsetPrefix: `Remove-Item Env:\\`,
unsetSuffix: "\n",
unsetDelimiter: "",
usageHint: func(s ...interface{}) string {
return fmt.Sprintf(`# %s
# & %s | Invoke-Expression
`, s...)
},
},
"cmd": shellData{
"SET ",
"\n",
"=",
"SET ",
"\n",
"=",
func(s ...interface{}) string {
prefix: "SET ",
suffix: "\n",
delimiter: "=",
unsetPrefix: "SET ",
unsetSuffix: "\n",
unsetDelimiter: "=",
usageHint: func(s ...interface{}) string {
return fmt.Sprintf(`REM %s
REM @FOR /f "tokens=*" %%i IN ('%s') DO @%%i
`, s...)
},
},
"emacs": shellData{
"(setenv \"",
"\")\n",
"\" \"",
"(setenv \"",
")\n",
"\" nil",
func(s ...interface{}) string {
prefix: "(setenv \"",
suffix: "\")\n",
delimiter: "\" \"",
unsetPrefix: "(setenv \"",
unsetSuffix: ")\n",
unsetDelimiter: "\" nil",
usageHint: func(s ...interface{}) string {
return fmt.Sprintf(`;; %s
;; (with-temp-buffer (shell-command "%s" (current-buffer)) (eval-buffer))
`, s...)
},
},
"bash": shellData{
"export ",
"\"\n",
"=\"",
"unset ",
"\n",
"",
func(s ...interface{}) string {
prefix: "export ",
suffix: "\"\n",
delimiter: "=\"",
unsetPrefix: "unset ",
unsetSuffix: "\n",
unsetDelimiter: "",
usageHint: func(s ...interface{}) string {
return fmt.Sprintf(`
# %s
# eval $(%s)
@ -107,13 +115,13 @@ REM @FOR /f "tokens=*" %%i IN ('%s') DO @%%i
},
},
"none": shellData{
"",
"\n",
"=",
"",
"\n",
"=",
func(s ...interface{}) string {
prefix: "",
suffix: "\n",
delimiter: "=",
unsetPrefix: "",
unsetSuffix: "\n",
unsetDelimiter: "",
usageHint: func(s ...interface{}) string {
return fmt.Sprintf(`
# %s
# eval $(%s)
@ -124,14 +132,6 @@ REM @FOR /f "tokens=*" %%i IN ('%s') DO @%%i
var defaultShell shellData = shellConfigMap["bash"]
// Config represents the shell config
type Config struct {
Prefix string
Delimiter string
Suffix string
UsageHint string
}
var (
// ForceShell forces a shell name
ForceShell string
@ -142,23 +142,24 @@ func Detect() (string, error) {
return shell.Detect()
}
func generateUsageHint(sh, usgPlz, usgCmd string) string {
shellCfg, ok := shellConfigMap[sh]
func getShell(name string) shellData {
shell, ok := shellConfigMap[name]
if !ok {
shellCfg = defaultShell
shell = defaultShell
}
return shell
}
func generateUsageHint(sh, usgPlz, usgCmd string) string {
shellCfg := getShell(sh)
return shellCfg.usageHint(usgPlz, usgCmd)
}
// CfgSet generates context variables for shell
func CfgSet(ec EnvConfig, plz, cmd string) *Config {
shellCfg, ok := shellConfigMap[ec.Shell]
if !ok {
shellCfg = defaultShell
}
shellCfg := getShell(ec.Shell)
s := &Config{}
s.Suffix, s.Prefix, s.Delimiter = shellCfg.Suffix, shellCfg.Prefix, shellCfg.Delimiter
s.Suffix, s.Prefix, s.Delimiter = shellCfg.suffix, shellCfg.prefix, shellCfg.delimiter
s.UsageHint = generateUsageHint(ec.Shell, plz, cmd)
return s
@ -178,27 +179,21 @@ func SetScript(ec EnvConfig, w io.Writer, envTmpl string, data interface{}) erro
// UnsetScript writes out a shell-compatible unset script
func UnsetScript(ec EnvConfig, w io.Writer, vars []string) error {
var sb strings.Builder
shCfg, ok := shellConfigMap[ec.Shell]
if !ok {
shCfg = defaultShell
}
pfx, sfx, delim := shCfg.UnsetPrefix, shCfg.UnsetSuffix, shCfg.UnsetDelimiter
shellCfg := getShell(ec.Shell)
pfx, sfx, delim := shellCfg.unsetPrefix, shellCfg.unsetSuffix, shellCfg.unsetDelimiter
switch ec.Shell {
case "cmd", "emacs", "fish":
break
case "powershell":
vars = []string{strings.Join(vars, " Env:\\\\")}
default:
vars = []string{strings.Join(vars, " ")}
}
for _, v := range vars {
if _, err := sb.WriteString(fmt.Sprintf("%s%s%s%s", pfx, v, delim, sfx)); err != nil {
return err
}
}
case "powershell":
if _, err := sb.WriteString(fmt.Sprintf("%s%s%s", pfx, strings.Join(vars, " Env:\\\\"), sfx)); err != nil {
return err
}
default:
if _, err := sb.WriteString(fmt.Sprintf("%s%s%s", pfx, strings.Join(vars, " "), sfx)); err != nil {
return err
}
}
_, err := w.Write([]byte(sb.String()))
return err
}

View File

@ -117,6 +117,9 @@ set -e bar;`},
}
func TestDetect(t *testing.T) {
orgShellEnv := os.Getenv("SHELL")
defer os.Setenv("SHELL", orgShellEnv)
os.Setenv("SHELL", "bash")
if s, err := Detect(); err != nil {
t.Fatalf("unexpected error: '%v' during shell detection. Returned shell: %s", err, s)