From 4b780ca778ee7f25af808da38ede964a01698c70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 9 Apr 2018 18:49:13 +0200 Subject: [PATCH] commands: Make convert command non-global See #4598 --- commands/convert.go | 89 +++++++++++++++++++++++++-------------------- commands/hugo.go | 2 +- 2 files changed, 51 insertions(+), 40 deletions(-) diff --git a/commands/convert.go b/commands/convert.go index f63f8522f..cc07fe087 100644 --- a/commands/convert.go +++ b/commands/convert.go @@ -28,56 +28,67 @@ import ( "github.com/spf13/cobra" ) +var ( + _ cmder = (*convertCmd)(nil) +) + var outputDir string var unsafe bool -var convertCmd = &cobra.Command{ - Use: "convert", - Short: "Convert your content to different formats", - Long: `Convert your content (e.g. front matter) to different formats. +type convertCmd struct { + cmd *cobra.Command +} + +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.`, - RunE: nil, -} + RunE: nil, + } -var toJSONCmd = &cobra.Command{ - Use: "toJSON", - Short: "Convert front matter to JSON", - Long: `toJSON converts all front matter in the content directory + cmd.AddCommand( + &cobra.Command{ + Use: "toJSON", + Short: "Convert front matter to JSON", + Long: `toJSON converts all front matter in the content directory to use JSON for the front matter.`, - RunE: func(cmd *cobra.Command, args []string) error { - return convertContents(rune([]byte(parser.JSONLead)[0])) - }, -} - -var toTOMLCmd = &cobra.Command{ - Use: "toTOML", - Short: "Convert front matter to TOML", - Long: `toTOML converts all front matter in the content directory + RunE: func(cmd *cobra.Command, args []string) error { + return convertContents(rune([]byte(parser.JSONLead)[0])) + }, + }, + &cobra.Command{ + Use: "toTOML", + Short: "Convert front matter to TOML", + Long: `toTOML converts all front matter in the content directory to use TOML for the front matter.`, - RunE: func(cmd *cobra.Command, args []string) error { - return convertContents(rune([]byte(parser.TOMLLead)[0])) - }, -} - -var toYAMLCmd = &cobra.Command{ - Use: "toYAML", - Short: "Convert front matter to YAML", - Long: `toYAML converts all front matter in the content directory + RunE: func(cmd *cobra.Command, args []string) error { + return convertContents(rune([]byte(parser.TOMLLead)[0])) + }, + }, + &cobra.Command{ + Use: "toYAML", + Short: "Convert front matter to YAML", + Long: `toYAML converts all front matter in the content directory to use YAML for the front matter.`, - RunE: func(cmd *cobra.Command, args []string) error { - return convertContents(rune([]byte(parser.YAMLLead)[0])) - }, + RunE: func(cmd *cobra.Command, args []string) error { + 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() { - convertCmd.AddCommand(toJSONCmd) - 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 (c *convertCmd) getCommand() *cobra.Command { + return c.cmd } func convertContents(mark rune) error { diff --git a/commands/hugo.go b/commands/hugo.go index 5db5239ea..f4fbe45da 100644 --- a/commands/hugo.go +++ b/commands/hugo.go @@ -199,7 +199,7 @@ func AddCommands() { HugoCmd.AddCommand(configCmd) HugoCmd.AddCommand(newCheckCmd().getCommand()) HugoCmd.AddCommand(newBenchmarkCmd().getCommand()) - HugoCmd.AddCommand(convertCmd) + HugoCmd.AddCommand(newConvertCmd().getCommand()) HugoCmd.AddCommand(newCmd) HugoCmd.AddCommand(listCmd) HugoCmd.AddCommand(importCmd)