commands: Make convert command non-global

See #4598
This commit is contained in:
Bjørn Erik Pedersen 2018-04-09 18:49:13 +02:00
parent 7bc5e89fba
commit 4b780ca778
No known key found for this signature in database
GPG key ID: 330E6E2BD4859D8F
2 changed files with 51 additions and 40 deletions

View file

@ -28,56 +28,67 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
var (
_ cmder = (*convertCmd)(nil)
)
var outputDir string var outputDir string
var unsafe bool var unsafe bool
var convertCmd = &cobra.Command{ type convertCmd struct {
Use: "convert", cmd *cobra.Command
Short: "Convert your content to different formats", }
Long: `Convert your content (e.g. front matter) to different formats.
func newConvertCmd() *convertCmd {
cmd := &cobra.Command{
Use: "convert",
Short: "Convert your content to different formats",
Long: `Convert your content (e.g. front matter) to different formats.
See convert's subcommands toJSON, toTOML and toYAML for more information.`, See convert's subcommands toJSON, toTOML and toYAML for more information.`,
RunE: nil, RunE: nil,
} }
var toJSONCmd = &cobra.Command{ cmd.AddCommand(
Use: "toJSON", &cobra.Command{
Short: "Convert front matter to JSON", Use: "toJSON",
Long: `toJSON converts all front matter in the content directory Short: "Convert front matter to JSON",
Long: `toJSON converts all front matter in the content directory
to use JSON for the front matter.`, to use JSON for the front matter.`,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return convertContents(rune([]byte(parser.JSONLead)[0])) return convertContents(rune([]byte(parser.JSONLead)[0]))
}, },
} },
&cobra.Command{
var toTOMLCmd = &cobra.Command{ Use: "toTOML",
Use: "toTOML", Short: "Convert front matter to TOML",
Short: "Convert front matter to TOML", Long: `toTOML converts all front matter in the content directory
Long: `toTOML converts all front matter in the content directory
to use TOML for the front matter.`, to use TOML for the front matter.`,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return convertContents(rune([]byte(parser.TOMLLead)[0])) return convertContents(rune([]byte(parser.TOMLLead)[0]))
}, },
} },
&cobra.Command{
var toYAMLCmd = &cobra.Command{ Use: "toYAML",
Use: "toYAML", Short: "Convert front matter to YAML",
Short: "Convert front matter to YAML", Long: `toYAML converts all front matter in the content directory
Long: `toYAML converts all front matter in the content directory
to use YAML for the front matter.`, to use YAML for the front matter.`,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
return convertContents(rune([]byte(parser.YAMLLead)[0])) return convertContents(rune([]byte(parser.YAMLLead)[0]))
}, },
},
)
cmd.PersistentFlags().StringVarP(&outputDir, "output", "o", "", "filesystem path to write files to")
cmd.PersistentFlags().StringVarP(&source, "source", "s", "", "filesystem path to read files relative from")
cmd.PersistentFlags().BoolVar(&unsafe, "unsafe", false, "enable less safe operations, please backup first")
cmd.PersistentFlags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{})
return &convertCmd{cmd: cmd}
} }
func init() { func (c *convertCmd) getCommand() *cobra.Command {
convertCmd.AddCommand(toJSONCmd) return c.cmd
convertCmd.AddCommand(toTOMLCmd)
convertCmd.AddCommand(toYAMLCmd)
convertCmd.PersistentFlags().StringVarP(&outputDir, "output", "o", "", "filesystem path to write files to")
convertCmd.PersistentFlags().StringVarP(&source, "source", "s", "", "filesystem path to read files relative from")
convertCmd.PersistentFlags().BoolVar(&unsafe, "unsafe", false, "enable less safe operations, please backup first")
convertCmd.PersistentFlags().SetAnnotation("source", cobra.BashCompSubdirsInDir, []string{})
} }
func convertContents(mark rune) error { func convertContents(mark rune) error {

View file

@ -199,7 +199,7 @@ func AddCommands() {
HugoCmd.AddCommand(configCmd) HugoCmd.AddCommand(configCmd)
HugoCmd.AddCommand(newCheckCmd().getCommand()) HugoCmd.AddCommand(newCheckCmd().getCommand())
HugoCmd.AddCommand(newBenchmarkCmd().getCommand()) HugoCmd.AddCommand(newBenchmarkCmd().getCommand())
HugoCmd.AddCommand(convertCmd) HugoCmd.AddCommand(newConvertCmd().getCommand())
HugoCmd.AddCommand(newCmd) HugoCmd.AddCommand(newCmd)
HugoCmd.AddCommand(listCmd) HugoCmd.AddCommand(listCmd)
HugoCmd.AddCommand(importCmd) HugoCmd.AddCommand(importCmd)