diff --git a/commands/env.go b/commands/env.go index 1aff42ae..13a472f8 100644 --- a/commands/env.go +++ b/commands/env.go @@ -130,6 +130,10 @@ func shellCfgSet(c CommandLine, api libmachine.API) (*ShellConfig, error) { shellCfg.Prefix = "SET " shellCfg.Suffix = "\n" shellCfg.Delimiter = "=" + case "emacs": + shellCfg.Prefix = "(setenv \"" + shellCfg.Suffix = "\")\n" + shellCfg.Delimiter = "\" \"" default: shellCfg.Prefix = "export " shellCfg.Suffix = "\"\n" @@ -170,6 +174,10 @@ func shellCfgUnset(c CommandLine, api libmachine.API) (*ShellConfig, error) { shellCfg.Prefix = "SET " shellCfg.Suffix = "\n" shellCfg.Delimiter = "=" + case "emacs": + shellCfg.Prefix = "(setenv \"" + shellCfg.Suffix = ")\n" + shellCfg.Delimiter = "\" nil" default: shellCfg.Prefix = "unset " shellCfg.Suffix = "\n" @@ -229,6 +237,9 @@ func (g *EnvUsageHintGenerator) GenerateUsageHint(userShell string, args []strin case "cmd": cmd = fmt.Sprintf("\tFOR /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 = ";;" default: cmd = fmt.Sprintf("eval \"$(%s)\"", commandLine) } diff --git a/commands/env_test.go b/commands/env_test.go index 6e6300e6..9989d2ca 100644 --- a/commands/env_test.go +++ b/commands/env_test.go @@ -65,6 +65,12 @@ func TestHints(t *testing.T) { {"cmd", "./machine env --shell=cmd --swarm default", "REM Run this command to configure your shell: \nREM \tFOR /f \"tokens=*\" %i IN ('./machine env --shell=cmd --swarm default') DO %i\n"}, {"cmd", "./machine env --shell=cmd --no-proxy --swarm default", "REM Run this command to configure your shell: \nREM \tFOR /f \"tokens=*\" %i IN ('./machine env --shell=cmd --no-proxy --swarm default') DO %i\n"}, {"cmd", "./machine env --shell=cmd --unset", "REM Run this command to configure your shell: \nREM \tFOR /f \"tokens=*\" %i IN ('./machine env --shell=cmd --unset') DO %i\n"}, + + {"emacs", "./machine env --shell=emacs default", ";; Run this command to configure your shell: \n;; (with-temp-buffer (shell-command \"./machine env --shell=emacs default\" (current-buffer)) (eval-buffer))\n"}, + {"emacs", "./machine env --shell=emacs --no-proxy default", ";; Run this command to configure your shell: \n;; (with-temp-buffer (shell-command \"./machine env --shell=emacs --no-proxy default\" (current-buffer)) (eval-buffer))\n"}, + {"emacs", "./machine env --shell=emacs --swarm default", ";; Run this command to configure your shell: \n;; (with-temp-buffer (shell-command \"./machine env --shell=emacs --swarm default\" (current-buffer)) (eval-buffer))\n"}, + {"emacs", "./machine env --shell=emacs --no-proxy --swarm default", ";; Run this command to configure your shell: \n;; (with-temp-buffer (shell-command \"./machine env --shell=emacs --no-proxy --swarm default\" (current-buffer)) (eval-buffer))\n"}, + {"emacs", "./machine env --shell=emacs --unset", ";; Run this command to configure your shell: \n;; (with-temp-buffer (shell-command \"./machine env --shell=emacs --unset\" (current-buffer)) (eval-buffer))\n"}, } for _, test := range tests { @@ -213,6 +219,42 @@ func TestShellCfgSet(t *testing.T) { }, expectedErr: nil, }, + { + description: "emacs set happy path", + commandLine: &commandstest.FakeCommandLine{ + CliArgs: []string{"quux"}, + LocalFlags: &commandstest.FakeFlagger{ + Data: map[string]interface{}{ + "shell": "emacs", + "swarm": false, + "no-proxy": false, + }, + }, + }, + api: &libmachinetest.FakeAPI{ + Hosts: []*host.Host{ + { + Name: "quux", + }, + }, + }, + connChecker: &FakeConnChecker{ + DockerHost: "tcp://1.2.3.4:2376", + AuthOptions: nil, + Err: nil, + }, + expectedShellCfg: &ShellConfig{ + Prefix: "(setenv \"", + Suffix: "\")\n", + Delimiter: "\" \"", + DockerCertPath: filepath.Join(mcndirs.GetMachineDir(), "quux"), + DockerHost: "tcp://1.2.3.4:2376", + DockerTLSVerify: "1", + UsageHint: usageHint, + MachineName: "quux", + }, + expectedErr: nil, + }, { description: "cmd.exe happy path", commandLine: &commandstest.FakeCommandLine{ diff --git a/test/integration/core/env_shell.bats b/test/integration/core/env_shell.bats old mode 100644 new mode 100755 index e290997e..2349f3d5 --- a/test/integration/core/env_shell.bats +++ b/test/integration/core/env_shell.bats @@ -51,6 +51,15 @@ load ${BASE_TEST_DIR}/helpers.bash [[ ${lines[4]} == "set -gx NO_PROXY \"$(machine ip $NAME)\";" ]] } +@test "$DRIVER: test emacs notation" { + run machine env --shell emacs --no-proxy $NAME + [[ ${lines[0]} == "(setenv \"DOCKER_TLS_VERIFY\" \"1\")" ]] + [[ ${lines[1]} == "(setenv \"DOCKER_HOST\" \"$(machine url $NAME)\")" ]] + [[ ${lines[2]} == "(setenv \"DOCKER_CERT_PATH\" \"$MACHINE_STORAGE_PATH/machines/$NAME\")" ]] + [[ ${lines[3]} == "(setenv \"DOCKER_MACHINE_NAME\" \"$NAME\")" ]] + [[ ${lines[4]} == "(setenv \"NO_PROXY\" \"$(machine ip $NAME)\")" ]] +} + @test "$DRIVER: test no proxy with NO_PROXY already set" { export NO_PROXY=localhost run machine env --no-proxy $NAME @@ -103,4 +112,12 @@ load ${BASE_TEST_DIR}/helpers.bash [[ ${lines[1]} == "SET DOCKER_HOST=" ]] [[ ${lines[2]} == "SET DOCKER_CERT_PATH=" ]] [[ ${lines[3]} == "SET DOCKER_MACHINE_NAME=" ]] -} \ No newline at end of file +} + +@test "$DRIVER: unset with emacs shell" { + run machine env --shell emacs -u + [[ ${lines[0]} == "(setenv \"DOCKER_TLS_VERIFY\" nil)" ]] + [[ ${lines[1]} == "(setenv \"DOCKER_HOST\" nil)" ]] + [[ ${lines[2]} == "(setenv \"DOCKER_CERT_PATH\" nil)" ]] + [[ ${lines[3]} == "(setenv \"DOCKER_MACHINE_NAME\" nil)" ]] +}