Skip to content

Commit

Permalink
Add aliases for Cobra version command (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
mszostok authored Sep 4, 2022
1 parent 822bd50 commit 75daa2c
Show file tree
Hide file tree
Showing 20 changed files with 68 additions and 9 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/examples/screen-cobra-version.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/examples/screen-cobra-version_-h.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/examples/screen-custom-formatting-.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/examples/screen-custom-layout-.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/examples/screen-custom-renderer-.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/examples/screen-plain-.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/examples/screen-printer--ojson.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/examples/screen-printer--oshort.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/examples/screen-printer--oyaml.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/examples/screen-printer-.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/examples/screen-printer-post-hook-.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/examples/screen-upgrade-notice-cobra-version.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/examples/screen-upgrade-notice-custom-version.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions examples/cobra-alias/main.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
7 changes: 5 additions & 2 deletions extension/cobra.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ var example = `
<cli> version
<cli> version -o=json
<cli> version -o=yaml
<cli> version -o=pretty
<cli> 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)
}
Expand All @@ -30,6 +32,7 @@ func NewVersionCobraCmd(opts ...CobraOption) *cobra.Command {
Use: "version",
Short: "Print the CLI version",
Example: strings.ReplaceAll(example, "<cli>", os.Args[0]),
Aliases: options.Aliases,
RunE: func(cmd *cobra.Command, args []string) error {
return verPrinter.Print(cmd.OutOrStdout())
},
Expand Down
31 changes: 26 additions & 5 deletions extension/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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
}
7 changes: 5 additions & 2 deletions magefiles/hack/gallery.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -54,7 +54,9 @@ main() {
sleep 1

capture "plain" ""

capture "cobra" "version"

capture "printer" ""
capture "printer" "-oyaml"
capture "printer" "-ojson"
Expand Down Expand Up @@ -82,6 +84,7 @@ main() {
sleep 1

capture "cobra" "version -h"
capture "cobra-alias" "version -h"
}

main

0 comments on commit 75daa2c

Please sign in to comment.