diff --git a/docs/assets/examples/screen-cobra-alias-version_-h.png b/docs/assets/examples/screen-cobra-alias-version_-h.png new file mode 100644 index 0000000..33e7b8e Binary files /dev/null and b/docs/assets/examples/screen-cobra-alias-version_-h.png differ diff --git a/docs/assets/examples/screen-cobra-version.png b/docs/assets/examples/screen-cobra-version.png index 7c70c3a..85b9474 100644 Binary files a/docs/assets/examples/screen-cobra-version.png and b/docs/assets/examples/screen-cobra-version.png differ diff --git a/docs/assets/examples/screen-cobra-version_-h.png b/docs/assets/examples/screen-cobra-version_-h.png index 88143d5..1b9f01e 100644 Binary files a/docs/assets/examples/screen-cobra-version_-h.png and b/docs/assets/examples/screen-cobra-version_-h.png differ diff --git a/docs/assets/examples/screen-custom-formatting-.png b/docs/assets/examples/screen-custom-formatting-.png index 1f0eced..9990368 100644 Binary files a/docs/assets/examples/screen-custom-formatting-.png and b/docs/assets/examples/screen-custom-formatting-.png differ diff --git a/docs/assets/examples/screen-custom-layout-.png b/docs/assets/examples/screen-custom-layout-.png index 7b1efb2..33aef21 100644 Binary files a/docs/assets/examples/screen-custom-layout-.png and b/docs/assets/examples/screen-custom-layout-.png differ diff --git a/docs/assets/examples/screen-custom-renderer-.png b/docs/assets/examples/screen-custom-renderer-.png index 03c040f..34c51c3 100644 Binary files a/docs/assets/examples/screen-custom-renderer-.png and b/docs/assets/examples/screen-custom-renderer-.png differ diff --git a/docs/assets/examples/screen-plain-.png b/docs/assets/examples/screen-plain-.png index 19f2b89..c49e2e1 100644 Binary files a/docs/assets/examples/screen-plain-.png and b/docs/assets/examples/screen-plain-.png differ diff --git a/docs/assets/examples/screen-printer--ojson.png b/docs/assets/examples/screen-printer--ojson.png index 510877f..169f508 100644 Binary files a/docs/assets/examples/screen-printer--ojson.png and b/docs/assets/examples/screen-printer--ojson.png differ diff --git a/docs/assets/examples/screen-printer--oshort.png b/docs/assets/examples/screen-printer--oshort.png index a1e22ed..598c254 100644 Binary files a/docs/assets/examples/screen-printer--oshort.png and b/docs/assets/examples/screen-printer--oshort.png differ diff --git a/docs/assets/examples/screen-printer--oyaml.png b/docs/assets/examples/screen-printer--oyaml.png index 14efa7d..415cbd4 100644 Binary files a/docs/assets/examples/screen-printer--oyaml.png and b/docs/assets/examples/screen-printer--oyaml.png differ diff --git a/docs/assets/examples/screen-printer-.png b/docs/assets/examples/screen-printer-.png index 396b2b5..0de2183 100644 Binary files a/docs/assets/examples/screen-printer-.png and b/docs/assets/examples/screen-printer-.png differ diff --git a/docs/assets/examples/screen-printer-post-hook-.png b/docs/assets/examples/screen-printer-post-hook-.png index bd118e3..a180021 100644 Binary files a/docs/assets/examples/screen-printer-post-hook-.png and b/docs/assets/examples/screen-printer-post-hook-.png differ diff --git a/docs/assets/examples/screen-upgrade-notice-cobra-version.png b/docs/assets/examples/screen-upgrade-notice-cobra-version.png index 4d6be17..abdb3d2 100644 Binary files a/docs/assets/examples/screen-upgrade-notice-cobra-version.png and b/docs/assets/examples/screen-upgrade-notice-cobra-version.png differ diff --git a/docs/assets/examples/screen-upgrade-notice-cobra-version_-ojson.png b/docs/assets/examples/screen-upgrade-notice-cobra-version_-ojson.png index cd795bf..b197932 100644 Binary files a/docs/assets/examples/screen-upgrade-notice-cobra-version_-ojson.png and b/docs/assets/examples/screen-upgrade-notice-cobra-version_-ojson.png differ diff --git a/docs/assets/examples/screen-upgrade-notice-custom-version.png b/docs/assets/examples/screen-upgrade-notice-custom-version.png index c32a630..99623cd 100644 Binary files a/docs/assets/examples/screen-upgrade-notice-custom-version.png and b/docs/assets/examples/screen-upgrade-notice-custom-version.png differ diff --git a/docs/assets/examples/screen-upgrade-notice-sub-cmd-version_check.png b/docs/assets/examples/screen-upgrade-notice-sub-cmd-version_check.png index 32a32f2..a2b0c32 100644 Binary files a/docs/assets/examples/screen-upgrade-notice-sub-cmd-version_check.png and b/docs/assets/examples/screen-upgrade-notice-sub-cmd-version_check.png differ diff --git a/examples/cobra-alias/main.go b/examples/cobra-alias/main.go new file mode 100644 index 0000000..4906138 --- /dev/null +++ b/examples/cobra-alias/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "os" + + "github.com/spf13/cobra" + + "go.szostok.io/version/extension" +) + +// NewRoot returns a root cobra.Command for the whole CLI. +func NewRoot() *cobra.Command { + cmd := &cobra.Command{ + Use: "example", + Short: "An example CLI built with github.com/spf13/cobra", + } + + cmd.AddCommand( + // you just need to add this, and you are done. + extension.NewVersionCobraCmd( + extension.WithAliasesOptions("v"), + ), + ) + + return cmd +} + +func main() { + if err := NewRoot().Execute(); err != nil { + os.Exit(1) + } +} diff --git a/extension/cobra.go b/extension/cobra.go index 44207e8..e357077 100644 --- a/extension/cobra.go +++ b/extension/cobra.go @@ -13,13 +13,15 @@ var example = ` version version -o=json version -o=yaml - version -o=pretty version -o=short ` // NewVersionCobraCmd returns a root cobra.Command for printing CLI version. func NewVersionCobraCmd(opts ...CobraOption) *cobra.Command { - var options CobraOptions + options := CobraOptions{ + Aliases: []string{"ver"}, + } + for _, customize := range opts { customize.ApplyToCobraOption(&options) } @@ -30,6 +32,7 @@ func NewVersionCobraCmd(opts ...CobraOption) *cobra.Command { Use: "version", Short: "Print the CLI version", Example: strings.ReplaceAll(example, "", os.Args[0]), + Aliases: options.Aliases, RunE: func(cmd *cobra.Command, args []string) error { return verPrinter.Print(cmd.OutOrStdout()) }, diff --git a/extension/opts.go b/extension/opts.go index a93e938..c816166 100644 --- a/extension/opts.go +++ b/extension/opts.go @@ -9,7 +9,7 @@ import ( // It allows having the same functional opt func across all constructors. For example: // // - extension.NewVersionCobraCmd(extension.WithUpgradeNotice(..)) -// - extension.NewVersionCobraCmd(extension.WithUpgradeNotice(..)) +// - extension.NewVersionCobraCmd(extension.WithPrinterOptions(..)) type ( // CobraOption provides an option to set a Cobra options. @@ -21,24 +21,25 @@ type ( // CobraOptions holds Cobra command possible customization settings. CobraOptions struct { PrinterOptions []printer.ContainerOption + Aliases []string } ) // CustomPrinterOptions provides an option to set a custom printer related options across multiple constructors. type CustomPrinterOptions struct { - PrinterOptions []printer.ContainerOption + printerOptions []printer.ContainerOption } // WithPrinterOptions sets a custom printer related options. func WithPrinterOptions(opts ...printer.ContainerOption) *CustomPrinterOptions { return &CustomPrinterOptions{ - PrinterOptions: opts, + printerOptions: opts, } } // ApplyToCobraOption sets a given option for Cobra. func (c *CustomPrinterOptions) ApplyToCobraOption(options *CobraOptions) { - options.PrinterOptions = append(options.PrinterOptions, c.PrinterOptions...) + options.PrinterOptions = append(options.PrinterOptions, c.printerOptions...) } // EnableUpgradeNotice provides an option to enable upgrade notice across multiple constructors. @@ -59,15 +60,35 @@ func WithUpgradeNotice(owner, repo string, opts ...upgrade.Options) *EnableUpgra // ApplyToCobraOption sets a given option for Cobra. // It's a syntax sugar for: -// extension.NewVersionCobraCmd( +// +// extension.NewVersionCobraCmd( // extension.WithPrinterOptions( // printer.WithUpgradeNotice("mszostok", "codeowners-validator"), // ), // ) +// // so you can just do: // extension.NewVersionCobraCmd( +// // extension.WithUpgradeNotice("mszostok", "codeowners-validator"), // ) func (c *EnableUpgradeNotice) ApplyToCobraOption(options *CobraOptions) { options.PrinterOptions = append(options.PrinterOptions, printer.WithUpgradeNotice(c.owner, c.repo, c.upgradeOpts...)) } + +// AliasesOptions provides an option to set a custom printer related options across multiple constructors. +type AliasesOptions struct { + aliases []string +} + +// WithAliasesOptions sets a given aliases for the 'version' command. +func WithAliasesOptions(aliases ...string) *AliasesOptions { + return &AliasesOptions{ + aliases: aliases, + } +} + +// ApplyToCobraOption sets a given option for Cobra. +func (c *AliasesOptions) ApplyToCobraOption(options *CobraOptions) { + options.Aliases = c.aliases +} diff --git a/magefiles/hack/gallery.sh b/magefiles/hack/gallery.sh index 0eb2837..4b129d6 100755 --- a/magefiles/hack/gallery.sh +++ b/magefiles/hack/gallery.sh @@ -42,8 +42,8 @@ capture() { rm -f "$filename" || true # only term: screencapture -ol$(osascript -e 'tell app "iTerm" to id of window 1') test.png - screencapture -x -R0,25,1285,650 "$filename" -# screencapture -l$(osascript -e 'tell app "iTerm" to id of window 1') "$filename" + screencapture -x -R0,25,1285,650 "$filename" + # screencapture -l$(osascript -e 'tell app "iTerm" to id of window 1') "$filename" } main() { @@ -54,7 +54,9 @@ main() { sleep 1 capture "plain" "" + capture "cobra" "version" + capture "printer" "" capture "printer" "-oyaml" capture "printer" "-ojson" @@ -82,6 +84,7 @@ main() { sleep 1 capture "cobra" "version -h" + capture "cobra-alias" "version -h" } main