-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
418c613
commit 047af19
Showing
2 changed files
with
82 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
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