refactor: Refactor cli implemention and configuration (#266)

* Refactor cli implemention and configuration

* cleanup
This commit is contained in:
Khosrow Moossavi
2020-05-20 22:21:19 -04:00
committed by GitHub
parent 085cac0c54
commit 04a9ef49eb
54 changed files with 943 additions and 493 deletions

View File

@@ -1,48 +0,0 @@
package cmd
import (
"github.com/spf13/cobra"
)
var asciidocCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "asciidoc [PATH]",
Aliases: []string{"adoc"},
Short: "Generate AsciiDoc of inputs and outputs",
Annotations: formatAnnotations("asciidoc"),
RunE: formatRunE,
}
var asciidocTableCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "table [PATH]",
Aliases: []string{"tbl"},
Short: "Generate AsciiDoc tables of inputs and outputs",
Annotations: formatAnnotations("asciidoc table"),
RunE: formatRunE,
}
var asciidocDocumentCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "document [PATH]",
Aliases: []string{"doc"},
Short: "Generate AsciiDoc document of inputs and outputs",
Annotations: formatAnnotations("asciidoc document"),
RunE: formatRunE,
}
func init() {
asciidocCmd.PersistentFlags().BoolVar(new(bool), "no-required", false, "do not show \"Required\" column or section")
asciidocCmd.PersistentFlags().BoolVar(new(bool), "no-sensitive", false, "do not show \"Sensitive\" column or section")
asciidocCmd.PersistentFlags().MarkDeprecated("no-required", "use '--required=false' instead") //nolint:errcheck
asciidocCmd.PersistentFlags().MarkDeprecated("no-sensitive", "use '--sensitive=false' instead") //nolint:errcheck
asciidocCmd.PersistentFlags().BoolVar(&settings.ShowRequired, "required", true, "show \"Required\" column or section")
asciidocCmd.PersistentFlags().BoolVar(&settings.ShowSensitivity, "sensitive", true, "show \"Sensitive\" column or section")
asciidocCmd.PersistentFlags().IntVar(&settings.IndentLevel, "indent", 2, "indention level of AsciiDoc sections [1, 2, 3, 4, 5]")
asciidocCmd.AddCommand(asciidocTableCmd)
asciidocCmd.AddCommand(asciidocDocumentCmd)
rootCmd.AddCommand(asciidocCmd)
}

39
cmd/asciidoc/asciidoc.go Normal file
View File

@@ -0,0 +1,39 @@
package asciidoc
import (
"github.com/spf13/cobra"
"github.com/segmentio/terraform-docs/cmd/asciidoc/document"
"github.com/segmentio/terraform-docs/cmd/asciidoc/table"
"github.com/segmentio/terraform-docs/internal/cli"
)
// NewCommand returns a new cobra.Command for 'asciidoc' formatter
func NewCommand(config *cli.Config) *cobra.Command {
cmd := &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "asciidoc [PATH]",
Aliases: []string{"adoc"},
Short: "Generate AsciiDoc of inputs and outputs",
Annotations: cli.Annotations("asciidoc"),
PreRunE: cli.PreRunEFunc(config),
RunE: cli.RunEFunc(config),
}
// flags
cmd.PersistentFlags().BoolVar(&config.Settings.Required, "required", true, "show \"Required\" column or section")
cmd.PersistentFlags().BoolVar(&config.Settings.Sensitive, "sensitive", true, "show \"Sensitive\" column or section")
cmd.PersistentFlags().IntVar(&config.Settings.Indent, "indent", 2, "indention level of AsciiDoc sections [1, 2, 3, 4, 5]")
// deprecation
cmd.PersistentFlags().BoolVar(new(bool), "no-required", false, "do not show \"Required\" column or section")
cmd.PersistentFlags().BoolVar(new(bool), "no-sensitive", false, "do not show \"Sensitive\" column or section")
cmd.PersistentFlags().MarkDeprecated("no-required", "use '--required=false' instead") //nolint:errcheck
cmd.PersistentFlags().MarkDeprecated("no-sensitive", "use '--sensitive=false' instead") //nolint:errcheck
// subcommands
cmd.AddCommand(document.NewCommand(config))
cmd.AddCommand(table.NewCommand(config))
return cmd
}

View File

@@ -0,0 +1,21 @@
package document
import (
"github.com/spf13/cobra"
"github.com/segmentio/terraform-docs/internal/cli"
)
// NewCommand returns a new cobra.Command for 'asciidoc document' formatter
func NewCommand(config *cli.Config) *cobra.Command {
cmd := &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "document [PATH]",
Aliases: []string{"doc"},
Short: "Generate AsciiDoc document of inputs and outputs",
Annotations: cli.Annotations("asciidoc document"),
PreRunE: cli.PreRunEFunc(config),
RunE: cli.RunEFunc(config),
}
return cmd
}

View File

@@ -0,0 +1,21 @@
package table
import (
"github.com/spf13/cobra"
"github.com/segmentio/terraform-docs/internal/cli"
)
// NewCommand returns a new cobra.Command for 'asciidoc table' formatter
func NewCommand(config *cli.Config) *cobra.Command {
cmd := &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "table [PATH]",
Aliases: []string{"tbl"},
Short: "Generate AsciiDoc tables of inputs and outputs",
Annotations: cli.Annotations("asciidoc table"),
PreRunE: cli.PreRunEFunc(config),
RunE: cli.RunEFunc(config),
}
return cmd
}

View File

@@ -1,40 +0,0 @@
package cmd
import (
"os"
"github.com/spf13/cobra"
)
var completionCmd = &cobra.Command{
Args: cobra.NoArgs,
Use: "completion SHELL",
Short: "Generate autocomplete for terraform-docs",
Long: "Generate autocomplete for terraform-docs",
}
var bashCompletionCmd = &cobra.Command{
Args: cobra.NoArgs,
Use: "bash",
Short: "Generate autocomplete for bash",
Long: "Generate autocomplete for bash",
Run: func(cmd *cobra.Command, args []string) {
_ = rootCmd.GenBashCompletion(os.Stdout)
},
}
var zshCompletionCmd = &cobra.Command{
Args: cobra.NoArgs,
Use: "zsh",
Short: "Generate autocomplete for zsh",
Long: "Generate autocomplete for zsh",
Run: func(cmd *cobra.Command, args []string) {
_ = rootCmd.GenZshCompletion(os.Stdout)
},
}
func init() {
completionCmd.AddCommand(bashCompletionCmd)
completionCmd.AddCommand(zshCompletionCmd)
rootCmd.AddCommand(completionCmd)
}

View File

@@ -0,0 +1,20 @@
package bash
import (
"os"
"github.com/spf13/cobra"
)
// NewCommand returns a new cobra.Command for 'completion bash' command
func NewCommand() *cobra.Command {
cmd := &cobra.Command{
Args: cobra.NoArgs,
Use: "bash",
Short: "Generate shell completion for bash",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Parent().Parent().GenBashCompletion(os.Stdout)
},
}
return cmd
}

View File

@@ -0,0 +1,40 @@
package completion
import (
"github.com/spf13/cobra"
"github.com/segmentio/terraform-docs/cmd/completion/bash"
"github.com/segmentio/terraform-docs/cmd/completion/zsh"
)
// NewCommand returns a new cobra.Command for 'completion' command
func NewCommand() *cobra.Command {
cmd := &cobra.Command{
Args: cobra.NoArgs,
Use: "completion SHELL",
Short: "Generate shell completion code for the specified shell (bash or zsh)",
Long: longDescription,
}
// subcommands
cmd.AddCommand(bash.NewCommand())
cmd.AddCommand(zsh.NewCommand())
return cmd
}
const longDescription = `Outputs terraform-doc shell completion for the given shell (bash or zsh)
This depends on the bash-completion binary. Example installation instructions:
# for bash users
$ terraform-doc completion bash > ~/.terraform-doc-completion
$ source ~/.terraform-doc-completion
# for zsh users
% terraform-doc completion zsh > /usr/local/share/zsh/site-functions/_terraform-doc
% autoload -U compinit && compinit
# or if zsh-completion is installed via homebrew
% terraform-doc completion zsh > "${fpath[1]}/_terraform-doc"
Additionally, you may want to output the completion to a file and source in your .bashrc
Note for zsh users: [1] zsh completions are only supported in versions of zsh >= 5.2
`

20
cmd/completion/zsh/zsh.go Normal file
View File

@@ -0,0 +1,20 @@
package zsh
import (
"os"
"github.com/spf13/cobra"
)
// NewCommand returns a new cobra.Command for 'completion zsh' command
func NewCommand() *cobra.Command {
cmd := &cobra.Command{
Args: cobra.NoArgs,
Use: "zsh",
Short: "Generate shel completion for zsh",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Parent().Parent().GenZshCompletion(os.Stdout)
},
}
return cmd
}

View File

@@ -1,22 +0,0 @@
package cmd
import (
"github.com/spf13/cobra"
)
var jsonCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "json [PATH]",
Short: "Generate JSON of inputs and outputs",
Annotations: formatAnnotations("json"),
RunE: formatRunE,
}
func init() {
jsonCmd.PersistentFlags().BoolVar(new(bool), "no-escape", false, "do not escape special characters")
jsonCmd.PersistentFlags().MarkDeprecated("no-escape", "use '--escape=false' instead") //nolint:errcheck
jsonCmd.PersistentFlags().BoolVar(&settings.EscapeCharacters, "escape", true, "escape special characters")
rootCmd.AddCommand(jsonCmd)
}

28
cmd/json/json.go Normal file
View File

@@ -0,0 +1,28 @@
package json
import (
"github.com/spf13/cobra"
"github.com/segmentio/terraform-docs/internal/cli"
)
// NewCommand returns a new cobra.Command for 'json' formatter
func NewCommand(config *cli.Config) *cobra.Command {
cmd := &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "json [PATH]",
Short: "Generate JSON of inputs and outputs",
Annotations: cli.Annotations("json"),
PreRunE: cli.PreRunEFunc(config),
RunE: cli.RunEFunc(config),
}
// flags
cmd.PersistentFlags().BoolVar(&config.Settings.Escape, "escape", true, "escape special characters")
// deprecation
cmd.PersistentFlags().BoolVar(new(bool), "no-escape", false, "do not escape special characters")
cmd.PersistentFlags().MarkDeprecated("no-escape", "use '--escape=false' instead") //nolint:errcheck
return cmd
}

View File

@@ -1,51 +0,0 @@
package cmd
import (
"github.com/spf13/cobra"
)
var markdownCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "markdown [PATH]",
Aliases: []string{"md"},
Short: "Generate Markdown of inputs and outputs",
Annotations: formatAnnotations("markdown"),
RunE: formatRunE,
}
var markdownTableCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "table [PATH]",
Aliases: []string{"tbl"},
Short: "Generate Markdown tables of inputs and outputs",
Annotations: formatAnnotations("markdown table"),
RunE: formatRunE,
}
var markdownDocumentCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "document [PATH]",
Aliases: []string{"doc"},
Short: "Generate Markdown document of inputs and outputs",
Annotations: formatAnnotations("markdown document"),
RunE: formatRunE,
}
func init() {
markdownCmd.PersistentFlags().BoolVar(new(bool), "no-required", false, "do not show \"Required\" column or section")
markdownCmd.PersistentFlags().BoolVar(new(bool), "no-sensitive", false, "do not show \"Sensitive\" column or section")
markdownCmd.PersistentFlags().BoolVar(new(bool), "no-escape", false, "do not escape special characters")
markdownCmd.PersistentFlags().MarkDeprecated("no-required", "use '--required=false' instead") //nolint:errcheck
markdownCmd.PersistentFlags().MarkDeprecated("no-sensitive", "use '--sensitive=false' instead") //nolint:errcheck
markdownCmd.PersistentFlags().MarkDeprecated("no-escape", "use '--escape=false' instead") //nolint:errcheck
markdownCmd.PersistentFlags().BoolVar(&settings.ShowRequired, "required", true, "show \"Required\" column or section")
markdownCmd.PersistentFlags().BoolVar(&settings.ShowSensitivity, "sensitive", true, "show \"Sensitive\" column or section")
markdownCmd.PersistentFlags().BoolVar(&settings.EscapeCharacters, "escape", true, "escape special characters")
markdownCmd.PersistentFlags().IntVar(&settings.IndentLevel, "indent", 2, "indention level of Markdown sections [1, 2, 3, 4, 5]")
markdownCmd.AddCommand(markdownTableCmd)
markdownCmd.AddCommand(markdownDocumentCmd)
rootCmd.AddCommand(markdownCmd)
}

View File

@@ -0,0 +1,21 @@
package document
import (
"github.com/spf13/cobra"
"github.com/segmentio/terraform-docs/internal/cli"
)
// NewCommand returns a new cobra.Command for 'markdown document' formatter
func NewCommand(config *cli.Config) *cobra.Command {
cmd := &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "document [PATH]",
Aliases: []string{"doc"},
Short: "Generate Markdown document of inputs and outputs",
Annotations: cli.Annotations("markdown document"),
PreRunE: cli.PreRunEFunc(config),
RunE: cli.RunEFunc(config),
}
return cmd
}

42
cmd/markdown/markdown.go Normal file
View File

@@ -0,0 +1,42 @@
package markdown
import (
"github.com/spf13/cobra"
"github.com/segmentio/terraform-docs/cmd/markdown/document"
"github.com/segmentio/terraform-docs/cmd/markdown/table"
"github.com/segmentio/terraform-docs/internal/cli"
)
// NewCommand returns a new cobra.Command for 'markdown' formatter
func NewCommand(config *cli.Config) *cobra.Command {
cmd := &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "markdown [PATH]",
Aliases: []string{"md"},
Short: "Generate Markdown of inputs and outputs",
Annotations: cli.Annotations("markdown"),
PreRunE: cli.PreRunEFunc(config),
RunE: cli.RunEFunc(config),
}
// flags
cmd.PersistentFlags().BoolVar(&config.Settings.Required, "required", true, "show \"Required\" column or section")
cmd.PersistentFlags().BoolVar(&config.Settings.Sensitive, "sensitive", true, "show \"Sensitive\" column or section")
cmd.PersistentFlags().BoolVar(&config.Settings.Escape, "escape", true, "escape special characters")
cmd.PersistentFlags().IntVar(&config.Settings.Indent, "indent", 2, "indention level of Markdown sections [1, 2, 3, 4, 5]")
// deprecation
cmd.PersistentFlags().BoolVar(new(bool), "no-required", false, "do not show \"Required\" column or section")
cmd.PersistentFlags().BoolVar(new(bool), "no-sensitive", false, "do not show \"Sensitive\" column or section")
cmd.PersistentFlags().BoolVar(new(bool), "no-escape", false, "do not escape special characters")
cmd.PersistentFlags().MarkDeprecated("no-required", "use '--required=false' instead") //nolint:errcheck
cmd.PersistentFlags().MarkDeprecated("no-sensitive", "use '--sensitive=false' instead") //nolint:errcheck
cmd.PersistentFlags().MarkDeprecated("no-escape", "use '--escape=false' instead") //nolint:errcheck
// subcommands
cmd.AddCommand(document.NewCommand(config))
cmd.AddCommand(table.NewCommand(config))
return cmd
}

View File

@@ -0,0 +1,21 @@
package table
import (
"github.com/spf13/cobra"
"github.com/segmentio/terraform-docs/internal/cli"
)
// NewCommand returns a new cobra.Command for 'markdown table' formatter
func NewCommand(config *cli.Config) *cobra.Command {
cmd := &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "table [PATH]",
Aliases: []string{"tbl"},
Short: "Generate Markdown tables of inputs and outputs",
Annotations: cli.Annotations("markdown table"),
PreRunE: cli.PreRunEFunc(config),
RunE: cli.RunEFunc(config),
}
return cmd
}

View File

@@ -1,22 +0,0 @@
package cmd
import (
"github.com/spf13/cobra"
)
var prettyCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "pretty [PATH]",
Short: "Generate colorized pretty of inputs and outputs",
Annotations: formatAnnotations("pretty"),
RunE: formatRunE,
}
func init() {
prettyCmd.PersistentFlags().BoolVar(new(bool), "no-color", false, "do not colorize printed result")
prettyCmd.PersistentFlags().MarkDeprecated("no-color", "use '--color=false' instead") //nolint:errcheck
prettyCmd.PersistentFlags().BoolVar(&settings.ShowColor, "color", true, "colorize printed result")
rootCmd.AddCommand(prettyCmd)
}

28
cmd/pretty/pretty.go Normal file
View File

@@ -0,0 +1,28 @@
package pretty
import (
"github.com/spf13/cobra"
"github.com/segmentio/terraform-docs/internal/cli"
)
// NewCommand returns a new cobra.Command for pretty formatter
func NewCommand(config *cli.Config) *cobra.Command {
cmd := &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "pretty [PATH]",
Short: "Generate colorized pretty of inputs and outputs",
Annotations: cli.Annotations("pretty"),
PreRunE: cli.PreRunEFunc(config),
RunE: cli.RunEFunc(config),
}
// flags
cmd.PersistentFlags().BoolVar(&config.Settings.Color, "color", true, "colorize printed result")
// deprecation
cmd.PersistentFlags().BoolVar(new(bool), "no-color", false, "do not colorize printed result")
cmd.PersistentFlags().MarkDeprecated("no-color", "use '--color=false' instead") //nolint:errcheck
return cmd
}

View File

@@ -2,180 +2,85 @@ package cmd
import (
"fmt"
"strings"
"github.com/spf13/cobra"
"github.com/segmentio/terraform-docs/internal/format"
"github.com/segmentio/terraform-docs/internal/module"
"github.com/segmentio/terraform-docs/internal/version"
"github.com/segmentio/terraform-docs/pkg/print"
"github.com/segmentio/terraform-docs/cmd/asciidoc"
"github.com/segmentio/terraform-docs/cmd/completion"
"github.com/segmentio/terraform-docs/cmd/json"
"github.com/segmentio/terraform-docs/cmd/markdown"
"github.com/segmentio/terraform-docs/cmd/pretty"
"github.com/segmentio/terraform-docs/cmd/tfvars"
"github.com/segmentio/terraform-docs/cmd/toml"
"github.com/segmentio/terraform-docs/cmd/version"
"github.com/segmentio/terraform-docs/cmd/xml"
"github.com/segmentio/terraform-docs/cmd/yaml"
"github.com/segmentio/terraform-docs/internal/cli"
)
var hides []string
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(),
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
oppositeBool := func(name string) bool {
val, _ := cmd.Flags().GetBool(name)
return !val
}
for _, h := range hides {
switch h {
case "header":
case "inputs":
case "outputs":
case "providers":
case "requirements":
default:
return fmt.Errorf("'%s' is not a valid section to hide, available options [header, inputs, outputs, providers, requirements]", h)
}
}
if len(hides) > 0 && contains("header") {
settings.ShowHeader = false
} else {
settings.ShowHeader = oppositeBool("no-header")
}
options.ShowHeader = settings.ShowHeader
if len(hides) > 0 && contains("inputs") {
settings.ShowInputs = false
} else {
settings.ShowInputs = oppositeBool("no-inputs")
}
if len(hides) > 0 && contains("outputs") {
settings.ShowOutputs = false
} else {
settings.ShowOutputs = oppositeBool("no-outputs")
}
if len(hides) > 0 && contains("providers") {
settings.ShowProviders = false
} else {
settings.ShowProviders = oppositeBool("no-providers")
}
if len(hides) > 0 && contains("requirements") {
settings.ShowRequirements = false
} else {
settings.ShowRequirements = oppositeBool("no-requirements")
}
settings.OutputValues = options.OutputValues
if !cmd.Flags().Changed("color") {
settings.ShowColor = oppositeBool("no-color")
}
if !cmd.Flags().Changed("sort") {
settings.SortByName = oppositeBool("no-sort")
}
if !cmd.Flags().Changed("required") {
settings.ShowRequired = oppositeBool("no-required")
}
if !cmd.Flags().Changed("escape") {
settings.EscapeCharacters = oppositeBool("no-escape")
}
if !cmd.Flags().Changed("sensitive") {
settings.ShowSensitivity = oppositeBool("no-sensitive")
}
return nil
},
}
func init() {
rootCmd.PersistentFlags().StringSliceVar(&hides, "hide", []string{}, "hide section [header, inputs, outputs, providers, requirements]")
rootCmd.PersistentFlags().BoolVar(new(bool), "no-header", false, "do not show module header")
rootCmd.PersistentFlags().BoolVar(new(bool), "no-inputs", false, "do not show inputs")
rootCmd.PersistentFlags().BoolVar(new(bool), "no-outputs", false, "do not show outputs")
rootCmd.PersistentFlags().BoolVar(new(bool), "no-providers", false, "do not show providers")
rootCmd.PersistentFlags().BoolVar(new(bool), "no-requirements", false, "do not show module requirements")
rootCmd.PersistentFlags().BoolVar(new(bool), "no-sort", false, "do no sort items")
rootCmd.PersistentFlags().BoolVar(&settings.SortByName, "sort", true, "sort items")
rootCmd.PersistentFlags().BoolVar(&settings.SortByRequired, "sort-by-required", false, "sort items by name and print required ones first (default false)")
rootCmd.PersistentFlags().BoolVar(&settings.SortByType, "sort-by-type", false, "sort items by type of them (default false)")
rootCmd.PersistentFlags().StringVar(&options.HeaderFromFile, "header-from", "main.tf", "relative path of a file to read header from")
rootCmd.PersistentFlags().BoolVar(&options.OutputValues, "output-values", false, "inject output values into outputs (default false)")
rootCmd.PersistentFlags().StringVar(&options.OutputValuesPath, "output-values-from", "", "inject output values from file into outputs")
rootCmd.PersistentFlags().MarkDeprecated("no-header", "use '--hide header' instead") //nolint:errcheck
rootCmd.PersistentFlags().MarkDeprecated("no-inputs", "use '--hide inputs' instead") //nolint:errcheck
rootCmd.PersistentFlags().MarkDeprecated("no-outputs", "use '--hide outputs' instead") //nolint:errcheck
rootCmd.PersistentFlags().MarkDeprecated("no-providers", "use '--hide providers' instead") //nolint:errcheck
rootCmd.PersistentFlags().MarkDeprecated("no-requirements", "use '--hide requirements' instead") //nolint:errcheck
rootCmd.PersistentFlags().MarkDeprecated("no-sort", "use '--sort=false' instead") //nolint:errcheck
}
// 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 := rootCmd.Execute(); err != nil {
if err := NewCommand().Execute(); err != nil {
fmt.Printf("Error: %s\n", err.Error())
return err
}
return nil
}
// RootCmd represents the base command when called without any subcommands
func RootCmd() *cobra.Command {
return rootCmd
}
// NewCommand returns a new cobra.Command for 'root' command
func NewCommand() *cobra.Command {
config := cli.DefaultConfig()
cmd := &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.Full(),
SilenceUsage: true,
SilenceErrors: true,
}
func contains(section string) bool {
for _, h := range hides {
if h == section {
return true
}
}
return false
}
// flags
cmd.PersistentFlags().StringSliceVar(&config.Sections.Hide, "hide", []string{}, "hide section [header, inputs, outputs, providers, requirements]")
var formatRunE = func(cmd *cobra.Command, args []string) error {
name := strings.Replace(cmd.CommandPath(), "terraform-docs ", "", -1)
printer, err := format.Factory(name, settings)
if err != nil {
return err
}
_, err = options.With(&module.Options{
Path: args[0],
SortBy: &module.SortBy{
Name: settings.SortByName,
Required: settings.SortByRequired,
Type: settings.SortByType,
},
})
if err != nil {
return err
}
tfmodule, err := module.LoadWithOptions(options)
if err != nil {
return err
}
output, err := printer.Print(tfmodule, settings)
if err != nil {
return err
}
fmt.Println(output)
return nil
}
cmd.PersistentFlags().BoolVar(&config.Sort.Enabled, "sort", true, "sort items")
cmd.PersistentFlags().BoolVar(&config.Sort.By.Required, "sort-by-required", false, "sort items by name and print required ones first (default false)")
cmd.PersistentFlags().BoolVar(&config.Sort.By.Type, "sort-by-type", false, "sort items by type of them (default false)")
var formatAnnotations = func(cmd string) map[string]string {
annotations := make(map[string]string)
for _, s := range strings.Split(cmd, " ") {
annotations["command"] = s
}
annotations["kind"] = "formatter"
return annotations
cmd.PersistentFlags().StringVar(&config.HeaderFrom, "header-from", "main.tf", "relative path of a file to read header from")
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 \"\")")
// deprecation
cmd.PersistentFlags().BoolVar(new(bool), "no-header", false, "do not show module header")
cmd.PersistentFlags().BoolVar(new(bool), "no-inputs", false, "do not show inputs")
cmd.PersistentFlags().BoolVar(new(bool), "no-outputs", false, "do not show outputs")
cmd.PersistentFlags().BoolVar(new(bool), "no-providers", false, "do not show providers")
cmd.PersistentFlags().BoolVar(new(bool), "no-requirements", false, "do not show module requirements")
cmd.PersistentFlags().BoolVar(new(bool), "no-sort", false, "do no sort items")
cmd.PersistentFlags().MarkDeprecated("no-header", "use '--hide header' instead") //nolint:errcheck
cmd.PersistentFlags().MarkDeprecated("no-inputs", "use '--hide inputs' instead") //nolint:errcheck
cmd.PersistentFlags().MarkDeprecated("no-outputs", "use '--hide outputs' instead") //nolint:errcheck
cmd.PersistentFlags().MarkDeprecated("no-providers", "use '--hide providers' instead") //nolint:errcheck
cmd.PersistentFlags().MarkDeprecated("no-requirements", "use '--hide requirements' instead") //nolint:errcheck
cmd.PersistentFlags().MarkDeprecated("no-sort", "use '--sort=false' instead") //nolint:errcheck
// formatter subcommands
cmd.AddCommand(asciidoc.NewCommand(config))
cmd.AddCommand(json.NewCommand(config))
cmd.AddCommand(markdown.NewCommand(config))
cmd.AddCommand(pretty.NewCommand(config))
cmd.AddCommand(tfvars.NewCommand(config))
cmd.AddCommand(toml.NewCommand(config))
cmd.AddCommand(xml.NewCommand(config))
cmd.AddCommand(yaml.NewCommand(config))
// other subcommands
cmd.AddCommand(completion.NewCommand())
cmd.AddCommand(version.NewCommand())
return cmd
}

View File

@@ -1,35 +0,0 @@
package cmd
import (
"github.com/spf13/cobra"
)
var tfvarsCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "tfvars [PATH]",
Short: "Generate terraform.tfvars of inputs",
Annotations: formatAnnotations("tfvars"),
}
var tfvarsHCLCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "hcl [PATH]",
Short: "Generate HCL format of terraform.tfvars of inputs",
Annotations: formatAnnotations("tfvars hcl"),
RunE: formatRunE,
}
var tfvarsJSONCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "json [PATH]",
Short: "Generate JSON format of terraform.tfvars of inputs",
Annotations: formatAnnotations("tfvars json"),
RunE: formatRunE,
}
func init() {
tfvarsCmd.AddCommand(tfvarsHCLCmd)
tfvarsCmd.AddCommand(tfvarsJSONCmd)
rootCmd.AddCommand(tfvarsCmd)
}

20
cmd/tfvars/hcl/hcl.go Normal file
View File

@@ -0,0 +1,20 @@
package hcl
import (
"github.com/spf13/cobra"
"github.com/segmentio/terraform-docs/internal/cli"
)
// NewCommand returns a new cobra.Command for 'tfvars hcl' formatter
func NewCommand(config *cli.Config) *cobra.Command {
cmd := &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "hcl [PATH]",
Short: "Generate HCL format of terraform.tfvars of inputs",
Annotations: cli.Annotations("tfvars hcl"),
PreRunE: cli.PreRunEFunc(config),
RunE: cli.RunEFunc(config),
}
return cmd
}

20
cmd/tfvars/json/json.go Normal file
View File

@@ -0,0 +1,20 @@
package json
import (
"github.com/spf13/cobra"
"github.com/segmentio/terraform-docs/internal/cli"
)
// NewCommand returns a new cobra.Command for 'tfvars json' formatter
func NewCommand(config *cli.Config) *cobra.Command {
cmd := &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "json [PATH]",
Short: "Generate JSON format of terraform.tfvars of inputs",
Annotations: cli.Annotations("tfvars json"),
PreRunE: cli.PreRunEFunc(config),
RunE: cli.RunEFunc(config),
}
return cmd
}

25
cmd/tfvars/tfvars.go Normal file
View File

@@ -0,0 +1,25 @@
package tfvars
import (
"github.com/spf13/cobra"
"github.com/segmentio/terraform-docs/cmd/tfvars/hcl"
"github.com/segmentio/terraform-docs/cmd/tfvars/json"
"github.com/segmentio/terraform-docs/internal/cli"
)
// NewCommand returns a new cobra.Command for 'tfvars' formatter
func NewCommand(config *cli.Config) *cobra.Command {
cmd := &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "tfvars [PATH]",
Short: "Generate terraform.tfvars of inputs",
Annotations: cli.Annotations("tfvars"),
}
// subcommands
cmd.AddCommand(hcl.NewCommand(config))
cmd.AddCommand(json.NewCommand(config))
return cmd
}

View File

@@ -1,17 +0,0 @@
package cmd
import (
"github.com/spf13/cobra"
)
var tomlCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "toml [PATH]",
Short: "Generate TOML of inputs and outputs",
Annotations: formatAnnotations("toml"),
RunE: formatRunE,
}
func init() {
rootCmd.AddCommand(tomlCmd)
}

20
cmd/toml/toml.go Normal file
View File

@@ -0,0 +1,20 @@
package toml
import (
"github.com/spf13/cobra"
"github.com/segmentio/terraform-docs/internal/cli"
)
// NewCommand returns a new cobra.Command for 'toml' formatter
func NewCommand(config *cli.Config) *cobra.Command {
cmd := &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "toml [PATH]",
Short: "Generate TOML of inputs and outputs",
Annotations: cli.Annotations("toml"),
PreRunE: cli.PreRunEFunc(config),
RunE: cli.RunEFunc(config),
}
return cmd
}

View File

@@ -1,22 +0,0 @@
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/segmentio/terraform-docs/internal/version"
)
var versionCmd = &cobra.Command{
Args: cobra.NoArgs,
Use: "version",
Short: "Print the version number of terraform-docs",
Run: func(cmd *cobra.Command, args []string) {
fmt.Print(fmt.Sprintf("terraform-docs version %s\n", version.Version()))
},
}
func init() {
rootCmd.AddCommand(versionCmd)
}

27
cmd/version/version.go Normal file
View File

@@ -0,0 +1,27 @@
package version
import (
"fmt"
"github.com/spf13/cobra"
"github.com/segmentio/terraform-docs/internal/version"
)
// NewCommand returns a new cobra.Command for 'version' command
func NewCommand() *cobra.Command {
cmd := &cobra.Command{
Args: cobra.NoArgs,
Use: "version",
Short: "Print the version number of terraform-docs",
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("terraform-docs version %s\n", Full())
},
}
return cmd
}
// Full returns the full version of the binary
func Full() string {
return version.Full()
}

View File

@@ -1,17 +0,0 @@
package cmd
import (
"github.com/spf13/cobra"
)
var xmlCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "xml [PATH]",
Short: "Generate XML of inputs and outputs",
Annotations: formatAnnotations("xml"),
RunE: formatRunE,
}
func init() {
rootCmd.AddCommand(xmlCmd)
}

20
cmd/xml/xml.go Normal file
View File

@@ -0,0 +1,20 @@
package xml
import (
"github.com/spf13/cobra"
"github.com/segmentio/terraform-docs/internal/cli"
)
// NewCommand returns a new cobra.Command for 'xml' formatter
func NewCommand(config *cli.Config) *cobra.Command {
cmd := &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "xml [PATH]",
Short: "Generate XML of inputs and outputs",
Annotations: cli.Annotations("xml"),
PreRunE: cli.PreRunEFunc(config),
RunE: cli.RunEFunc(config),
}
return cmd
}

View File

@@ -1,17 +0,0 @@
package cmd
import (
"github.com/spf13/cobra"
)
var yamlCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "yaml [PATH]",
Short: "Generate YAML of inputs and outputs",
Annotations: formatAnnotations("yaml"),
RunE: formatRunE,
}
func init() {
rootCmd.AddCommand(yamlCmd)
}

20
cmd/yaml/yaml.go Normal file
View File

@@ -0,0 +1,20 @@
package yaml
import (
"github.com/spf13/cobra"
"github.com/segmentio/terraform-docs/internal/cli"
)
// NewCommand returns a new cobra.Command for 'yaml' formatter
func NewCommand(config *cli.Config) *cobra.Command {
cmd := &cobra.Command{
Args: cobra.ExactArgs(1),
Use: "yaml [PATH]",
Short: "Generate YAML of inputs and outputs",
Annotations: cli.Annotations("yaml"),
PreRunE: cli.PreRunEFunc(config),
RunE: cli.RunEFunc(config),
}
return cmd
}