Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions 02-refactor-to-cobra/cmd/beers-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ import (
func main() {
rootCmd := &cobra.Command{Use: "beers-cli"}
rootCmd.AddCommand(cli.InitBeersCmd())
rootCmd.AddCommand(cli.InitStoresCmd())
rootCmd.Execute()
}
31 changes: 31 additions & 0 deletions 02-refactor-to-cobra/internal/cli/beers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@ var beers = map[string]string{
"01D9X5CVS1M9VR5ZD627XDF6ND": "Belgian Moon",
}

var stores = map[string]string{
"01DC9ZAPGKEQJS4P4A48EG3P43": "Mercadona",
"01DC9ZB23EW0J0ARAER09SJDKC": "Carrefour",
"01DC9ZB89V1PQD977ZE6QXSQHH": "Alcampo",
}

const idFlag = "id"
const idStore = "id"

// InitBeersCmd initialize beers command
func InitBeersCmd() *cobra.Command {
Expand All @@ -30,6 +37,18 @@ func InitBeersCmd() *cobra.Command {
return beersCmd
}

func InitStoresCmd() *cobra.Command {
storesCmd := &cobra.Command{
Use: "stores",
Short: "Print data about the stores",
Run: runStoresFn(),
}

storesCmd.Flags().StringP(idStore, "i", "", "id of the store")

return storesCmd
}

func runBeersFn() CobraFn {
return func(cmd *cobra.Command, args []string) {
id, _ := cmd.Flags().GetString(idFlag)
Expand All @@ -41,3 +60,15 @@ func runBeersFn() CobraFn {
}
}
}

func runStoresFn() CobraFn {
return func(cmd *cobra.Command, args []string) {
id, _ := cmd.Flags().GetString(idStore)

if id != "" {
fmt.Println(stores[id])
} else {
fmt.Println(stores)
}
}
}