Skip to content

Commit

Permalink
Refactor base code
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorAvelar committed Jun 3, 2021
1 parent 418c613 commit 047af19
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 76 deletions.
81 changes: 81 additions & 0 deletions commands/helpers.go
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)
}
77 changes: 1 addition & 76 deletions commands/mollie.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -201,7 +198,7 @@ func addPersistentFlags() {

func addCommands() {
MollieCmd.AddCommand(
Methods(),
methods(),
)
// MollieCmd.AddCommand(Profile())
// MollieCmd.AddCommand(Browse())
Expand All @@ -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)
}

0 comments on commit 047af19

Please sign in to comment.