fix(cli): json format for trivy version (#1854)

Co-authored-by: knqyf263 <knqyf263@gmail.com>
This commit is contained in:
DmitriyLewen
2022-03-21 18:41:41 +06:00
committed by GitHub
parent b2b68951f2
commit c3aca1524c
4 changed files with 103 additions and 65 deletions

View File

@@ -18,6 +18,7 @@ COMMANDS:
server, s server mode
config, conf scan config files
plugin, p manage plugins
version print the version
help, h Shows a list of commands or help for one command
GLOBAL OPTIONS:

View File

@@ -358,6 +358,7 @@ func NewApp(version string) *cli.App {
NewServerCommand(),
NewConfigCommand(),
NewPluginCommand(),
NewVersionCommand(),
}
app.Commands = append(app.Commands, plugin.LoadCommands()...)
@@ -717,6 +718,21 @@ func NewPluginCommand() *cli.Command {
}
}
// NewVersionCommand adds version command
func NewVersionCommand() *cli.Command {
return &cli.Command{
Name: "version",
Usage: "print the version",
Action: func(ctx *cli.Context) error {
showVersion(ctx.String("cache-dir"), ctx.String("format"), ctx.App.Version, ctx.App.Writer)
return nil
},
Flags: []cli.Flag{
&formatFlag,
},
}
}
// StringSliceFlag is defined globally. When the app runs multiple times,
// the previous value will be retained and it causes unexpected results.
// The flag value is copied through this function to prevent the issue.

View File

@@ -2,17 +2,10 @@ package commands
import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/aquasecurity/trivy-db/pkg/metadata"
)
func Test_showVersion(t *testing.T) {
@@ -24,41 +17,21 @@ func Test_showVersion(t *testing.T) {
tests := []struct {
name string
args args
createDB bool
expectedOutput string
want string
}{
{
name: "happy path, table output",
args: args{
outputFormat: "table",
version: "v1.2.3",
cacheDir: "testdata",
},
expectedOutput: `Version: v1.2.3
want: `Version: v1.2.3
Vulnerability DB:
Version: 42
UpdatedAt: 2020-03-16 23:40:20 +0000 UTC
NextUpdate: 2020-03-16 23:57:00 +0000 UTC
DownloadedAt: 2020-03-16 23:40:20 +0000 UTC
`,
createDB: true,
},
{
name: "happy path, JSON output",
args: args{
outputFormat: "json",
version: "1.2.3",
},
expectedOutput: `{"Version":"1.2.3","VulnerabilityDB":{"Version":42,"NextUpdate":"2020-03-16T23:57:00Z","UpdatedAt":"2020-03-16T23:40:20Z","DownloadedAt":"2020-03-16T23:40:20Z"}}
`,
createDB: true,
},
{
name: "sad path, no DB is available",
args: args{
outputFormat: "json",
version: "1.2.3",
},
expectedOutput: `{"Version":"1.2.3"}
Version: 2
UpdatedAt: 2022-03-02 06:07:07.99504083 +0000 UTC
NextUpdate: 2022-03-02 12:07:07.99504023 +0000 UTC
DownloadedAt: 2022-03-02 10:03:38.383312 +0000 UTC
`,
},
{
@@ -68,41 +41,82 @@ Vulnerability DB:
version: "1.2.3",
cacheDir: "/foo/bar/bogus",
},
expectedOutput: `{"Version":"1.2.3"}
want: `{"Version":"1.2.3"}
`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var cacheDir string
switch {
case tt.args.cacheDir != "":
cacheDir = tt.args.cacheDir
default:
cacheDir, _ = os.MkdirTemp("", "Test_showVersion-*")
defer os.RemoveAll(cacheDir)
}
if tt.createDB {
fs := afero.NewOsFs()
err := os.MkdirAll(filepath.Join(cacheDir, "db"), os.ModePerm)
require.NoError(t, err)
metadataFile := filepath.Join(cacheDir, "db", "metadata.json")
b, err := json.Marshal(metadata.Metadata{
Version: 42,
NextUpdate: time.Unix(1584403020, 0),
UpdatedAt: time.Unix(1584402020, 0),
DownloadedAt: time.Unix(1584402020, 0),
got := new(bytes.Buffer)
showVersion(tt.args.cacheDir, tt.args.outputFormat, tt.args.version, got)
assert.Equal(t, tt.want, got.String(), tt.name)
})
require.NoError(t, err)
err = afero.WriteFile(fs, metadataFile, b, 0600)
require.NoError(t, err)
}
}
fw := new(bytes.Buffer)
showVersion(cacheDir, tt.args.outputFormat, tt.args.version, fw)
assert.Equal(t, tt.expectedOutput, fw.String(), tt.name)
//Check flag and command for print version
func TestPrintVersion(t *testing.T) {
tableOutput := `Version: test
Vulnerability DB:
Version: 2
UpdatedAt: 2022-03-02 06:07:07.99504083 +0000 UTC
NextUpdate: 2022-03-02 12:07:07.99504023 +0000 UTC
DownloadedAt: 2022-03-02 10:03:38.383312 +0000 UTC
`
jsonOutput := `{"Version":"test","VulnerabilityDB":{"Version":2,"NextUpdate":"2022-03-02T12:07:07.99504023Z","UpdatedAt":"2022-03-02T06:07:07.99504083Z","DownloadedAt":"2022-03-02T10:03:38.383312Z"}}
`
tests := []struct {
name string
arguments []string // 1st argument is path to trivy binaries
want string
wantErr string
}{
{
name: "happy path. '-v' flag is used",
arguments: []string{"trivy", "-v", "--cache-dir", "testdata"},
want: tableOutput,
},
{
name: "happy path. '-version' flag is used",
arguments: []string{"trivy", "-version", "--cache-dir", "testdata"},
want: tableOutput,
},
{
name: "happy path. 'version' command is used",
arguments: []string{"trivy", "--cache-dir", "testdata", "version"},
want: tableOutput,
},
{
name: "happy path. 'version', '--format json' flags are used",
arguments: []string{"trivy", "--cache-dir", "testdata", "version", "--format", "json"},
want: jsonOutput,
},
{
name: "sad path. '-v', '--format json' flags are used",
arguments: []string{"trivy", "-v", "--format", "json"},
wantErr: "flag provided but not defined: -format",
},
{
name: "sad path. '-version', '--format json' flags are used",
arguments: []string{"trivy", "-version", "--format", "json"},
wantErr: "flag provided but not defined: -format",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
got := new(bytes.Buffer)
app := NewApp("test")
app.Writer = got
err := app.Run(test.arguments)
if test.wantErr != "" {
require.Error(t, err)
assert.Contains(t, err.Error(), test.wantErr)
return
}
assert.Equal(t, test.want, got.String())
})
}
}

View File

@@ -0,0 +1,7 @@
{
"Version": 2,
"NextUpdate": "2022-03-02T12:07:07.99504023Z",
"UpdatedAt": "2022-03-02T06:07:07.99504083Z",
"DownloadedAt": "2022-03-02T10:03:38.383312Z"
}