Skip to content

Commit 7f87a01

Browse files
committed
command/plan: user friendly error if plan file given to plan command
1 parent 92faace commit 7f87a01

2 files changed

Lines changed: 31 additions & 1 deletion

File tree

‎command/plan.go‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (c *PlanCommand) Run(args []string) int {
6161
// This is going to keep track of shadow errors
6262
var shadowErr error
6363

64-
ctx, _, err := c.Context(contextOpts{
64+
ctx, planned, err := c.Context(contextOpts{
6565
Destroy: destroy,
6666
Path: path,
6767
StatePath: c.Meta.statePath,
@@ -71,6 +71,13 @@ func (c *PlanCommand) Run(args []string) int {
7171
c.Ui.Error(err.Error())
7272
return 1
7373
}
74+
if planned {
75+
c.Ui.Error(
76+
"The plan command cannot be called with a saved plan file.\n\n" +
77+
"The plan command expects a configuration directory as an argument.",
78+
)
79+
return 1
80+
}
7481

7582
err = terraform.SetDebugInfo(DefaultDataDir)
7683
if err != nil {

‎command/plan_test.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,29 @@ func TestPlan(t *testing.T) {
3737
}
3838
}
3939

40+
func TestPlan_plan(t *testing.T) {
41+
tmp, cwd := testCwd(t)
42+
defer testFixCwd(t, tmp, cwd)
43+
44+
planPath := testPlanFile(t, &terraform.Plan{
45+
Module: testModule(t, "apply"),
46+
})
47+
48+
p := testProvider()
49+
ui := new(cli.MockUi)
50+
c := &PlanCommand{
51+
Meta: Meta{
52+
ContextOpts: testCtxConfig(p),
53+
Ui: ui,
54+
},
55+
}
56+
57+
args := []string{planPath}
58+
if code := c.Run(args); code != 1 {
59+
t.Fatalf("bad: %d\n\n%s", code, ui.ErrorWriter.String())
60+
}
61+
}
62+
4063
func TestPlan_destroy(t *testing.T) {
4164
originalState := &terraform.State{
4265
Modules: []*terraform.ModuleState{

0 commit comments

Comments
 (0)