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()
}
32 changes: 32 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 storeIdFlag = "store"

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

// InitStoresCmd initialize stores command
func InitStoresCmd() *cobra.Command {
storeCmd := &cobra.Command{
Use: "stores",
Short: "Print data about stores that sell beers",
Run: runStoresFn(),
}

storeCmd.Flags().StringP(storeIdFlag, "s", "", "id of the store")

return storeCmd
}

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

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

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