Skip to content

Commit 2c77837

Browse files
committed
command/apply: optional arg, default to pwd for config dir
1 parent abc6df2 commit 2c77837

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎command/apply.go‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,21 @@ func (c *ApplyCommand) Run(args []string) int {
3131
return 1
3232
}
3333

34+
var configPath string
3435
args = cmdFlags.Args()
3536
if len(args) > 1 {
3637
c.Ui.Error("The apply command expacts at most one argument.")
3738
cmdFlags.Usage()
3839
return 1
40+
} else if len(args) == 1 {
41+
configPath = args[0]
42+
} else {
43+
var err error
44+
configPath, err = os.Getwd()
45+
if err != nil {
46+
c.Ui.Error(fmt.Sprintf("Error getting pwd: %s", err))
47+
}
3948
}
40-
configPath := args[0]
4149

4250
// If we don't specify an output path, default to out normal state
4351
// path.

‎command/apply_test.go‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,52 @@ func TestApply_error(t *testing.T) {
190190
}
191191
}
192192

193+
func TestApply_noArgs(t *testing.T) {
194+
cwd, err := os.Getwd()
195+
if err != nil {
196+
t.Fatalf("err: %s", err)
197+
}
198+
if err := os.Chdir(testFixturePath("plan")); err != nil {
199+
t.Fatalf("err: %s", err)
200+
}
201+
defer os.Chdir(cwd)
202+
203+
statePath := testTempFile(t)
204+
205+
p := testProvider()
206+
ui := new(cli.MockUi)
207+
c := &ApplyCommand{
208+
ContextOpts: testCtxConfig(p),
209+
Ui: ui,
210+
}
211+
212+
args := []string{
213+
"-init",
214+
"-state", statePath,
215+
}
216+
if code := c.Run(args); code != 0 {
217+
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
218+
}
219+
220+
if _, err := os.Stat(statePath); err != nil {
221+
t.Fatalf("err: %s", err)
222+
}
223+
224+
f, err := os.Open(statePath)
225+
if err != nil {
226+
t.Fatalf("err: %s", err)
227+
}
228+
defer f.Close()
229+
230+
state, err := terraform.ReadState(f)
231+
if err != nil {
232+
t.Fatalf("err: %s", err)
233+
}
234+
if state == nil {
235+
t.Fatal("state should not be nil")
236+
}
237+
}
238+
193239
func TestApply_plan(t *testing.T) {
194240
planPath := testPlanFile(t, &terraform.Plan{
195241
Config: new(config.Config),

0 commit comments

Comments
 (0)