From 2bd45cc1b8b2c737f95bc67769819c3a232e8409 Mon Sep 17 00:00:00 2001 From: eduardo Date: Mon, 6 Dec 2021 00:20:04 -0300 Subject: [PATCH] add shops command --- 02-refactor-to-cobra/cmd/beers-cli/main.go | 4 +- 02-refactor-to-cobra/internal/cli/beers.go | 43 ---------------------- 02-refactor-to-cobra/internal/cli/shops.go | 43 ++++++++++++++++++++++ 3 files changed, 45 insertions(+), 45 deletions(-) delete mode 100644 02-refactor-to-cobra/internal/cli/beers.go create mode 100644 02-refactor-to-cobra/internal/cli/shops.go diff --git a/02-refactor-to-cobra/cmd/beers-cli/main.go b/02-refactor-to-cobra/cmd/beers-cli/main.go index 4b3ae21..60dddac 100644 --- a/02-refactor-to-cobra/cmd/beers-cli/main.go +++ b/02-refactor-to-cobra/cmd/beers-cli/main.go @@ -6,7 +6,7 @@ import ( ) func main() { - rootCmd := &cobra.Command{Use: "beers-cli"} - rootCmd.AddCommand(cli.InitBeersCmd()) + rootCmd := &cobra.Command{Use: "shops-cli"} + rootCmd.AddCommand(cli.InitShopsCmd()) rootCmd.Execute() } diff --git a/02-refactor-to-cobra/internal/cli/beers.go b/02-refactor-to-cobra/internal/cli/beers.go deleted file mode 100644 index 26c141f..0000000 --- a/02-refactor-to-cobra/internal/cli/beers.go +++ /dev/null @@ -1,43 +0,0 @@ -package cli - -import ( - "fmt" - - "github.com/spf13/cobra" -) - -// CobraFn function definion of run cobra command -type CobraFn func(cmd *cobra.Command, args []string) - -var beers = map[string]string{ - "01D9X58E7NPXX5MVCR9QN794CH": "Mad Jack Mixer", - "01D9X5BQ5X48XMMVZ2F2G3R5MS": "Keystone Ice", - "01D9X5CVS1M9VR5ZD627XDF6ND": "Belgian Moon", -} - -const idFlag = "id" - -// InitBeersCmd initialize beers command -func InitBeersCmd() *cobra.Command { - beersCmd := &cobra.Command{ - Use: "beers", - Short: "Print data about beers", - Run: runBeersFn(), - } - - beersCmd.Flags().StringP(idFlag, "i", "", "id of the beer") - - return beersCmd -} - -func runBeersFn() CobraFn { - return func(cmd *cobra.Command, args []string) { - id, _ := cmd.Flags().GetString(idFlag) - - if id != "" { - fmt.Println(beers[id]) - } else { - fmt.Println(beers) - } - } -} diff --git a/02-refactor-to-cobra/internal/cli/shops.go b/02-refactor-to-cobra/internal/cli/shops.go new file mode 100644 index 0000000..18d6126 --- /dev/null +++ b/02-refactor-to-cobra/internal/cli/shops.go @@ -0,0 +1,43 @@ +package cli + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +// CobraFn function definion of run cobra command +type CobraFn func(cmd *cobra.Command, args []string) + +var shops = map[string]string{ + "01DC9ZAPGKEQJS4P4A48EG3P43": "Mercadona", + "01DC9ZB23EW0J0ARAER09SJDKC": "Carrefour", + "01DC9ZB89V1PQD977ZE6QXSQHH": "Alcampo", +} + +const idFlag = "id" + +// InitshopsCmd initialize beers command +func InitShopsCmd() *cobra.Command { + shopsCmd := &cobra.Command{ + Use: "shops", + Short: "Print data about shops", + Run: runShopsFn(), + } + + shopsCmd.Flags().StringP(idFlag, "i", "", "id of the shop") + + return shopsCmd +} + +func runShopsFn() CobraFn { + return func(cmd *cobra.Command, args []string) { + id, _ := cmd.Flags().GetString(idFlag) + + if id != "" { + fmt.Println(shops[id]) + } else { + fmt.Println(shops) + } + } +}