Skip to content

Commit

Permalink
feat(cli): Move version to its own command
Browse files Browse the repository at this point in the history
  • Loading branch information
SkYNewZ committed Apr 13, 2020
1 parent decbb71 commit 287f53d
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 0 deletions.
42 changes: 42 additions & 0 deletions cmd/radarr/cmd_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"fmt"
"runtime"

"github.com/urfave/cli/v2"
)

var versionCommand *cli.Command = &cli.Command{
Name: "version",
Usage: "Print version",
HideHelp: true,
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(*cli.Context) error {
fmt.Printf("radarr %s compiled with %v on %v/%v\n", Version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
return nil
}
75 changes: 75 additions & 0 deletions cmd/radarr/cmd_version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package main

import (
"flag"
"fmt"
"runtime"
"testing"

"github.com/kami-zh/go-capturer"
"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
c *cli.Context
wantErr bool
out string
}{
{
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),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
out := capturer.CaptureStdout(func() {
if err := showVersion(tt.c); (err != nil) != tt.wantErr {
t.Errorf("showVersion() error = %v, wantErr %v", err, tt.wantErr)
}
})

if out != tt.out {
t.Errorf("showVersion() error = %v, want %v", out, tt.out)
}
})
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/SkYNewZ/radarr
go 1.14

require (
github.com/kami-zh/go-capturer v0.0.0-20171211120116-e492ea43421d
github.com/olekukonko/tablewriter v0.0.4
github.com/urfave/cli/v2 v2.2.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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/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/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 Down

0 comments on commit 287f53d

Please sign in to comment.