fix: Don't crash when reading header if 'main.tf' not found (#235)

Signed-off-by: Khosrow Moossavi <khos2ow@gmail.com>
This commit is contained in:
Khosrow Moossavi
2020-04-02 16:17:30 -04:00
committed by GitHub
parent a514d6e78f
commit 80172d77f4
3 changed files with 22 additions and 7 deletions

View File

@@ -13,11 +13,13 @@ var settings = print.NewSettings()
var options = module.NewOptions()
var rootCmd = &cobra.Command{
Args: cobra.NoArgs,
Use: "terraform-docs",
Short: "A utility to generate documentation from Terraform modules in various output formats",
Long: "A utility to generate documentation from Terraform modules in various output formats",
Version: version.Version(),
Args: cobra.NoArgs,
Use: "terraform-docs",
Short: "A utility to generate documentation from Terraform modules in various output formats",
Long: "A utility to generate documentation from Terraform modules in various output formats",
Version: version.Version(),
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
oppositeBool := func(name string) bool {
val, _ := cmd.Flags().GetBool(name)
@@ -67,7 +69,10 @@ func init() {
// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() error {
return rootCmd.Execute()
if err := rootCmd.Execute(); err != nil {
fmt.Printf("Error: %s\n", err.Error())
}
return nil
}
// RootCmd represents the base command when called without any subcommands

View File

@@ -72,7 +72,10 @@ func loadHeader(options *Options) (string, error) {
filename := filepath.Join(options.Path, options.HeaderFromFile)
_, err := ioutil.ReadFile(filename)
if err != nil {
return "", err
if options.HeaderFromFile != "main.tf" {
return "", err // user explicitly asked for a file which doesn't exist
}
return "", nil // absorb the error to not break workflow of users who don't have 'main.tf at all
}
lines := reader.Lines{
FileName: filename,

View File

@@ -75,6 +75,13 @@ func TestLoadHeader(t *testing.T) {
expected: "Custom Header:\n\nExample of 'foo_bar' module in `foo_bar.tf`.\n\n- list item 1\n- list item 2",
wantErr: false,
},
{
name: "load module header from path",
path: "no-inputs",
header: "main.tf",
expected: "",
wantErr: false,
},
{
name: "load module header from path",
path: "full-example",