diff --git a/02-refactor-to-cobra/cmd/beers-cli/main.go b/02-refactor-to-cobra/cmd/beers-cli/main.go index 4b3ae21..e074938 100644 --- a/02-refactor-to-cobra/cmd/beers-cli/main.go +++ b/02-refactor-to-cobra/cmd/beers-cli/main.go @@ -8,5 +8,6 @@ import ( func main() { rootCmd := &cobra.Command{Use: "beers-cli"} rootCmd.AddCommand(cli.InitBeersCmd()) + rootCmd.AddCommand(cli.InitStoresCmd()) rootCmd.Execute() } diff --git a/02-refactor-to-cobra/internal/cli/beers.go b/02-refactor-to-cobra/internal/cli/beers.go index 26c141f..26b73e3 100644 --- a/02-refactor-to-cobra/internal/cli/beers.go +++ b/02-refactor-to-cobra/internal/cli/beers.go @@ -6,17 +6,12 @@ import ( "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{ diff --git a/02-refactor-to-cobra/internal/cli/common.go b/02-refactor-to-cobra/internal/cli/common.go new file mode 100644 index 0000000..01082df --- /dev/null +++ b/02-refactor-to-cobra/internal/cli/common.go @@ -0,0 +1,10 @@ +package cli + +import ( + "github.com/spf13/cobra" +) + +// CobraFn function definion of run cobra command +type CobraFn func(cmd *cobra.Command, args []string) + +const idFlag = "id" diff --git a/02-refactor-to-cobra/internal/cli/stores.go b/02-refactor-to-cobra/internal/cli/stores.go new file mode 100644 index 0000000..b1cf8f0 --- /dev/null +++ b/02-refactor-to-cobra/internal/cli/stores.go @@ -0,0 +1,38 @@ +package cli + +import ( + "fmt" + + "github.com/spf13/cobra" +) + +var stores = map[string]string{ + "01DC9ZAPGKEQJS4P4A48EG3P43": "Mercadona", + "01DC9ZB23EW0J0ARAER09SJDKC": "Carrefour", + "01DC9ZB89V1PQD977ZE6QXSQHH": "Alcampo", +} + +// InitStoresCmd initialize stores command +func InitStoresCmd() *cobra.Command { + storesCmd := &cobra.Command{ + Use: "stores", + Short: "Print data about stores", + Run: runStoresFn(), + } + + storesCmd.Flags().StringP(idFlag, "i", "", "id of the store") + + return storesCmd +} + +func runStoresFn() CobraFn { + return func(cmd *cobra.Command, args []string) { + id, _ := cmd.Flags().GetString(idFlag) + + if id != "" { + fmt.Println(stores[id]) + } else { + fmt.Println(stores) + } + } +}