Skip to content

feat:New store command #69

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 .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
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()
}
23 changes: 2 additions & 21 deletions 02-refactor-to-cobra/internal/cli/beers.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,24 @@
package cli

import (
"fmt"

"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{
Use: "beers",
Short: "Print data about beers",
Run: runBeersFn(),
Run: runCommandFn(beers),
}

beersCmd.Flags().StringP(idFlag, "i", "", "id of the beer")
beersCmd.Flags().StringP(idFlag, shortIdFlag, "", "id of the beer")

return beersCmd
}

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

if id != "" {
fmt.Println(beers[id])
} else {
fmt.Println(beers)
}
}
}
23 changes: 23 additions & 0 deletions 02-refactor-to-cobra/internal/cli/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cli

import (
"fmt"
"github.com/spf13/cobra"
)

const idFlag = "id"
const shortIdFlag = "i"

type CobraFn func(cmd *cobra.Command, args []string)

func runCommandFn(values map[string]string) CobraFn {
return func(cmd *cobra.Command, args []string) {
id, _ := cmd.Flags().GetString(idFlag)

if id != "" {
fmt.Println(values[id])
} else {
fmt.Println(values)
}
}
}
23 changes: 23 additions & 0 deletions 02-refactor-to-cobra/internal/cli/stores.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package cli

import (
"github.com/spf13/cobra"
)

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

func InitStoresCmd() *cobra.Command {
storesCmd := &cobra.Command{
Use: "stores",
Short: "Print store identify",
Run: runCommandFn(stores),
}

storesCmd.Flags().StringP(idFlag, shortIdFlag, "", "id of the Store")

return storesCmd
}
2 changes: 1 addition & 1 deletion 07-behaviour_error_handling/cmd/beers-cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func main() {

csvData := flag.Bool("csv", false, "load data from csv")
csvData := flag.Bool("csv", true, "load data from csv")
flag.Parse()
var repo beerscli.BeerRepo
repo = csv.NewRepository()
Expand Down
3 changes: 2 additions & 1 deletion 07-behaviour_error_handling/internal/beer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package beerscli

import (
"encoding/json"
"github.com/CodelyTV/golang-examples/07-behaviour_error_handling/internal/errors"
)

// Beer representation of beer into data struct
Expand Down Expand Up @@ -66,7 +67,7 @@ func (t *BeerType) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return err
return errors.WrapJsonProcessingDataError(err, "can't parser bytes to Json")
}
*t = toID[j]
return nil
Expand Down
5 changes: 4 additions & 1 deletion 07-behaviour_error_handling/internal/cli/beers.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ func InitBeersCmd(repository beerscli.BeerRepo) *cobra.Command {
func runBeersFn(repository beerscli.BeerRepo) CobraFn {
return func(cmd *cobra.Command, args []string) {
beers, err := repository.GetBeers()
if errors.IsDataUnreacheable(err) {
if errors.IsFileErrorType(err) {
log.Fatal(err)
}
if errors.IsFormatDataError(err) {
log.Fatal(err)
}

Expand Down
28 changes: 0 additions & 28 deletions 07-behaviour_error_handling/internal/errors/errortypes.go

This file was deleted.

23 changes: 23 additions & 0 deletions 07-behaviour_error_handling/internal/errors/fileerrortype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package errors

import (
"github.com/pkg/errors"
)

type fileDataError struct {
error
}

func WrapFileDataUError(err error, format string, args ...interface{}) error {
return &fileDataError{errors.Wrapf(err, format, args...)}
}

func NewFileDataError(format string, args ...interface{}) error {
return &fileDataError{errors.Errorf(format, args...)}
}

func IsFileErrorType(err error) bool {
err = errors.Cause(err)
_, ok := err.(*fileDataError)
return ok
}
23 changes: 23 additions & 0 deletions 07-behaviour_error_handling/internal/errors/formaterrortype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package errors

import (
"github.com/pkg/errors"
)

type formatDataError struct {
error
}

func WrapFormatDataError(err error, format string, args ...interface{}) error {
return &formatDataError{errors.Wrapf(err, format, args...)}
}

func NewFormatDataError(format string, args ...interface{}) error {
return &formatDataError{errors.Errorf(format, args...)}
}

func IsFormatDataError(err error) bool {
err = errors.Cause(err)
_, ok := err.(*formatDataError)
return ok
}
23 changes: 23 additions & 0 deletions 07-behaviour_error_handling/internal/errors/httperrortype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package errors

import (
"github.com/pkg/errors"
)

type httpDataError struct {
error
}

func WrapHttpDataError(err error, format string, args ...interface{}) error {
return &httpDataError{errors.Wrapf(err, format, args...)}
}

func NewHttpDataError(format string, args ...interface{}) error {
return &httpDataError{errors.Errorf(format, args...)}
}

func IsHttpDataError(err error) bool {
err = errors.Cause(err)
_, ok := err.(*httpDataError)
return ok
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package errors

import (
"github.com/pkg/errors"
)

type jsonProcessingDataError struct {
error
}

func WrapJsonProcessingDataError(err error, format string, args ...interface{}) error {
return &jsonProcessingDataError{errors.Wrapf(err, format, args...)}
}

func NewJsonProcessingDataError(format string, args ...interface{}) error {
return &jsonProcessingDataError{errors.Errorf(format, args...)}
}

func IsJsonProcessingDataError(err error) bool {
err = errors.Cause(err)
_, ok := err.(*jsonProcessingDataError)
return ok
}
15 changes: 13 additions & 2 deletions 07-behaviour_error_handling/internal/storage/csv/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package csv

import (
"bufio"
"github.com/CodelyTV/golang-examples/07-behaviour_error_handling/internal/errors"
"os"
"strconv"
"strings"
Expand All @@ -19,15 +20,24 @@ func NewRepository() beerscli.BeerRepo {

// GetBeers fetch beers data from csv
func (r *repository) GetBeers() ([]beerscli.Beer, error) {
f, _ := os.Open("07-behaviour_error_handling/data/beers.csv")
f, err := os.Open("07-behaviour_error_handling/data/beers2.csv")

if err != nil {
return nil, errors.WrapFileDataUError(err, "error getting beers to %s", "csv data")
}

reader := bufio.NewReader(f)

var beers []beerscli.Beer

for line := readLine(reader); line != nil; line = readLine(reader) {
values := strings.Split(string(line), ",")

productID, _ := strconv.Atoi(values[0])
productID, err := strconv.Atoi(values[0])

if err != nil {
return nil, errors.WrapFormatDataError(err, "can't format product Id with Atoi method")
}

beer := beerscli.NewBeer(
productID,
Expand All @@ -47,5 +57,6 @@ func (r *repository) GetBeers() ([]beerscli.Beer, error) {

func readLine(reader *bufio.Reader) (line []byte) {
line, _, _ = reader.ReadLine()

return
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ func NewOntarioRepository() beerscli.BeerRepo {
func (b *beerRepo) GetBeers() (beers []beerscli.Beer, err error) {
response, err := http.Get(fmt.Sprintf("%v%v", b.url, productsEndpoint))
if err != nil {
return nil, errors.WrapDataUnreacheable(err, "error getting response to %s", productsEndpoint)
return nil, errors.WrapHttpDataError(err, "error getting response to %s", productsEndpoint)
}

contents, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, errors.WrapDataUnreacheable(err, "error reading the response from %s", productsEndpoint)
return nil, errors.WrapHttpDataError(err, "error reading the response from %s", productsEndpoint)
}

err = json.Unmarshal(contents, &beers)
if err != nil {
return nil, errors.WrapDataUnreacheable(err, "can't parsing response into beers")
return nil, errors.WrapJsonProcessingDataError(err, "can't parsing response into beers")
}
return
}
15 changes: 7 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3Ee
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
Expand Down Expand Up @@ -43,16 +43,17 @@ github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwKs=
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
Expand All @@ -68,8 +69,6 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -91,8 +90,6 @@ github.com/soheilhy/cmux v0.1.4/go.mod h1:IM3LyeVVIOuxMH7sFAkER9+bJ4dT7Ms6E4xg4k
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v0.0.3 h1:ZlrZ4XsMRm04Fr5pSFxBgfND2EBVa1nLpiy1stUsX/8=
github.com/spf13/cobra v0.0.3/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/cobra v0.0.7 h1:FfTH+vuMXOas8jmfb5/M7dzEYx7LpcLb7a0LPe34uOU=
github.com/spf13/cobra v0.0.7/go.mod h1:/6GTrnGXV9HjY+aR4k0oJ5tcvakLuG6EuKReYlHNrgE=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
Expand All @@ -102,8 +99,8 @@ github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/y
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
Expand Down Expand Up @@ -142,10 +139,12 @@ google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZi
google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=