Fix -results-dir option of scan subcommand

This commit is contained in:
Kota Kanbe
2016-09-14 21:36:00 +09:00
parent e6f4d07a87
commit 3c39f1e737
8 changed files with 38 additions and 38 deletions

View File

@@ -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)
}
}

View File

@@ -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]
}

View File

@@ -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) {

View File

@@ -39,5 +39,3 @@ const (
type ResultWriter interface {
Write([]models.ScanResult) error
}
var resultDirPath string