Skip to content

Commit

Permalink
command: "terraform workspace show" to print current workspace name
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
michaelhelmick authored and apparentlymart committed Jul 5, 2017
1 parent 909989a commit 9d7fce2
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 1 deletion.
1 change: 1 addition & 0 deletions command/workspace_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
70 changes: 69 additions & 1 deletion command/workspace_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
37 changes: 37 additions & 0 deletions command/workspace_show.go
Original file line number Diff line number Diff line change
@@ -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"
}
6 changes: 6 additions & 0 deletions commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 9d7fce2

Please sign in to comment.