From 655826a03479ce9ea6b5ff01568c622a85f5ec4c Mon Sep 17 00:00:00 2001 From: Quentin Lemaire Date: Sat, 18 Apr 2020 14:58:31 +0200 Subject: [PATCH] feat(cli): Set up verbose mode --- .github/workflows/cd.yml | 2 +- cmd/radarr/cmd_movie.go | 20 +++++++++++++++++++ cmd/radarr/cmd_status.go | 2 ++ cmd/radarr/cmd_version.go | 31 ++--------------------------- cmd/radarr/cmd_version_test.go | 36 +--------------------------------- cmd/radarr/main.go | 22 ++++++++++++++++----- go.mod | 1 + go.sum | 10 ++++++++++ 8 files changed, 54 insertions(+), 70 deletions(-) diff --git a/.github/workflows/cd.yml b/.github/workflows/cd.yml index aaebe9b..587cdec 100644 --- a/.github/workflows/cd.yml +++ b/.github/workflows/cd.yml @@ -34,7 +34,7 @@ jobs: REPO_ROOT=$(git rev-parse --show-toplevel) export PATH=$PATH:$(go env GOPATH)/bin export VERSION=$(git describe --tags --exact-match 2>/dev/null) - gox -os="darwin linux windows" -arch="amd64" -output="$REPO_ROOT/bin/{{.Dir}}_{{.OS}}_{{.Arch}}" -ldflags "-X main.Version=${VERSION}" ./... + gox -os="darwin linux windows" -arch="amd64" -output="$REPO_ROOT/bin/{{.Dir}}_{{.OS}}_{{.Arch}}" -ldflags "-X main.version=${VERSION}" ./... - name: Upload them as artifacts uses: actions/upload-artifact@v1 diff --git a/cmd/radarr/cmd_movie.go b/cmd/radarr/cmd_movie.go index adff8f5..508434a 100644 --- a/cmd/radarr/cmd_movie.go +++ b/cmd/radarr/cmd_movie.go @@ -73,6 +73,7 @@ func init() { } func listMovies(c *cli.Context) error { + log.Debugln("Listing movies") movies, err := radarrClient.Movies.List() if err != nil { return err @@ -80,6 +81,7 @@ func listMovies(c *cli.Context) error { // Print as JSON if provided if c.Bool("json") { + log.Debugln("Print output as JSON") data, err := json.Marshal(movies) if err != nil { return err @@ -89,9 +91,12 @@ func listMovies(c *cli.Context) error { return nil } + log.Debugln("Creating table") t.SetHeader([]string{"Id", "Title", "Downloaded", "Monitored", "Added"}) var data [][]string + log.Debugf("Found %d movies", len(movies)) + // Filter movies for _, movie := range movies { data = append(data, []string{ @@ -119,6 +124,8 @@ func getMovie(c *cli.Context) error { return err } + log.Debugf("Searching movie with ID %d", m) + movie, err := radarrClient.Movies.Get(m) if err != nil { return err @@ -126,6 +133,7 @@ func getMovie(c *cli.Context) error { // Print as JSON if provided if c.Bool("json") { + log.Debugln("Print output as JSON") data, err := json.Marshal(movie) if err != nil { return err @@ -135,6 +143,7 @@ func getMovie(c *cli.Context) error { return nil } + log.Debugln("Creating table") v := reflect.ValueOf(*movie) typeOfMovie := v.Type() @@ -177,6 +186,8 @@ func upcoming(c *cli.Context) error { opts.End = end.Value() } + log.Debugf("Searching upcoming movies with start=%v end=%v", opts.Start, opts.End) + movies, err := radarrClient.Movies.Upcoming(opts) if err != nil { return err @@ -184,6 +195,7 @@ func upcoming(c *cli.Context) error { // Print as JSON if provided if c.Bool("json") { + log.Debugln("Print output as JSON") data, err := json.Marshal(movies) if err != nil { return err @@ -196,6 +208,8 @@ func upcoming(c *cli.Context) error { t.SetHeader([]string{"Id", "Title", "Downloaded", "Monitored", "Added"}) var data [][]string + log.Debugf("Found %d movies", len(movies)) + // Filter movies for _, movie := range movies { data = append(data, []string{ @@ -218,6 +232,8 @@ func deleteMovie(c *cli.Context) error { return errors.New("Please specify a movie ID") } + log.Debugf("Deleting movie with ID %s", movieID) + m, err := strconv.Atoi(movieID) if err != nil { return err @@ -241,6 +257,7 @@ func deleteMovie(c *cli.Context) error { } func excluded(c *cli.Context) error { + log.Debugln("Searching excluded movies") movies, err := radarrClient.Movies.Excluded() if err != nil { return err @@ -248,6 +265,7 @@ func excluded(c *cli.Context) error { // Print as JSON if provided if c.Bool("json") { + log.Debugln("Print output as JSON") data, err := json.Marshal(movies) if err != nil { return err @@ -265,6 +283,8 @@ func excluded(c *cli.Context) error { headers = append(headers, typeOfMovie.Field(i).Name) } + log.Debugf("Found %d movies", len(movies)) + var data [][]string for _, movie := range movies { v := reflect.ValueOf(*movie) diff --git a/cmd/radarr/cmd_status.go b/cmd/radarr/cmd_status.go index af0eb11..47fe93c 100644 --- a/cmd/radarr/cmd_status.go +++ b/cmd/radarr/cmd_status.go @@ -19,6 +19,7 @@ func init() { } func getStatus(c *cli.Context) error { + log.Debugln("Get Radarr status") status, err := radarrClient.SystemStatus.Get() if err != nil { return err @@ -26,6 +27,7 @@ func getStatus(c *cli.Context) error { // Print as JSON if provided if c.Bool("json") { + log.Debugln("Print output as JSON") data, err := json.Marshal(status) if err != nil { return err diff --git a/cmd/radarr/cmd_version.go b/cmd/radarr/cmd_version.go index 00de20e..fa54f14 100644 --- a/cmd/radarr/cmd_version.go +++ b/cmd/radarr/cmd_version.go @@ -1,7 +1,6 @@ package main import ( - "encoding/json" "fmt" "runtime" @@ -15,38 +14,12 @@ var versionCommand *cli.Command = &cli.Command{ Action: showVersion, } -type version struct { - GOOS string `json:"GOOS"` - GOARCH string `json:"GOARCH"` - Version string `json:"version"` - Runtime string `json:"runtime"` -} - -func (v *version) String() string { - return fmt.Sprintf("radarr %s compiled with %v on %v/%v", v.Version, v.Runtime, v.GOOS, v.GOARCH) -} - -var v *version = &version{ - GOARCH: runtime.GOARCH, - GOOS: runtime.GOOS, - Runtime: runtime.Version(), - Version: Version, -} - func init() { app.Commands = append(app.Commands, versionCommand) } func showVersion(c *cli.Context) error { - if c.Bool("json") { - data, err := json.Marshal(v) - if err != nil { - return err - } - fmt.Println(string(data)) - return nil - } - - fmt.Println(v) + log.Debugln("Print version") + fmt.Printf("radarr %s compiled with %v on %v/%v\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH) return nil } diff --git a/cmd/radarr/cmd_version_test.go b/cmd/radarr/cmd_version_test.go index 30cec68..0f4f3bc 100644 --- a/cmd/radarr/cmd_version_test.go +++ b/cmd/radarr/cmd_version_test.go @@ -10,41 +10,7 @@ import ( "github.com/urfave/cli/v2" ) -func Test_version_String(t *testing.T) { - type fields struct { - GOOS string - GOARCH string - Version string - Runtime string - } - tests := []struct { - name string - fields fields - want string - }{ - { - name: "Format is well", - fields: fields{GOARCH: "foo", GOOS: "bar", Runtime: "1", Version: "0"}, - want: "radarr 0 compiled with 1 on bar/foo", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - v := &version{ - GOOS: tt.fields.GOOS, - GOARCH: tt.fields.GOARCH, - Version: tt.fields.Version, - Runtime: tt.fields.Runtime, - } - if got := v.String(); got != tt.want { - t.Errorf("version.String() = %v, want %v", got, tt.want) - } - }) - } -} - func Test_showVersion(t *testing.T) { - // TODO: Need help to test the --json flag var c *cli.Context = cli.NewContext(cli.NewApp(), flag.CommandLine, nil) tests := []struct { name string @@ -56,7 +22,7 @@ func Test_showVersion(t *testing.T) { name: "Plain", wantErr: false, c: c, - out: fmt.Sprintf("radarr %s compiled with %v on %v/%v\n", Version, runtime.Version(), runtime.GOOS, runtime.GOARCH), + out: fmt.Sprintf("radarr %s compiled with %v on %v/%v\n", version, runtime.Version(), runtime.GOOS, runtime.GOARCH), }, } for _, tt := range tests { diff --git a/cmd/radarr/main.go b/cmd/radarr/main.go index 7b0dae7..349bc6d 100644 --- a/cmd/radarr/main.go +++ b/cmd/radarr/main.go @@ -1,13 +1,13 @@ package main import ( - "log" "os" "sort" "time" "github.com/SkYNewZ/radarr" "github.com/olekukonko/tablewriter" + "github.com/sirupsen/logrus" "github.com/urfave/cli/v2" ) @@ -15,13 +15,14 @@ var radarrClient *radarr.Service var t *tablewriter.Table = tablewriter.NewWriter(os.Stdout) -// Version program version -var Version string = "development" +var version string = "development" +var verbose bool = false +var log *logrus.Logger = logrus.New() var app *cli.App = &cli.App{ Name: "Radarr CLI", Usage: "Perform actions on your Radarr instance", - Version: Version, + Version: version, Compiled: time.Now(), EnableBashCompletion: true, Flags: []cli.Flag{ @@ -41,22 +42,33 @@ var app *cli.App = &cli.App{ Name: "json", Usage: "Print output as JSON instead of table", }, + &cli.BoolFlag{ + Name: "verbose", + Aliases: []string{"v"}, + Usage: "Print debug infos", + Destination: &verbose, + }, }, HideVersion: true, Authors: []*cli.Author{{Email: "quentin@lemairepro.fr", Name: "SkYNewZ"}}, Before: func(c *cli.Context) error { + // Instantiate Radarr client s, err := radarr.New(c.String("url"), c.String("apiKey"), nil) if err != nil { return err } radarrClient = s + + // Set debug level + if verbose { + log.SetLevel(logrus.DebugLevel) + } return nil }, } func init() { - log.SetFlags(0) t.SetAlignment(tablewriter.ALIGN_LEFT) t.SetAutoWrapText(false) sort.Sort(cli.FlagsByName(app.Flags)) diff --git a/go.mod b/go.mod index 5697103..16a5de9 100644 --- a/go.mod +++ b/go.mod @@ -5,5 +5,6 @@ go 1.14 require ( github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d github.com/olekukonko/tablewriter v0.0.4 + github.com/sirupsen/logrus v1.5.0 github.com/urfave/cli/v2 v2.2.0 ) diff --git a/go.sum b/go.sum index 5077ddf..ca48b7d 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,12 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY= github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= +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/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d h1:cVtBfNW5XTHiKQe7jDaDBSh/EVM4XLPutLAGboIXuM0= github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d/go.mod h1:P2viExyCEfeWGU259JnaQ34Inuec4R38JCyBx2edgD0= +github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54= github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8= @@ -13,7 +17,13 @@ github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0 github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +github.com/sirupsen/logrus v1.5.0 h1:1N5EYkVAPEywqZRJd7cwnRtCb6xJx7NH3T3WUTF980Q= +github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo= +github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/urfave/cli/v2 v2.2.0 h1:JTTnM6wKzdA0Jqodd966MVj4vWbbquZykeX1sKbe2C4= github.com/urfave/cli/v2 v2.2.0/go.mod h1:SE9GqnLQmjVa0iPEY0f1w3ygNIYcIJ0OKPMoW2caLfQ= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=