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.
This commit is contained in:
Mike Helmick 2017-07-05 17:35:46 -04:00 committed by Martin Atkins
parent 909989acfa
commit 9d7fce2f69
4 changed files with 113 additions and 1 deletions

View File

@ -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.

View File

@ -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)
}
}

37
command/workspace_show.go Normal file
View File

@ -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"
}

View File

@ -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,