mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-12 15:50:15 -08:00
app: Fix a few edge cases with version flag (#443)
* app: Show just version if DB is missing Signed-off-by: Simarpreet Singh <simar@linux.com> * app: Dont panic if cache-dir is bogus Signed-off-by: Simarpreet Singh <simar@linux.com> * app: DRY up logic for showVersion Signed-off-by: Simarpreet Singh <simar@linux.com>
This commit is contained in:
@@ -20,8 +20,8 @@ import (
|
||||
)
|
||||
|
||||
type VersionInfo struct {
|
||||
Version string `json:",omitempty"`
|
||||
VulnerabilityDB db.Metadata `json:",omitempty"`
|
||||
Version string `json:",omitempty"`
|
||||
VulnerabilityDB *db.Metadata `json:",omitempty"`
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -247,40 +247,46 @@ OPTIONS:
|
||||
}
|
||||
|
||||
func showVersion(cacheDir, outputFormat, version string, outputWriter io.Writer) {
|
||||
db.Init(cacheDir)
|
||||
metadata, err := db.Config{}.GetMetadata()
|
||||
if err != nil {
|
||||
fmt.Fprintf(outputWriter, "unable to display current version: %s", err.Error())
|
||||
return
|
||||
}
|
||||
switch outputFormat {
|
||||
case "json":
|
||||
b, _ := json.Marshal(VersionInfo{
|
||||
Version: version,
|
||||
VulnerabilityDB: db.Metadata{
|
||||
var dbMeta *db.Metadata
|
||||
|
||||
err := db.Init(cacheDir)
|
||||
if err == nil {
|
||||
metadata, _ := db.Config{}.GetMetadata()
|
||||
if !metadata.UpdatedAt.IsZero() && !metadata.NextUpdate.IsZero() && metadata.Version != 0 {
|
||||
dbMeta = &db.Metadata{
|
||||
Version: metadata.Version,
|
||||
Type: metadata.Type,
|
||||
NextUpdate: metadata.NextUpdate.UTC(),
|
||||
UpdatedAt: metadata.UpdatedAt.UTC(),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
switch outputFormat {
|
||||
case "json":
|
||||
b, _ := json.Marshal(VersionInfo{
|
||||
Version: version,
|
||||
VulnerabilityDB: dbMeta,
|
||||
})
|
||||
fmt.Fprintln(outputWriter, string(b))
|
||||
default:
|
||||
var dbType string
|
||||
switch metadata.Type {
|
||||
case 0:
|
||||
dbType = "Full"
|
||||
case 1:
|
||||
dbType = "Light"
|
||||
}
|
||||
|
||||
fmt.Fprintf(outputWriter, `Version: %s
|
||||
Vulnerability DB:
|
||||
output := fmt.Sprintf("Version: %s\n", version)
|
||||
if dbMeta != nil {
|
||||
var dbType string
|
||||
switch dbMeta.Type {
|
||||
case 0:
|
||||
dbType = "Full"
|
||||
case 1:
|
||||
dbType = "Light"
|
||||
}
|
||||
output += fmt.Sprintf(`Vulnerability DB:
|
||||
Type: %s
|
||||
Version: %d
|
||||
UpdatedAt: %s
|
||||
NextUpdate: %s
|
||||
`, version, dbType, metadata.Version, metadata.UpdatedAt.UTC().String(), metadata.NextUpdate.UTC().String())
|
||||
`, dbType, dbMeta.Version, dbMeta.UpdatedAt.UTC(), dbMeta.NextUpdate.UTC())
|
||||
}
|
||||
fmt.Fprintf(outputWriter, output)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -60,21 +60,38 @@ Vulnerability DB:
|
||||
{
|
||||
name: "sad path, no DB is available",
|
||||
args: args{
|
||||
outputFormat: "table",
|
||||
outputFormat: "json",
|
||||
version: "1.2.3",
|
||||
},
|
||||
expectedOutput: `unable to display current version: unexpected end of JSON input`,
|
||||
expectedOutput: `{"Version":"1.2.3"}
|
||||
`,
|
||||
},
|
||||
{
|
||||
name: "sad path, bogus cache dir",
|
||||
args: args{
|
||||
outputFormat: "json",
|
||||
version: "1.2.3",
|
||||
cacheDir: "/foo/bar/bogus",
|
||||
},
|
||||
expectedOutput: `{"Version":"1.2.3"}
|
||||
`,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
d, _ := ioutil.TempDir("", "Test_showVersion-*")
|
||||
defer func() {
|
||||
os.RemoveAll(d)
|
||||
}()
|
||||
var cacheDir string
|
||||
switch {
|
||||
case tt.args.cacheDir != "":
|
||||
cacheDir = tt.args.cacheDir
|
||||
default:
|
||||
cacheDir, _ = ioutil.TempDir("", "Test_showVersion-*")
|
||||
defer func() {
|
||||
os.RemoveAll(cacheDir)
|
||||
}()
|
||||
}
|
||||
|
||||
if tt.createDB {
|
||||
db.Init(d)
|
||||
db.Init(cacheDir)
|
||||
db.Config{}.SetMetadata(db.Metadata{
|
||||
Version: 42,
|
||||
Type: 1,
|
||||
@@ -87,7 +104,7 @@ Vulnerability DB:
|
||||
var wb []byte
|
||||
fw := fakeIOWriter{written: wb}
|
||||
|
||||
showVersion(d, tt.args.outputFormat, tt.args.version, &fw)
|
||||
showVersion(cacheDir, tt.args.outputFormat, tt.args.version, &fw)
|
||||
assert.Equal(t, tt.expectedOutput, string(fw.written), tt.name)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user