diff --git a/README.md b/README.md index 530d192..bf0e30d 100644 --- a/README.md +++ b/README.md @@ -4,15 +4,21 @@ Carousell is both a library and CLI tool written in Golang that fetches Carousel # Installing -Using Carousell is easy. First, use `go install` to install the latest version of the library. This command will install the `carousell` executable along with the library and its dependencies: +Using Carousell is easy. First, use `go install` to install the latest version of the executable. This command will install the `carousell` executable in `$GOPATH/bin`: ``` -go install -u github.com/rodionlim/carousell +go install github.com/rodionlim/carousell@latest +``` + +To use the library, `go get` will install the libraries and dependencies for your project. + +``` +go get -u github.com/rodionlim/carousell ``` # Usage -There are two commands, `get` and `notify`. Flags can be used to modify the search behaviour, e.g. `-r` flag will query for only recent listings. +There are two commands, `get` and `notify`. Flags can be used to modify the search behaviour, e.g. `-r` flag will query for only recent listings, `-v` flag represents verbose mode and add logging to the output `get` will fetch the listings and output them to the console. diff --git a/carousell b/carousell index 0f24142..3ff1931 100755 Binary files a/carousell and b/carousell differ diff --git a/cmd/get.go b/cmd/get.go index 5f0cd93..40957e7 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -1,14 +1,16 @@ /* -Copyright © 2022 NAME HERE +Copyright © 2022 Rodion Lim */ package cmd import ( + "context" "fmt" "os" crs "github.com/rodionlim/carousell/library/carousell" + "github.com/rodionlim/carousell/library/log" "github.com/spf13/cobra" ) @@ -24,17 +26,21 @@ are search terms in Carousell, and at least one search term should be provided. Flags can be used to modify the search behaviour, such as specifying the -r recent flag, to include only recent listings etc.`, Run: func(cmd *cobra.Command, args []string) { + logger := log.Ctx(context.Background()) + setVerbosity(cmd) opts := getOpts(cmd, args) + req := crs.NewReq(opts...) listings, err := req.Get() if err != nil { fmt.Println("Something unexpected happened") + logger.Error(err.Error()) os.Exit(1) } // If user specifies that they want a summarized version of the output - shortFlag, err := cmd.Flags().GetBool("shorthand") - if err == nil && shortFlag { + shortFlag, _ := cmd.Flags().GetBool("shorthand") + if shortFlag { for _, listing := range listings { listing.Print() fmt.Println() diff --git a/cmd/notify.go b/cmd/notify.go index 200a450..4685b95 100644 --- a/cmd/notify.go +++ b/cmd/notify.go @@ -29,6 +29,7 @@ If using an app's oauth access token, ensure that the app has been invited to th Flags can be used to modify the search behaviour, e.g. specifying the -r recent flag, to include only recent listings etc.`, Run: func(cmd *cobra.Command, args []string) { + setVerbosity(cmd) opts := getOpts(cmd, args) req := crs.NewReq(opts...) interval, _ := cmd.Flags().GetInt("interval") @@ -51,6 +52,7 @@ Slack Channel: %s listings, err := req.Get() if err != nil { fmt.Println("Something unexpected happened") + logger.Error(err.Error()) os.Exit(1) } logger.Infof("***Recv initial listings*** \n%v", crs.ShortenListings(listings)) @@ -60,12 +62,13 @@ Slack Channel: %s d := time.Minute * time.Duration(interval) ticker := time.NewTicker(d) for { - fmt.Printf("Waiting for %d mins before next query\n", interval) + logger.Infof("Waiting for %d mins before next query\n", interval) <-ticker.C listings, err = req.Get() logger.Infof("***Recv listings*** \n%v", crs.ShortenListings(listings)) if err != nil { fmt.Println("Something unexpected happened") + logger.Error(err.Error()) os.Exit(1) } cache.ProcessAndStore(listings, func(listing crs.Listing) error { diff --git a/cmd/root.go b/cmd/root.go index 989dc9c..d3484cf 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,13 +1,15 @@ /* -Copyright © 2022 NAME HERE +Copyright © 2022 Rodion Lim */ package cmd import ( + "io" "os" crs "github.com/rodionlim/carousell/library/carousell" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -46,6 +48,14 @@ func init() { rootCmd.PersistentFlags().BoolP("recent", "r", false, "Search recent listings") rootCmd.PersistentFlags().IntP("price-floor", "f", 0, "Minimum price of listing") rootCmd.PersistentFlags().IntP("price-ceil", "c", 0, "Maximum price of listing") + rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Enable verbose mode with logging") +} + +func setVerbosity(cmd *cobra.Command) { + verbose, _ := rootCmd.PersistentFlags().GetBool("verbose") + if !verbose { + logrus.SetOutput(io.Discard) + } } func getOpts(cmd *cobra.Command, args []string) []func(r *crs.Req) { diff --git a/library/carousell/carousell.go b/library/carousell/carousell.go index ea05efd..a3d6ce9 100644 --- a/library/carousell/carousell.go +++ b/library/carousell/carousell.go @@ -7,6 +7,7 @@ package carousell import ( + "context" "errors" "fmt" "net/http" @@ -14,6 +15,7 @@ import ( "strconv" "strings" + "github.com/rodionlim/carousell/library/log" "golang.org/x/net/html" ) @@ -72,6 +74,8 @@ func WithRecent(r *Req) { // Get gets and parse carousell listing based on user parameters. func (r *Req) Get() ([]Listing, error) { + logger := log.Ctx(context.Background()) + if err := r.validate(); err != nil { return nil, err } @@ -86,11 +90,13 @@ func (r *Req) Get() ([]Listing, error) { } url.RawQuery = q.Encode() + logger.Infof("Send req [%s]", url.String()) resp, err := http.Get(url.String()) if err != nil { return nil, err } defer resp.Body.Close() + logger.Infof("Recv resp status_code[%v] headers[%v]", resp.StatusCode, resp.Header) node, err := html.Parse(resp.Body) if err != nil {