forked from terraform-docs/terraform-docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroot.go
102 lines (83 loc) · 4.63 KB
/
root.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
Copyright 2021 The terraform-docs Authors.
Licensed under the MIT license (the "License"); you may not
use this file except in compliance with the License.
You may obtain a copy of the License at the LICENSE file in
the root directory of this source tree.
*/
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/terraform-docs/terraform-docs/cmd/asciidoc"
"github.com/terraform-docs/terraform-docs/cmd/completion"
"github.com/terraform-docs/terraform-docs/cmd/json"
"github.com/terraform-docs/terraform-docs/cmd/markdown"
"github.com/terraform-docs/terraform-docs/cmd/pretty"
"github.com/terraform-docs/terraform-docs/cmd/tfvars"
"github.com/terraform-docs/terraform-docs/cmd/toml"
versioncmd "github.com/terraform-docs/terraform-docs/cmd/version"
"github.com/terraform-docs/terraform-docs/cmd/xml"
"github.com/terraform-docs/terraform-docs/cmd/yaml"
"github.com/terraform-docs/terraform-docs/internal/cli"
"github.com/terraform-docs/terraform-docs/internal/version"
"github.com/terraform-docs/terraform-docs/print"
)
// 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 {
if err := NewCommand().Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
return err
}
return nil
}
// NewCommand returns a new cobra.Command for 'root' command
func NewCommand() *cobra.Command {
config := print.DefaultConfig()
runtime := cli.NewRuntime(config)
cmd := &cobra.Command{
Args: cobra.MaximumNArgs(1),
Use: "terraform-docs [PATH]",
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.Full(),
SilenceUsage: true,
SilenceErrors: true,
Annotations: cli.Annotations("root"),
PreRunE: runtime.PreRunEFunc,
RunE: runtime.RunEFunc,
}
// flags
cmd.PersistentFlags().StringVarP(&config.File, "config", "c", ".terraform-docs.yml", "config file name")
cmd.PersistentFlags().BoolVar(&config.Recursive.Enabled, "recursive", false, "update submodules recursively (default false)")
cmd.PersistentFlags().StringVar(&config.Recursive.Path, "recursive-path", "modules", "submodules path to recursively update")
cmd.PersistentFlags().StringSliceVar(&config.Sections.Show, "show", []string{}, "show section ["+print.AllSections+"]")
cmd.PersistentFlags().StringSliceVar(&config.Sections.Hide, "hide", []string{}, "hide section ["+print.AllSections+"]")
cmd.PersistentFlags().StringVar(&config.Output.File, "output-file", "", "file path to insert output into (default \"\")")
cmd.PersistentFlags().StringVar(&config.Output.Mode, "output-mode", "inject", "output to file method ["+print.OutputModes+"]")
cmd.PersistentFlags().StringVar(&config.Output.Template, "output-template", print.OutputTemplate, "output template")
cmd.PersistentFlags().BoolVar(&config.Output.Check, "output-check", false, "check if content of output file is up to date (default false)")
cmd.PersistentFlags().BoolVar(&config.Sort.Enabled, "sort", true, "sort items")
cmd.PersistentFlags().StringVar(&config.Sort.By, "sort-by", "name", "sort items by criteria ["+print.SortTypes+"]")
cmd.PersistentFlags().StringVar(&config.HeaderFrom, "header-from", "main.tf", "relative path of a file to read header from")
cmd.PersistentFlags().StringVar(&config.FooterFrom, "footer-from", "", "relative path of a file to read footer from (default \"\")")
cmd.PersistentFlags().BoolVar(&config.Settings.LockFile, "lockfile", true, "read .terraform.lock.hcl if exist")
cmd.PersistentFlags().BoolVar(&config.OutputValues.Enabled, "output-values", false, "inject output values into outputs (default false)")
cmd.PersistentFlags().StringVar(&config.OutputValues.From, "output-values-from", "", "inject output values from file into outputs (default \"\")")
cmd.PersistentFlags().BoolVar(&config.Settings.ReadComments, "read-comments", true, "use comments as description when description is empty")
// formatter subcommands
cmd.AddCommand(asciidoc.NewCommand(runtime, config))
cmd.AddCommand(json.NewCommand(runtime, config))
cmd.AddCommand(markdown.NewCommand(runtime, config))
cmd.AddCommand(pretty.NewCommand(runtime, config))
cmd.AddCommand(tfvars.NewCommand(runtime, config))
cmd.AddCommand(toml.NewCommand(runtime, config))
cmd.AddCommand(xml.NewCommand(runtime, config))
cmd.AddCommand(yaml.NewCommand(runtime, config))
// other subcommands
cmd.AddCommand(completion.NewCommand())
cmd.AddCommand(versioncmd.NewCommand())
return cmd
}