From c5026f70e5a50004fa24acc11206b32ab04ffd86 Mon Sep 17 00:00:00 2001 From: Jorres Tarasov Date: Tue, 11 Jun 2024 15:30:34 +0200 Subject: [PATCH] feat: cli functions shouldn't be internal --- cmd/maintenance.go | 2 +- cmd/maintenance/host.go | 2 +- cmd/restart.go | 2 +- cmd/root.go | 5 ++- cmd/run.go | 2 +- internal/cli/cobra.go | 92 +++++------------------------------------ pkg/cli/cobra.go | 79 +++++++++++++++++++++++++++++++++++ 7 files changed, 97 insertions(+), 87 deletions(-) create mode 100644 pkg/cli/cobra.go diff --git a/cmd/maintenance.go b/cmd/maintenance.go index ac83ead..41dc36c 100644 --- a/cmd/maintenance.go +++ b/cmd/maintenance.go @@ -3,7 +3,7 @@ package cmd import ( "github.com/spf13/cobra" - "github.com/ydb-platform/ydbops/internal/cli" + "github.com/ydb-platform/ydbops/pkg/cli" "github.com/ydb-platform/ydbops/pkg/options" ) diff --git a/cmd/maintenance/host.go b/cmd/maintenance/host.go index ea88454..1f33c5d 100644 --- a/cmd/maintenance/host.go +++ b/cmd/maintenance/host.go @@ -5,7 +5,7 @@ import ( "github.com/spf13/cobra" - "github.com/ydb-platform/ydbops/internal/cli" + "github.com/ydb-platform/ydbops/pkg/cli" "github.com/ydb-platform/ydbops/pkg/client" "github.com/ydb-platform/ydbops/pkg/maintenance" "github.com/ydb-platform/ydbops/pkg/options" diff --git a/cmd/restart.go b/cmd/restart.go index 1bcff31..ea2626f 100644 --- a/cmd/restart.go +++ b/cmd/restart.go @@ -5,7 +5,7 @@ import ( "github.com/spf13/cobra" - "github.com/ydb-platform/ydbops/internal/cli" + "github.com/ydb-platform/ydbops/pkg/cli" "github.com/ydb-platform/ydbops/pkg/client" "github.com/ydb-platform/ydbops/pkg/options" "github.com/ydb-platform/ydbops/pkg/rolling" diff --git a/cmd/root.go b/cmd/root.go index 2dc1c44..88f801f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -7,7 +7,8 @@ import ( "go.uber.org/zap/zapcore" "github.com/ydb-platform/ydbops/cmd/maintenance" - "github.com/ydb-platform/ydbops/internal/cli" + iCli "github.com/ydb-platform/ydbops/internal/cli" + "github.com/ydb-platform/ydbops/pkg/cli" "github.com/ydb-platform/ydbops/pkg/options" ) @@ -77,5 +78,5 @@ func InitRootCmd(logLevelSetter zap.AtomicLevel, logger *zap.SugaredLogger) { registerAllSubcommands(RootCmd) - RootCmd.SetUsageTemplate(cli.UsageTemplate) + RootCmd.SetUsageTemplate(iCli.UsageTemplate) } diff --git a/cmd/run.go b/cmd/run.go index 8ef746f..7f01380 100644 --- a/cmd/run.go +++ b/cmd/run.go @@ -5,7 +5,7 @@ import ( "github.com/spf13/cobra" - "github.com/ydb-platform/ydbops/internal/cli" + "github.com/ydb-platform/ydbops/pkg/cli" "github.com/ydb-platform/ydbops/pkg/client" "github.com/ydb-platform/ydbops/pkg/options" "github.com/ydb-platform/ydbops/pkg/rolling" diff --git a/internal/cli/cobra.go b/internal/cli/cobra.go index eada436..695c3f2 100644 --- a/internal/cli/cobra.go +++ b/internal/cli/cobra.go @@ -8,12 +8,9 @@ import ( "github.com/fatih/color" "github.com/spf13/cobra" "github.com/spf13/pflag" - - "github.com/ydb-platform/ydbops/pkg/options" - "github.com/ydb-platform/ydbops/pkg/profile" ) -func determinePadding(curCommand, subCommandLineNumber, totalCommands int) string { +func DeterminePadding(curCommand, subCommandLineNumber, totalCommands int) string { if curCommand == totalCommands-1 { if subCommandLineNumber == 0 { return "└─ " @@ -27,7 +24,7 @@ func determinePadding(curCommand, subCommandLineNumber, totalCommands int) strin return "│ " } -func generateUsage(cmd *cobra.Command) string { +func GenerateUsage(cmd *cobra.Command) string { boldUsage := color.New(color.Bold).Sprint("Usage:") if cmd == cmd.Root() { return fmt.Sprintf("%s ydbops [global options...] 0 { if cmd == cmd.Root() { - return generateShortGlobalOptions(cmd) + return GenerateShortGlobalOptions(cmd) } - result = append(result, colorizeUsages(cmd)) + result = append(result, ColorizeUsages(cmd)) } if cmd.HasParent() { - result = append(result, generateCommandOptionsMessage(cmd.Parent())...) + result = append(result, GenerateCommandOptionsMessage(cmd.Parent())...) } return result } - -func PopulateProfileDefaultsAndValidate(optsArgs ...options.Options) func(*cobra.Command, []string) error { - return func(cmd *cobra.Command, args []string) error { - rootOpts := options.RootOptionsInstance - err := profile.FillDefaultsFromActiveProfile(rootOpts.ProfileFile, rootOpts.ActiveProfile) - if err != nil { - return err - } - - for _, opts := range optsArgs { - if err := opts.Validate(); err != nil { - return fmt.Errorf("%w\nTry '--help' option for more info", err) - } - } - return nil - } -} - -func RequireSubcommand(cmd *cobra.Command, args []string) error { - if len(args) == 0 { - return fmt.Errorf("you have not selected a subcommand\nTry '--help' option for more info") - } - return nil -} - -func SetDefaultsOn(cmd *cobra.Command) *cobra.Command { - cmd.Flags().SortFlags = false - cmd.PersistentFlags().SortFlags = false - - cobra.AddTemplateFunc("drawNiceTree", func(cmd *cobra.Command) string { - if cmd.HasAvailableSubCommands() { - var builder strings.Builder - builder.WriteString("Subcommands:") - for _, line := range generateCommandTree(cmd, 23) { - builder.WriteString("\n") - builder.WriteString(line) - } - builder.WriteString("\n") - return builder.String() - } - return "" - }) - - cobra.AddTemplateFunc("generateUsage", generateUsage) - - cobra.AddTemplateFunc("listAllFlagsInNiceGroups", func(cmd *cobra.Command) string { - if cmd == cmd.Root() { - return "Global options:\n" + colorizeUsages(cmd) - } - - if cmd.HasAvailableSubCommands() { - return strings.Join(generateShortGlobalOptions(cmd.Root()), "\n") - } - - if cmd.HasFlags() { - return fmt.Sprintf( - "Options:\n%s", - strings.Join(generateCommandOptionsMessage(cmd), "\n"), - ) - } - return "" - }) - - cmd.SetUsageTemplate(UsageTemplate) - - return cmd -} diff --git a/pkg/cli/cobra.go b/pkg/cli/cobra.go new file mode 100644 index 0000000..3c8a6db --- /dev/null +++ b/pkg/cli/cobra.go @@ -0,0 +1,79 @@ +package cli + +import ( + "fmt" + "strings" + + "github.com/spf13/cobra" + + "github.com/ydb-platform/ydbops/internal/cli" + "github.com/ydb-platform/ydbops/pkg/options" + "github.com/ydb-platform/ydbops/pkg/profile" +) + +func PopulateProfileDefaultsAndValidate(optsArgs ...options.Options) func(*cobra.Command, []string) error { + return func(cmd *cobra.Command, args []string) error { + rootOpts := options.RootOptionsInstance + err := profile.FillDefaultsFromActiveProfile(rootOpts.ProfileFile, rootOpts.ActiveProfile) + if err != nil { + return err + } + + for _, opts := range optsArgs { + if err := opts.Validate(); err != nil { + return fmt.Errorf("%w\nTry '--help' option for more info", err) + } + } + return nil + } +} + +func RequireSubcommand(cmd *cobra.Command, args []string) error { + if len(args) == 0 { + return fmt.Errorf("you have not selected a subcommand\nTry '--help' option for more info") + } + return nil +} + +func SetDefaultsOn(cmd *cobra.Command) *cobra.Command { + cmd.Flags().SortFlags = false + cmd.PersistentFlags().SortFlags = false + + cobra.AddTemplateFunc("drawNiceTree", func(cmd *cobra.Command) string { + if cmd.HasAvailableSubCommands() { + var builder strings.Builder + builder.WriteString("Subcommands:") + for _, line := range cli.GenerateCommandTree(cmd, 23) { + builder.WriteString("\n") + builder.WriteString(line) + } + builder.WriteString("\n") + return builder.String() + } + return "" + }) + + cobra.AddTemplateFunc("generateUsage", cli.GenerateUsage) + + cobra.AddTemplateFunc("listAllFlagsInNiceGroups", func(cmd *cobra.Command) string { + if cmd == cmd.Root() { + return "Global options:\n" + cli.ColorizeUsages(cmd) + } + + if cmd.HasAvailableSubCommands() { + return strings.Join(cli.GenerateShortGlobalOptions(cmd.Root()), "\n") + } + + if cmd.HasFlags() { + return fmt.Sprintf( + "Options:\n%s", + strings.Join(cli.GenerateCommandOptionsMessage(cmd), "\n"), + ) + } + return "" + }) + + cmd.SetUsageTemplate(cli.UsageTemplate) + + return cmd +}