-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): Move version to its own command
- Loading branch information
Showing
4 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters