From 047af1928f26b85204d9602eb15c25e60c98f9f5 Mon Sep 17 00:00:00 2001 From: "Victor H. Avelar" Date: Thu, 3 Jun 2021 15:55:45 +0200 Subject: [PATCH] Refactor base code --- commands/helpers.go | 81 +++++++++++++++++++++++++++++++++++++++++++++ commands/mollie.go | 77 +----------------------------------------- 2 files changed, 82 insertions(+), 76 deletions(-) create mode 100644 commands/helpers.go diff --git a/commands/helpers.go b/commands/helpers.go new file mode 100644 index 0000000..68e8cca --- /dev/null +++ b/commands/helpers.go @@ -0,0 +1,81 @@ +package commands + +import ( + "os" + + "github.com/VictorAvelar/mollie-cli/commands/displayers" + "github.com/spf13/cobra" + "github.com/spf13/pflag" +) + +// ParseStringFromFlags returns the string value of a flag by key. +func ParseStringFromFlags(cmd *cobra.Command, key string) string { + val, err := cmd.Flags().GetString(key) + if err != nil { + logger.Fatal(err) + } + return val +} + +// ParseIntFromFlags returns the string value of a flag by key. +func ParseIntFromFlags(cmd *cobra.Command, key string) int { + val, err := cmd.Flags().GetInt(key) + if err != nil { + logger.Fatal(err) + } + return val +} + +// ParsePromptBool returns a boolean to indicate if the values +// should be prompted to the user. +func ParsePromptBool(cmd *cobra.Command) bool { + val, err := cmd.Flags().GetBool("prompt") + if err != nil { + logger.Fatal(err) + } + + return val +} + +// PrintNonemptyFlagValue will log with level info any non empty +// string value. +// The key will be used as name indicator. +// E.g. "using key value: val" +func PrintNonemptyFlagValue(key, val string) { + if val != "" { + logger.Infof("using %s value: %s", key, val) + } +} + +// PrintNonEmptyFlags will print all the defined flags for this +// command, both persistent and local flags will be printed. +func PrintNonEmptyFlags(cmd *cobra.Command) { + cmd.Flags().Visit(printFlagValues) +} + +func printFlagValues(f *pflag.Flag) { + logger.Infof("using %s with value %s", f.Name, f.Value) +} + +// PrintJson dumps the given data as json and then it exits +// gracefully from the execution. +func PrintJson(d interface{}) { + disp := displayers.JsonDisplayer{ + Data: d, + } + + printer.Display(&disp) + os.Exit(0) +} + +// PrintJsonP dumps the given data as pretty json and then it exits +// gracefully from the execution. +func PrintJsonP(d interface{}) { + disp := displayers.JsonDisplayer{ + Data: d, + Pretty: true, + } + + printer.Display(&disp) + os.Exit(0) +} diff --git a/commands/mollie.go b/commands/mollie.go index 70d91b2..267c2eb 100644 --- a/commands/mollie.go +++ b/commands/mollie.go @@ -1,17 +1,14 @@ package commands import ( - "os" "time" "github.com/VictorAvelar/mollie-api-go/v2/mollie" - "github.com/VictorAvelar/mollie-cli/commands/displayers" "github.com/avocatl/admiral/pkg/commander" "github.com/avocatl/admiral/pkg/display" "github.com/mitchellh/go-homedir" "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spf13/pflag" "github.com/spf13/viper" ) @@ -201,7 +198,7 @@ func addPersistentFlags() { func addCommands() { MollieCmd.AddCommand( - Methods(), + methods(), ) // MollieCmd.AddCommand(Profile()) // MollieCmd.AddCommand(Browse()) @@ -218,75 +215,3 @@ func addCommands() { // MollieCmd.AddCommand(Version()) // MollieCmd.AddCommand(Docs()) } - -// ParseStringFromFlags returns the string value of a flag by key. -func ParseStringFromFlags(cmd *cobra.Command, key string) string { - val, err := cmd.Flags().GetString(key) - if err != nil { - logger.Fatal(err) - } - return val -} - -// ParseIntFromFlags returns the string value of a flag by key. -func ParseIntFromFlags(cmd *cobra.Command, key string) int { - val, err := cmd.Flags().GetInt(key) - if err != nil { - logger.Fatal(err) - } - return val -} - -// ParsePromptBool returns a boolean to indicate if the values -// should be prompted to the user. -func ParsePromptBool(cmd *cobra.Command) bool { - val, err := cmd.Flags().GetBool("prompt") - if err != nil { - logger.Fatal(err) - } - - return val -} - -// PrintNonemptyFlagValue will log with level info any non empty -// string value. -// The key will be used as name indicator. -// E.g. "using key value: val" -func PrintNonemptyFlagValue(key, val string) { - if val != "" { - logger.Infof("using %s value: %s", key, val) - } -} - -// PrintNonEmptyFlags will print all the defined flags for this -// command, both persistent and local flags will be printed. -func PrintNonEmptyFlags(cmd *cobra.Command) { - cmd.Flags().Visit(printFlagValues) -} - -func printFlagValues(f *pflag.Flag) { - logger.Infof("using %s with value %s", f.Name, f.Value) -} - -// PrintJson dumps the given data as json and then it exits -// gracefully from the execution. -func PrintJson(d interface{}) { - disp := displayers.JsonDisplayer{ - Data: d, - } - - printer.Display(&disp) - os.Exit(0) -} - -// PrintJsonP dumps the given data as pretty json and then it exits -// gracefully from the execution. -func PrintJsonP(d interface{}) { - disp := displayers.JsonDisplayer{ - Data: d, - Pretty: true, - } - - printer.Display(&disp) - os.Exit(0) -}