diff --git a/internal/app.go b/internal/app.go index 85172e8a61..3ef05ffb6b 100644 --- a/internal/app.go +++ b/internal/app.go @@ -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) } } diff --git a/internal/app_test.go b/internal/app_test.go index a8fc12f19d..671d02421a 100644 --- a/internal/app_test.go +++ b/internal/app_test.go @@ -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) }) }