mirror of
https://github.com/terraform-docs/terraform-docs.git
synced 2026-03-27 04:48:33 +07:00
refactor: Add factory function to return format types (#243)
This commit is contained in:
15
cmd/json.go
15
cmd/json.go
@@ -1,20 +1,15 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/segmentio/terraform-docs/internal/format"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var jsonCmd = &cobra.Command{
|
||||
Args: cobra.ExactArgs(1),
|
||||
Use: "json [PATH]",
|
||||
Short: "Generate JSON of inputs and outputs",
|
||||
Annotations: map[string]string{
|
||||
"kind": "formatter",
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return doPrint(args[0], format.NewJSON(settings))
|
||||
},
|
||||
Args: cobra.ExactArgs(1),
|
||||
Use: "json [PATH]",
|
||||
Short: "Generate JSON of inputs and outputs",
|
||||
Annotations: formatAnnotations("json"),
|
||||
RunE: formatRunE,
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -1,47 +1,34 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/segmentio/terraform-docs/internal/format"
|
||||
"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: map[string]string{
|
||||
"kind": "formatter",
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return doPrint(args[0], format.NewTable(settings))
|
||||
},
|
||||
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: map[string]string{
|
||||
"kind": "formatter",
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return doPrint(args[0], format.NewTable(settings))
|
||||
},
|
||||
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: map[string]string{
|
||||
"kind": "formatter",
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return doPrint(args[0], format.NewDocument(settings))
|
||||
},
|
||||
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() {
|
||||
|
||||
@@ -1,20 +1,15 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/segmentio/terraform-docs/internal/format"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var prettyCmd = &cobra.Command{
|
||||
Args: cobra.ExactArgs(1),
|
||||
Use: "pretty [PATH]",
|
||||
Short: "Generate colorized pretty of inputs and outputs",
|
||||
Annotations: map[string]string{
|
||||
"kind": "formatter",
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return doPrint(args[0], format.NewPretty(settings))
|
||||
},
|
||||
Args: cobra.ExactArgs(1),
|
||||
Use: "pretty [PATH]",
|
||||
Short: "Generate colorized pretty of inputs and outputs",
|
||||
Annotations: formatAnnotations("pretty"),
|
||||
RunE: formatRunE,
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
22
cmd/root.go
22
cmd/root.go
@@ -2,7 +2,9 @@ package cmd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"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"
|
||||
@@ -80,9 +82,14 @@ func RootCmd() *cobra.Command {
|
||||
return rootCmd
|
||||
}
|
||||
|
||||
func doPrint(path string, printer print.Format) error {
|
||||
_, err := options.With(&module.Options{
|
||||
Path: path,
|
||||
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,
|
||||
@@ -102,3 +109,12 @@ func doPrint(path string, printer print.Format) error {
|
||||
fmt.Println(output)
|
||||
return nil
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -1,41 +1,30 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/segmentio/terraform-docs/internal/format"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var tfvarsCmd = &cobra.Command{
|
||||
Args: cobra.ExactArgs(1),
|
||||
Use: "tfvars [PATH]",
|
||||
Short: "Generate terraform.tfvars of inputs",
|
||||
Annotations: map[string]string{
|
||||
"kind": "formatter",
|
||||
},
|
||||
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: map[string]string{
|
||||
"kind": "formatter",
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return doPrint(args[0], format.NewTfvarsHCL(settings))
|
||||
},
|
||||
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: map[string]string{
|
||||
"kind": "formatter",
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return doPrint(args[0], format.NewTfvarsJSON(settings))
|
||||
},
|
||||
Args: cobra.ExactArgs(1),
|
||||
Use: "json [PATH]",
|
||||
Short: "Generate JSON format of terraform.tfvars of inputs",
|
||||
Annotations: formatAnnotations("tfvars json"),
|
||||
RunE: formatRunE,
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
15
cmd/xml.go
15
cmd/xml.go
@@ -1,20 +1,15 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/segmentio/terraform-docs/internal/format"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var xmlCmd = &cobra.Command{
|
||||
Args: cobra.ExactArgs(1),
|
||||
Use: "xml [PATH]",
|
||||
Short: "Generate XML of inputs and outputs",
|
||||
Annotations: map[string]string{
|
||||
"kind": "formatter",
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return doPrint(args[0], format.NewXML(settings))
|
||||
},
|
||||
Args: cobra.ExactArgs(1),
|
||||
Use: "xml [PATH]",
|
||||
Short: "Generate XML of inputs and outputs",
|
||||
Annotations: formatAnnotations("xml"),
|
||||
RunE: formatRunE,
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
15
cmd/yaml.go
15
cmd/yaml.go
@@ -1,20 +1,15 @@
|
||||
package cmd
|
||||
|
||||
import (
|
||||
"github.com/segmentio/terraform-docs/internal/format"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var yamlCmd = &cobra.Command{
|
||||
Args: cobra.ExactArgs(1),
|
||||
Use: "yaml [PATH]",
|
||||
Short: "Generate YAML of inputs and outputs",
|
||||
Annotations: map[string]string{
|
||||
"kind": "formatter",
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return doPrint(args[0], format.NewYAML(settings))
|
||||
},
|
||||
Args: cobra.ExactArgs(1),
|
||||
Use: "yaml [PATH]",
|
||||
Short: "Generate YAML of inputs and outputs",
|
||||
Annotations: formatAnnotations("yaml"),
|
||||
RunE: formatRunE,
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
Reference in New Issue
Block a user