command/apply: optional arg, default to pwd for config dir

This commit is contained in:
Mitchell Hashimoto 2014-07-11 21:32:34 -07:00
parent abc6df2a7d
commit 2c77837a64
2 changed files with 55 additions and 1 deletions

View File

@ -31,13 +31,21 @@ func (c *ApplyCommand) Run(args []string) int {
return 1 return 1
} }
var configPath string
args = cmdFlags.Args() args = cmdFlags.Args()
if len(args) > 1 { if len(args) > 1 {
c.Ui.Error("The apply command expacts at most one argument.") c.Ui.Error("The apply command expacts at most one argument.")
cmdFlags.Usage() cmdFlags.Usage()
return 1 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 // If we don't specify an output path, default to out normal state
// path. // path.

View File

@ -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) { func TestApply_plan(t *testing.T) {
planPath := testPlanFile(t, &terraform.Plan{ planPath := testPlanFile(t, &terraform.Plan{
Config: new(config.Config), Config: new(config.Config),