Skip to content

Commit

Permalink
feat(cli): Add JSON output option
Browse files Browse the repository at this point in the history
  • Loading branch information
SkYNewZ committed Apr 13, 2020
1 parent 287f53d commit ef00621
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 18 deletions.
47 changes: 46 additions & 1 deletion cmd/radarr/cmd_movie.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -71,12 +72,23 @@ func init() {
}...)
}

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

// Print as JSON if provided
if c.Bool("json") {
data, err := json.Marshal(movies)
if err != nil {
return err
}

fmt.Println(string(data))
return nil
}

t.SetHeader([]string{"Id", "Title", "Downloaded", "Monitored", "Added"})
var data [][]string

Expand Down Expand Up @@ -112,6 +124,17 @@ func getMovie(c *cli.Context) error {
return err
}

// Print as JSON if provided
if c.Bool("json") {
data, err := json.Marshal(movie)
if err != nil {
return err
}

fmt.Println(string(data))
return nil
}

v := reflect.ValueOf(*movie)
typeOfMovie := v.Type()

Expand Down Expand Up @@ -159,6 +182,17 @@ func upcoming(c *cli.Context) error {
return err
}

// Print as JSON if provided
if c.Bool("json") {
data, err := json.Marshal(movies)
if err != nil {
return err
}

fmt.Println(string(data))
return nil
}

t.SetHeader([]string{"Id", "Title", "Downloaded", "Monitored", "Added"})
var data [][]string

Expand Down Expand Up @@ -212,6 +246,17 @@ func excluded(c *cli.Context) error {
return err
}

// Print as JSON if provided
if c.Bool("json") {
data, err := json.Marshal(movies)
if err != nil {
return err
}

fmt.Println(string(data))
return nil
}

// Set header
var headers []string
v := reflect.ValueOf(*movies[0])
Expand Down
14 changes: 13 additions & 1 deletion cmd/radarr/cmd_status.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"encoding/json"
"fmt"
"reflect"

Expand All @@ -17,12 +18,23 @@ func init() {
app.Commands = append(app.Commands, statusCommand)
}

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

// Print as JSON if provided
if c.Bool("json") {
data, err := json.Marshal(status)
if err != nil {
return err
}

fmt.Println(string(data))
return nil
}

v := reflect.ValueOf(*status)
typeOfStatus := v.Type()

Expand Down
14 changes: 12 additions & 2 deletions cmd/radarr/cmd_version.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

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

Expand Down Expand Up @@ -36,7 +37,16 @@ func init() {
app.Commands = append(app.Commands, versionCommand)
}

func showVersion(*cli.Context) error {
fmt.Printf("radarr %s compiled with %v on %v/%v\n", Version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
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)
return nil
}
2 changes: 2 additions & 0 deletions cmd/radarr/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This package contains the code of the radarr executable
package main
29 changes: 15 additions & 14 deletions cmd/radarr/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (
"github.com/urfave/cli/v2"
)

var radarURL string
var radarrAPIKey string
var radarrClient *radarr.Service

var t *tablewriter.Table = tablewriter.NewWriter(os.Stdout)
Expand All @@ -28,23 +26,26 @@ var app *cli.App = &cli.App{
EnableBashCompletion: true,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "url",
EnvVars: []string{"RADARR_URL"},
Required: true,
Usage: "Radarr instance URL",
Destination: &radarURL,
Name: "url",
EnvVars: []string{"RADARR_URL"},
Required: true,
Usage: "Radarr instance URL",
},
&cli.StringFlag{
Name: "apiKey",
EnvVars: []string{"RADARR_API_KEY"},
Required: true,
Usage: "Radarr API key",
Destination: &radarrAPIKey,
Name: "apiKey",
EnvVars: []string{"RADARR_API_KEY"},
Required: true,
Usage: "Radarr API key",
},
&cli.BoolFlag{
Name: "json",
Usage: "Print output as JSON instead of table",
},
},
Authors: []*cli.Author{{Email: "quentin@lemairepro.fr", Name: "SkYNewZ"}},
HideVersion: true,
Authors: []*cli.Author{{Email: "quentin@lemairepro.fr", Name: "SkYNewZ"}},
Before: func(c *cli.Context) error {
s, err := radarr.New(radarURL, radarrAPIKey, nil)
s, err := radarr.New(c.String("url"), c.String("apiKey"), nil)
if err != nil {
return err
}
Expand Down

0 comments on commit ef00621

Please sign in to comment.