Skip to content

Commit

Permalink
commands: config: adds --toml or -t option to output TOML instead…
Browse files Browse the repository at this point in the history
… of YAML
  • Loading branch information
bvobart committed Jun 6, 2021
1 parent 1afabd2 commit c3e9890
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
19 changes: 16 additions & 3 deletions commands/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"github.com/fatih/color"
"github.com/google/go-cmp/cmp"
"github.com/spf13/cobra"
"gopkg.in/yaml.v3"

"github.com/bvobart/mllint/config"
"github.com/bvobart/mllint/utils"
)

var outputToml = false

func NewConfigCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "config [dir]",
Expand All @@ -28,6 +29,7 @@ Specifying %s or %s will cause this command to purely print the current or defau
RunE: runConfig,
Args: cobra.MaximumNArgs(1),
}
cmd.Flags().BoolVarP(&outputToml, "toml", "t", false, "Specify this flag to output the config in TOML format instead of the default is YAML output.")
return cmd
}

Expand All @@ -50,7 +52,12 @@ func runConfig(_ *cobra.Command, args []string) error {
shush(func() { fmt.Print("---\n\n") })

// print the config
output, err := yaml.Marshal(conf)
var output []byte
if outputToml {
output, err = conf.TOML()
} else {
output, err = conf.YAML()
}
if err != nil {
return err
}
Expand All @@ -63,7 +70,13 @@ func runConfig(_ *cobra.Command, args []string) error {
func runConfigDefault() error {
shush(func() { color.Green("Using default configuration\n\n") })

output, err := yaml.Marshal(config.Default())
var output []byte
var err error
if outputToml {
output, err = config.Default().TOML()
} else {
output, err = config.Default().YAML()
}
if err != nil {
return err
}
Expand Down
12 changes: 12 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,15 @@ func ParseTOML(reader io.Reader) (*Config, error) {

return tomlFile.Tool.Mllint, nil
}

//---------------------------------------------------------------------------------------

func (conf *Config) YAML() ([]byte, error) {
return yaml.Marshal(conf)
}

func (conf *Config) TOML() ([]byte, error) {
pyproject := pyprojectTOML{}
pyproject.Tool.Mllint = conf
return toml.Marshal(pyproject)
}

0 comments on commit c3e9890

Please sign in to comment.