-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cli functions shouldn't be internal
- Loading branch information
Showing
7 changed files
with
97 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |