Skip to content

Commit

Permalink
feat(cli): Set up verbose mode
Browse files Browse the repository at this point in the history
  • Loading branch information
SkYNewZ committed Apr 26, 2020
1 parent e001434 commit 655826a
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions cmd/radarr/cmd_movie.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ func init() {
}

func listMovies(c *cli.Context) error {
log.Debugln("Listing movies")
movies, err := radarrClient.Movies.List()
if err != nil {
return err
}

// 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
Expand All @@ -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{
Expand Down Expand Up @@ -119,13 +124,16 @@ 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
}

// 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
Expand All @@ -135,6 +143,7 @@ func getMovie(c *cli.Context) error {
return nil
}

log.Debugln("Creating table")
v := reflect.ValueOf(*movie)
typeOfMovie := v.Type()

Expand Down Expand Up @@ -177,13 +186,16 @@ 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
}

// 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
Expand All @@ -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{
Expand All @@ -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
Expand All @@ -241,13 +257,15 @@ 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
}

// 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
Expand All @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions cmd/radarr/cmd_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ func init() {
}

func getStatus(c *cli.Context) error {
log.Debugln("Get Radarr status")
status, err := radarrClient.SystemStatus.Get()
if err != nil {
return err
}

// 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
Expand Down
31 changes: 2 additions & 29 deletions cmd/radarr/cmd_version.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"encoding/json"
"fmt"
"runtime"

Expand All @@ -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
}
36 changes: 1 addition & 35 deletions cmd/radarr/cmd_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down
22 changes: 17 additions & 5 deletions cmd/radarr/main.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
package main

import (
"log"
"os"
"sort"
"time"

"github.com/SkYNewZ/radarr"
"github.com/olekukonko/tablewriter"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)

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{
Expand All @@ -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: "[email protected]", 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))
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -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=
Expand All @@ -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=

0 comments on commit 655826a

Please sign in to comment.