command/show: show default state with no args [GH-349]

This commit is contained in:
Mitchell Hashimoto 2014-10-11 12:56:55 -07:00
parent d2e836275b
commit 069f758efb
3 changed files with 80 additions and 4 deletions

View File

@ -30,6 +30,7 @@ IMPROVEMENTS:
* config: Trailing commas are now allowed for the final elements of lists.
* core: Plugins are loaded from `~/.terraform.d/plugins` (Unix) or
`%USERDATA%/terraform.d/plugins` (Windows).
* command/show: With no arguments, it will show the default state. [GH-349]
* helper/schema: Can now have default values. [GH-245]
* providers/aws: Tag support for most resources.
* providers/aws: New resource `db_subnet_group`. [GH-295]

View File

@ -28,14 +28,27 @@ func (c *ShowCommand) Run(args []string) int {
}
args = cmdFlags.Args()
if len(args) != 1 {
if len(args) > 1 {
c.Ui.Error(
"The show command expects exactly one argument with the path\n" +
"The show command expects at most one argument with the path\n" +
"to a Terraform state or plan file.\n")
cmdFlags.Usage()
return 1
}
path := args[0]
var path string
if len(args) > 0 {
path = args[0]
} else {
// We should use the default state if it exists.
path = DefaultStateFilename
if _, err := os.Stat(DefaultStateFilename); err != nil {
if os.IsNotExist(err) {
c.Ui.Output("No state.")
return 0
}
}
}
var plan *terraform.Plan
var state *terraform.State

View File

@ -1,6 +1,9 @@
package command
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/hashicorp/terraform/config/module"
@ -27,6 +30,33 @@ func TestShow(t *testing.T) {
}
func TestShow_noArgs(t *testing.T) {
// Create the default state
td, err := ioutil.TempDir("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
statePath := filepath.Join(td, DefaultStateFilename)
f, err := os.Create(statePath)
if err != nil {
t.Fatalf("err: %s", err)
}
err = terraform.WriteState(testState(), f)
f.Close()
if err != nil {
t.Fatalf("err: %s", err)
}
// Change to the temporary directory
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(filepath.Dir(statePath)); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
ui := new(cli.MockUi)
c := &ShowCommand{
Meta: Meta{
@ -36,7 +66,39 @@ func TestShow_noArgs(t *testing.T) {
}
args := []string{}
if code := c.Run(args); code != 1 {
if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
}
}
func TestShow_noArgsNoState(t *testing.T) {
// Create the default state
td, err := ioutil.TempDir("", "tf")
if err != nil {
t.Fatalf("err: %s", err)
}
statePath := filepath.Join(td, DefaultStateFilename)
// Change to the temporary directory
cwd, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
if err := os.Chdir(filepath.Dir(statePath)); err != nil {
t.Fatalf("err: %s", err)
}
defer os.Chdir(cwd)
ui := new(cli.MockUi)
c := &ShowCommand{
Meta: Meta{
ContextOpts: testCtxConfig(testProvider()),
Ui: ui,
},
}
args := []string{}
if code := c.Run(args); code != 0 {
t.Fatalf("bad: \n%s", ui.OutputWriter.String())
}
}