diff --git a/command/providers.go b/command/providers.go index 3c1d14433..2a755f126 100644 --- a/command/providers.go +++ b/command/providers.go @@ -24,6 +24,7 @@ func (c *ProvidersCommand) Synopsis() string { } func (c *ProvidersCommand) Run(args []string) int { + c.Meta.process(args, false) cmdFlags := c.Meta.flagSet("providers") cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } @@ -43,6 +44,13 @@ func (c *ProvidersCommand) Run(args []string) int { c.Ui.Error(fmt.Sprintf("Failed to load root config module: %s", err)) return 1 } + if root == nil { + c.Ui.Error(fmt.Sprintf( + "No configuration files found in the directory: %s\n\n"+ + "This command requires configuration to run.", + configPath)) + return 1 + } // Validate the config (to ensure the version constraints are valid) err = root.Validate() diff --git a/command/providers_test.go b/command/providers_test.go index eb6c0dfc2..163cae129 100644 --- a/command/providers_test.go +++ b/command/providers_test.go @@ -41,3 +41,33 @@ func TestProviders(t *testing.T) { t.Errorf("output missing provider.baz\n\n%s", output) } } + +func TestProviders_noConfigs(t *testing.T) { + cwd, err := os.Getwd() + if err != nil { + t.Fatalf("err: %s", err) + } + if err := os.Chdir(testFixturePath("")); err != nil { + t.Fatalf("err: %s", err) + } + defer os.Chdir(cwd) + + ui := new(cli.MockUi) + c := &ProvidersCommand{ + Meta: Meta{ + Ui: ui, + }, + } + + args := []string{} + if code := c.Run(args); code == 0 { + t.Fatal("expected command to return non-zero exit code" + + " when no configs are available") + } + + output := ui.ErrorWriter.String() + expectedErrMsg := "No configuration files found" + if !strings.Contains(output, expectedErrMsg) { + t.Errorf("Expected error message: %s\nGiven output: %s", expectedErrMsg, output) + } +}