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 {
|
type VersionInfo struct {
|
||||||
Version string `json:",omitempty"`
|
Version string `json:",omitempty"`
|
||||||
VulnerabilityDB db.Metadata `json:",omitempty"`
|
VulnerabilityDB *db.Metadata `json:",omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -247,40 +247,46 @@ OPTIONS:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func showVersion(cacheDir, outputFormat, version string, outputWriter io.Writer) {
|
func showVersion(cacheDir, outputFormat, version string, outputWriter io.Writer) {
|
||||||
db.Init(cacheDir)
|
var dbMeta *db.Metadata
|
||||||
metadata, err := db.Config{}.GetMetadata()
|
|
||||||
if err != nil {
|
err := db.Init(cacheDir)
|
||||||
fmt.Fprintf(outputWriter, "unable to display current version: %s", err.Error())
|
if err == nil {
|
||||||
return
|
metadata, _ := db.Config{}.GetMetadata()
|
||||||
}
|
if !metadata.UpdatedAt.IsZero() && !metadata.NextUpdate.IsZero() && metadata.Version != 0 {
|
||||||
switch outputFormat {
|
dbMeta = &db.Metadata{
|
||||||
case "json":
|
|
||||||
b, _ := json.Marshal(VersionInfo{
|
|
||||||
Version: version,
|
|
||||||
VulnerabilityDB: db.Metadata{
|
|
||||||
Version: metadata.Version,
|
Version: metadata.Version,
|
||||||
Type: metadata.Type,
|
Type: metadata.Type,
|
||||||
NextUpdate: metadata.NextUpdate.UTC(),
|
NextUpdate: metadata.NextUpdate.UTC(),
|
||||||
UpdatedAt: metadata.UpdatedAt.UTC(),
|
UpdatedAt: metadata.UpdatedAt.UTC(),
|
||||||
},
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
switch outputFormat {
|
||||||
|
case "json":
|
||||||
|
b, _ := json.Marshal(VersionInfo{
|
||||||
|
Version: version,
|
||||||
|
VulnerabilityDB: dbMeta,
|
||||||
})
|
})
|
||||||
fmt.Fprintln(outputWriter, string(b))
|
fmt.Fprintln(outputWriter, string(b))
|
||||||
default:
|
default:
|
||||||
var dbType string
|
output := fmt.Sprintf("Version: %s\n", version)
|
||||||
switch metadata.Type {
|
if dbMeta != nil {
|
||||||
case 0:
|
var dbType string
|
||||||
dbType = "Full"
|
switch dbMeta.Type {
|
||||||
case 1:
|
case 0:
|
||||||
dbType = "Light"
|
dbType = "Full"
|
||||||
}
|
case 1:
|
||||||
|
dbType = "Light"
|
||||||
fmt.Fprintf(outputWriter, `Version: %s
|
}
|
||||||
Vulnerability DB:
|
output += fmt.Sprintf(`Vulnerability DB:
|
||||||
Type: %s
|
Type: %s
|
||||||
Version: %d
|
Version: %d
|
||||||
UpdatedAt: %s
|
UpdatedAt: %s
|
||||||
NextUpdate: %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",
|
name: "sad path, no DB is available",
|
||||||
args: args{
|
args: args{
|
||||||
outputFormat: "table",
|
outputFormat: "json",
|
||||||
version: "1.2.3",
|
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 {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
d, _ := ioutil.TempDir("", "Test_showVersion-*")
|
var cacheDir string
|
||||||
defer func() {
|
switch {
|
||||||
os.RemoveAll(d)
|
case tt.args.cacheDir != "":
|
||||||
}()
|
cacheDir = tt.args.cacheDir
|
||||||
|
default:
|
||||||
|
cacheDir, _ = ioutil.TempDir("", "Test_showVersion-*")
|
||||||
|
defer func() {
|
||||||
|
os.RemoveAll(cacheDir)
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
if tt.createDB {
|
if tt.createDB {
|
||||||
db.Init(d)
|
db.Init(cacheDir)
|
||||||
db.Config{}.SetMetadata(db.Metadata{
|
db.Config{}.SetMetadata(db.Metadata{
|
||||||
Version: 42,
|
Version: 42,
|
||||||
Type: 1,
|
Type: 1,
|
||||||
@@ -87,7 +104,7 @@ Vulnerability DB:
|
|||||||
var wb []byte
|
var wb []byte
|
||||||
fw := fakeIOWriter{written: wb}
|
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)
|
assert.Equal(t, tt.expectedOutput, string(fw.written), tt.name)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user