Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add workspace show command #15157

Merged
merged 5 commits into from
Jul 5, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
64 changes: 64 additions & 0 deletions command/workspace_command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,70 @@ func TestWorkspace_createAndList(t *testing.T) {
}
}

// 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("\nexpcted: %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)
}

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("\nexpcted: %q\nactual: %q", expected, actual)
}
}

// Don't allow names that aren't URL safe
func TestWorkspace_createInvalid(t *testing.T) {
// Create a temporary working directory that is empty
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 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh... I didn't catch this on the first pass. We need to return 0 here to indicate that the command has completed without error. Numbers greater than zero indicate errors, as we can see with the flag parse error above.

}

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