From 2c77837a640e66ca6432eedff014f303ba399b86 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 11 Jul 2014 21:32:34 -0700 Subject: [PATCH] command/apply: optional arg, default to pwd for config dir --- command/apply.go | 10 +++++++++- command/apply_test.go | 46 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/command/apply.go b/command/apply.go index 13b1b71671..636a8daa78 100644 --- a/command/apply.go +++ b/command/apply.go @@ -31,13 +31,21 @@ func (c *ApplyCommand) Run(args []string) int { return 1 } + var configPath string args = cmdFlags.Args() if len(args) > 1 { c.Ui.Error("The apply command expacts at most one argument.") cmdFlags.Usage() return 1 + } else if len(args) == 1 { + configPath = args[0] + } else { + var err error + configPath, err = os.Getwd() + if err != nil { + c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err)) + } } - configPath := args[0] // If we don't specify an output path, default to out normal state // path. diff --git a/command/apply_test.go b/command/apply_test.go index b068e76006..58a831a2f6 100644 --- a/command/apply_test.go +++ b/command/apply_test.go @@ -190,6 +190,52 @@ func TestApply_error(t *testing.T) { } } +func TestApply_noArgs(t *testing.T) { + cwd, err := os.Getwd() + if err != nil { + t.Fatalf("err: %s", err) + } + if err := os.Chdir(testFixturePath("plan")); err != nil { + t.Fatalf("err: %s", err) + } + defer os.Chdir(cwd) + + statePath := testTempFile(t) + + p := testProvider() + ui := new(cli.MockUi) + c := &ApplyCommand{ + ContextOpts: testCtxConfig(p), + Ui: ui, + } + + args := []string{ + "-init", + "-state", statePath, + } + if code := c.Run(args); code != 0 { + t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String()) + } + + if _, err := os.Stat(statePath); err != nil { + t.Fatalf("err: %s", err) + } + + f, err := os.Open(statePath) + if err != nil { + t.Fatalf("err: %s", err) + } + defer f.Close() + + state, err := terraform.ReadState(f) + if err != nil { + t.Fatalf("err: %s", err) + } + if state == nil { + t.Fatal("state should not be nil") + } +} + func TestApply_plan(t *testing.T) { planPath := testPlanFile(t, &terraform.Plan{ Config: new(config.Config),