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 server, s server mode
config, conf scan config files config, conf scan config files
plugin, p manage plugins plugin, p manage plugins
version print the version
help, h Shows a list of commands or help for one command help, h Shows a list of commands or help for one command
GLOBAL OPTIONS: GLOBAL OPTIONS:

View File

@@ -358,6 +358,7 @@ func NewApp(version string) *cli.App {
NewServerCommand(), NewServerCommand(),
NewConfigCommand(), NewConfigCommand(),
NewPluginCommand(), NewPluginCommand(),
NewVersionCommand(),
} }
app.Commands = append(app.Commands, plugin.LoadCommands()...) 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, // StringSliceFlag is defined globally. When the app runs multiple times,
// the previous value will be retained and it causes unexpected results. // the previous value will be retained and it causes unexpected results.
// The flag value is copied through this function to prevent the issue. // The flag value is copied through this function to prevent the issue.

View File

@@ -2,17 +2,10 @@ package commands
import ( import (
"bytes" "bytes"
"encoding/json"
"os"
"path/filepath"
"testing" "testing"
"time"
"github.com/spf13/afero"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/aquasecurity/trivy-db/pkg/metadata"
) )
func Test_showVersion(t *testing.T) { func Test_showVersion(t *testing.T) {
@@ -22,43 +15,23 @@ func Test_showVersion(t *testing.T) {
version string version string
} }
tests := []struct { tests := []struct {
name string name string
args args args args
createDB bool want string
expectedOutput string
}{ }{
{ {
name: "happy path, table output", name: "happy path, table output",
args: args{ args: args{
outputFormat: "table", outputFormat: "table",
version: "v1.2.3", version: "v1.2.3",
cacheDir: "testdata",
}, },
expectedOutput: `Version: v1.2.3 want: `Version: v1.2.3
Vulnerability DB: Vulnerability DB:
Version: 42 Version: 2
UpdatedAt: 2020-03-16 23:40:20 +0000 UTC UpdatedAt: 2022-03-02 06:07:07.99504083 +0000 UTC
NextUpdate: 2020-03-16 23:57:00 +0000 UTC NextUpdate: 2022-03-02 12:07:07.99504023 +0000 UTC
DownloadedAt: 2020-03-16 23:40:20 +0000 UTC DownloadedAt: 2022-03-02 10:03:38.383312 +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"}
`, `,
}, },
{ {
@@ -68,41 +41,82 @@ Vulnerability DB:
version: "1.2.3", version: "1.2.3",
cacheDir: "/foo/bar/bogus", cacheDir: "/foo/bar/bogus",
}, },
expectedOutput: `{"Version":"1.2.3"} want: `{"Version":"1.2.3"}
`, `,
}, },
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
var cacheDir string got := new(bytes.Buffer)
switch { showVersion(tt.args.cacheDir, tt.args.outputFormat, tt.args.version, got)
case tt.args.cacheDir != "": assert.Equal(t, tt.want, got.String(), tt.name)
cacheDir = tt.args.cacheDir })
default: }
cacheDir, _ = os.MkdirTemp("", "Test_showVersion-*") }
defer os.RemoveAll(cacheDir)
//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())
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),
})
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)
}) })
} }
} }

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"
}