Skip to content

Feature/add stores command #42

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 3 commits 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
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()
}
5 changes: 0 additions & 5 deletions 02-refactor-to-cobra/internal/cli/beers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
10 changes: 10 additions & 0 deletions 02-refactor-to-cobra/internal/cli/common.go
Original file line number Diff line number Diff line change
@@ -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"
38 changes: 38 additions & 0 deletions 02-refactor-to-cobra/internal/cli/stores.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}