diff --git a/commands/history.go b/commands/history.go index 67508bed..4d5644a8 100644 --- a/commands/history.go +++ b/commands/history.go @@ -33,9 +33,9 @@ import ( // HistoryCmd is Subcommand of list scanned results type HistoryCmd struct { - debug bool - debugSQL bool - jsonBaseDir string + debug bool + debugSQL bool + resultsDir string } // Name return subcommand name @@ -59,15 +59,15 @@ func (p *HistoryCmd) SetFlags(f *flag.FlagSet) { f.BoolVar(&p.debugSQL, "debug-sql", false, "SQL debug mode") wd, _ := os.Getwd() - defaultJSONBaseDir := filepath.Join(wd, "results") - f.StringVar(&p.jsonBaseDir, "results-dir", defaultJSONBaseDir, "/path/to/results") + defaultResultsDir := filepath.Join(wd, "results") + f.StringVar(&p.resultsDir, "results-dir", defaultResultsDir, "/path/to/results") } // Execute execute func (p *HistoryCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { c.Conf.DebugSQL = p.debugSQL - c.Conf.JSONBaseDir = p.jsonBaseDir + c.Conf.ResultsDir = p.resultsDir var err error var jsonDirs report.JSONDirs diff --git a/commands/scan.go b/commands/scan.go index 2daee210..bbd349cf 100644 --- a/commands/scan.go +++ b/commands/scan.go @@ -44,7 +44,7 @@ type ScanCmd struct { configPath string - jsonBaseDir string + resultsDir string cvedbpath string cveDictionaryURL string cacheDBPath string @@ -126,8 +126,8 @@ func (p *ScanCmd) SetFlags(f *flag.FlagSet) { defaultConfPath := filepath.Join(wd, "config.toml") f.StringVar(&p.configPath, "config", defaultConfPath, "/path/to/toml") - defaultJSONBaseDir := filepath.Join(wd, "results") - f.StringVar(&p.jsonBaseDir, "results-dir", defaultJSONBaseDir, "/path/to/results") + defaultResultsDir := filepath.Join(wd, "results") + f.StringVar(&p.resultsDir, "results-dir", defaultResultsDir, "/path/to/results") f.StringVar( &p.cvedbpath, @@ -347,7 +347,7 @@ func (p *ScanCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) reports = append(reports, report.AzureBlobWriter{}) } - c.Conf.JSONBaseDir = p.jsonBaseDir + c.Conf.ResultsDir = p.resultsDir c.Conf.CveDBPath = p.cvedbpath c.Conf.CveDictionaryURL = p.cveDictionaryURL c.Conf.CacheDBPath = p.cacheDBPath diff --git a/commands/tui.go b/commands/tui.go index 4112b598..4c7c1502 100644 --- a/commands/tui.go +++ b/commands/tui.go @@ -33,9 +33,9 @@ import ( // TuiCmd is Subcommand of host discovery mode type TuiCmd struct { - lang string - debugSQL bool - jsonBaseDir string + lang string + debugSQL bool + resultsDir string } // Name return subcommand name @@ -59,15 +59,15 @@ func (p *TuiCmd) SetFlags(f *flag.FlagSet) { wd, _ := os.Getwd() - defaultJSONBaseDir := filepath.Join(wd, "results") - f.StringVar(&p.jsonBaseDir, "results-dir", defaultJSONBaseDir, "/path/to/results") + defaultResultsDir := filepath.Join(wd, "results") + f.StringVar(&p.resultsDir, "results-dir", defaultResultsDir, "/path/to/results") } // Execute execute func (p *TuiCmd) Execute(_ context.Context, f *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { c.Conf.Lang = "en" c.Conf.DebugSQL = p.debugSQL - c.Conf.JSONBaseDir = p.jsonBaseDir + c.Conf.ResultsDir = p.resultsDir var jsonDirName string var err error diff --git a/config/config.go b/config/config.go index a9de9bb9..d6d4c034 100644 --- a/config/config.go +++ b/config/config.go @@ -47,7 +47,7 @@ type Config struct { SSHExternal bool HTTPProxy string `valid:"url"` - JSONBaseDir string + ResultsDir string CveDBPath string CacheDBPath string @@ -67,10 +67,10 @@ type Config struct { func (c Config) Validate() bool { errs := []error{} - if len(c.JSONBaseDir) != 0 { - if ok, _ := valid.IsFilePath(c.JSONBaseDir); !ok { + if len(c.ResultsDir) != 0 { + if ok, _ := valid.IsFilePath(c.ResultsDir); !ok { errs = append(errs, fmt.Errorf( - "JSON base directory must be a *Absolute* file path. -results-dir: %s", c.JSONBaseDir)) + "JSON base directory must be a *Absolute* file path. -results-dir: %s", c.ResultsDir)) } } diff --git a/report/json.go b/report/json.go index 757f1058..47811874 100644 --- a/report/json.go +++ b/report/json.go @@ -94,13 +94,13 @@ var JSONDirPattern = regexp.MustCompile(`^\d{8}_\d{4}$`) // GetValidJSONDirs return valid json directory as array func GetValidJSONDirs() (jsonDirs JSONDirs, err error) { var dirInfo []os.FileInfo - if dirInfo, err = ioutil.ReadDir(c.Conf.JSONBaseDir); err != nil { - err = fmt.Errorf("Failed to read %s: %s", c.Conf.JSONBaseDir, err) + if dirInfo, err = ioutil.ReadDir(c.Conf.ResultsDir); err != nil { + err = fmt.Errorf("Failed to read %s: %s", c.Conf.ResultsDir, err) return } for _, d := range dirInfo { if d.IsDir() && JSONDirPattern.MatchString(d.Name()) { - jsonDir := filepath.Join(c.Conf.JSONBaseDir, d.Name()) + jsonDir := filepath.Join(c.Conf.ResultsDir, d.Name()) jsonDirs = append(jsonDirs, jsonDir) } } diff --git a/report/tui.go b/report/tui.go index 6804d35f..4a41635b 100644 --- a/report/tui.go +++ b/report/tui.go @@ -73,14 +73,14 @@ func RunTui(jsonDirName string) subcommands.ExitStatus { func selectScanHistory(jsonDirName string) (latest models.ScanHistory, err error) { var jsonDir string if 0 < len(jsonDirName) { - jsonDir = filepath.Join(config.Conf.JSONBaseDir, jsonDirName) + jsonDir = filepath.Join(config.Conf.ResultsDir, jsonDirName) } else { var jsonDirs JSONDirs if jsonDirs, err = GetValidJSONDirs(); err != nil { return } if len(jsonDirs) == 0 { - return latest, fmt.Errorf("No scan results are found in %s", config.Conf.JSONBaseDir) + return latest, fmt.Errorf("No scan results are found in %s", config.Conf.ResultsDir) } jsonDir = jsonDirs[0] } diff --git a/report/util.go b/report/util.go index 8ac3044f..123d22f7 100644 --- a/report/util.go +++ b/report/util.go @@ -31,19 +31,21 @@ import ( ) func ensureResultDir(scannedAt time.Time) (path string, err error) { - if resultDirPath != "" { - return resultDirPath, nil - } - const timeLayout = "20060102_1504" - timedir := scannedAt.Format(timeLayout) - wd, _ := os.Getwd() - dir := filepath.Join(wd, "results", timedir) - if err := os.MkdirAll(dir, 0700); err != nil { + jsonDirName := scannedAt.Format(timeLayout) + + resultsDir := config.Conf.ResultsDir + if len(resultsDir) == 0 { + wd, _ := os.Getwd() + resultsDir = filepath.Join(wd, "results") + } + jsonDir := filepath.Join(resultsDir, jsonDirName) + + if err := os.MkdirAll(jsonDir, 0700); err != nil { return "", fmt.Errorf("Failed to create dir: %s", err) } - symlinkPath := filepath.Join(wd, "results", "current") + symlinkPath := filepath.Join(resultsDir, "current") if _, err := os.Lstat(symlinkPath); err == nil { if err := os.Remove(symlinkPath); err != nil { return "", fmt.Errorf( @@ -51,11 +53,11 @@ func ensureResultDir(scannedAt time.Time) (path string, err error) { } } - if err := os.Symlink(dir, symlinkPath); err != nil { + if err := os.Symlink(jsonDir, symlinkPath); err != nil { return "", fmt.Errorf( "Failed to create symlink: path: %s, err: %s", symlinkPath, err) } - return dir, nil + return jsonDir, nil } func toPlainText(scanResult models.ScanResult) (string, error) { diff --git a/report/writer.go b/report/writer.go index 272b1956..13fd22f2 100644 --- a/report/writer.go +++ b/report/writer.go @@ -39,5 +39,3 @@ const ( type ResultWriter interface { Write([]models.ScanResult) error } - -var resultDirPath string