Skip to content

Commit

Permalink
feat(log): add verbose flag to all commands
Browse files Browse the repository at this point in the history
  • Loading branch information
rodionlim committed Feb 20, 2022
1 parent 5f3f491 commit ccc1f35
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
Binary file modified carousell
Binary file not shown.
12 changes: 9 additions & 3 deletions cmd/get.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
Copyright © 2022 Rodion Lim <rodion.lim@hotmail.com>
*/
package cmd

import (
"context"
"fmt"
"os"

crs "github.com/rodionlim/carousell/library/carousell"
"github.com/rodionlim/carousell/library/log"
"github.com/spf13/cobra"
)

Expand All @@ -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()
Expand Down
5 changes: 4 additions & 1 deletion cmd/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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))
Expand All @@ -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 {
Expand Down
12 changes: 11 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/*
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
Copyright © 2022 Rodion Lim <rodion.lim@hotmail.com>
*/
package cmd

import (
"io"
"os"

crs "github.com/rodionlim/carousell/library/carousell"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions library/carousell/carousell.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
package carousell

import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"

"github.com/rodionlim/carousell/library/log"
"golang.org/x/net/html"
)

Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down

0 comments on commit ccc1f35

Please sign in to comment.