mirror of
https://github.com/terraform-docs/terraform-docs.git
synced 2026-03-27 04:48:33 +07:00
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:
17
cmd/root.go
17
cmd/root.go
@@ -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
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user