Skip to content

Add comando stores a beers.go - Modulo 02 #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
Empty file.
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()
}
29 changes: 29 additions & 0 deletions 02-refactor-to-cobra/internal/cli/beers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,37 @@ var beers = map[string]string{
"01D9X5CVS1M9VR5ZD627XDF6ND": "Belgian Moon",
}

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

const idFlag = "id"

// El comando de tiendas que enseñan las cervezas etc etc
func InitStoresCmd() *cobra.Command {
storesCmd := &cobra.Command{
Use: "stores",
Short: "Print data about stores that sells beers",
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)
}
}
}

// InitBeersCmd initialize beers command
func InitBeersCmd() *cobra.Command {
beersCmd := &cobra.Command{
Expand Down
6 changes: 2 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ module github.com/CodelyTV/golang-examples
go 1.12

require (
github.com/json-iterator/go v1.1.11
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/json-iterator/go v1.1.12
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v0.0.7
github.com/spf13/cobra v1.3.0
github.com/stretchr/testify v1.7.0
)
Loading