From 9d7fce2f69123100afd43be27a110f8431fbb43b Mon Sep 17 00:00:00 2001 From: Mike Helmick Date: Wed, 5 Jul 2017 17:35:46 -0400 Subject: [PATCH] command: "terraform workspace show" to print current workspace name This command serves as an alternative to the human-oriented list of workspaces for scripting use-cases where it's useful to know the _current_ workspace name. --- command/workspace_command.go | 1 + command/workspace_command_test.go | 70 ++++++++++++++++++++++++++++++- command/workspace_show.go | 37 ++++++++++++++++ commands.go | 6 +++ 4 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 command/workspace_show.go diff --git a/command/workspace_command.go b/command/workspace_command.go index c40f41c75cec..eda8cbe0681f 100644 --- a/command/workspace_command.go +++ b/command/workspace_command.go @@ -35,6 +35,7 @@ Usage: terraform workspace Subcommands: + show Show the current workspace name. list List workspaces. select Select a workspace. new Create a new workspace. diff --git a/command/workspace_command_test.go b/command/workspace_command_test.go index 3329e856527d..f1cd14957877 100644 --- a/command/workspace_command_test.go +++ b/command/workspace_command_test.go @@ -99,7 +99,75 @@ func TestWorkspace_createAndList(t *testing.T) { expected := "default\n test_a\n test_b\n* test_c" if actual != expected { - t.Fatalf("\nexpcted: %q\nactual: %q", expected, actual) + t.Fatalf("\nexpected: %q\nactual: %q", expected, actual) + } +} + +// Create some workspaces and test the show output. +func TestWorkspace_createAndShow(t *testing.T) { + // Create a temporary working directory that is empty + td := tempDir(t) + os.MkdirAll(td, 0755) + defer os.RemoveAll(td) + defer testChdir(t, td)() + + // make sure a vars file doesn't interfere + err := ioutil.WriteFile( + DefaultVarsFilename, + []byte(`foo = "bar"`), + 0644, + ) + if err != nil { + t.Fatal(err) + } + + // make sure current workspace show outputs "default" + showCmd := &WorkspaceShowCommand{} + ui := new(cli.MockUi) + showCmd.Meta = Meta{Ui: ui} + + if code := showCmd.Run(nil); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter) + } + + actual := strings.TrimSpace(ui.OutputWriter.String()) + expected := "default" + + if actual != expected { + t.Fatalf("\nexpected: %q\nactual: %q", expected, actual) + } + + newCmd := &WorkspaceNewCommand{} + + env := []string{"test_a"} + + // create test_a workspace + ui = new(cli.MockUi) + newCmd.Meta = Meta{Ui: ui} + if code := newCmd.Run(env); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter) + } + + selCmd := &WorkspaceSelectCommand{} + ui = new(cli.MockUi) + selCmd.Meta = Meta{Ui: ui} + if code := selCmd.Run(env); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter) + } + + showCmd = &WorkspaceShowCommand{} + ui = new(cli.MockUi) + showCmd.Meta = Meta{Ui: ui} + + if code := showCmd.Run(nil); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter) + } + + actual = strings.TrimSpace(ui.OutputWriter.String()) + expected = "test_a" + + if actual != expected { + t.Fatalf("\nexpected: %q\nactual: %q", expected, actual) } } diff --git a/command/workspace_show.go b/command/workspace_show.go new file mode 100644 index 000000000000..df478cb5718c --- /dev/null +++ b/command/workspace_show.go @@ -0,0 +1,37 @@ +package command + +import ( + "strings" +) + +type WorkspaceShowCommand struct { + Meta +} + +func (c *WorkspaceShowCommand) Run(args []string) int { + args = c.Meta.process(args, true) + + cmdFlags := c.Meta.flagSet("workspace show") + cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } + if err := cmdFlags.Parse(args); err != nil { + return 1 + } + + workspace := c.Workspace() + c.Ui.Output(workspace) + + return 0 +} + +func (c *WorkspaceShowCommand) Help() string { + helpText := ` +Usage: terraform workspace show + + Show the name of the current workspace. +` + return strings.TrimSpace(helpText) +} + +func (c *WorkspaceShowCommand) Synopsis() string { + return "Show the name of the current workspace" +} diff --git a/commands.go b/commands.go index 7e1468955975..85f1794b9d64 100644 --- a/commands.go +++ b/commands.go @@ -224,6 +224,12 @@ func init() { }, nil }, + "workspace show": func() (cli.Command, error) { + return &command.WorkspaceShowCommand{ + Meta: meta, + }, nil + }, + "workspace new": func() (cli.Command, error) { return &command.WorkspaceNewCommand{ Meta: meta,