* refactor: cve_client.go * refactor: don't use global Config in private func * remove import alias for config * refactor: dbclient * refactor: resultDir * refactor: resultsDir * refactor * refactor: gost * refactor: db client * refactor: cveDB * refactor: cvedb * refactor: exploitDB * refactor: remove detector/dbclient.go * refactor: writer * refactor: syslog writer * refactor: ips * refactor: ensureResultDir * refactor: proxy * fix(db): call CloseDB * add integration test * feat(report): sort array in json * sort func for json diff * add build-int to makefile * add int-rds-redis to makefile * fix: test case, makefile * fix makefile * show cve count after diff * make diff * diff -c * sort exploits in json for diff * sort metasploit, exploit
		
			
				
	
	
		
			104 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			104 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package reporter
 | 
						|
 | 
						|
import (
 | 
						|
	"encoding/json"
 | 
						|
	"io/ioutil"
 | 
						|
	"os"
 | 
						|
	"path/filepath"
 | 
						|
 | 
						|
	"github.com/future-architect/vuls/models"
 | 
						|
	"golang.org/x/xerrors"
 | 
						|
)
 | 
						|
 | 
						|
// LocalFileWriter writes results to a local file.
 | 
						|
type LocalFileWriter struct {
 | 
						|
	CurrentDir        string
 | 
						|
	DiffPlus          bool
 | 
						|
	DiffMinus         bool
 | 
						|
	FormatJSON        bool
 | 
						|
	FormatCsv         bool
 | 
						|
	FormatFullText    bool
 | 
						|
	FormatOneLineText bool
 | 
						|
	FormatList        bool
 | 
						|
	Gzip              bool
 | 
						|
}
 | 
						|
 | 
						|
func (w LocalFileWriter) Write(rs ...models.ScanResult) (err error) {
 | 
						|
	if w.FormatOneLineText {
 | 
						|
		path := filepath.Join(w.CurrentDir, "summary.txt")
 | 
						|
		text := formatOneLineSummary(rs...)
 | 
						|
		if err := w.writeFile(path, []byte(text), 0600); err != nil {
 | 
						|
			return xerrors.Errorf(
 | 
						|
				"Failed to write to file. path: %s, err: %w",
 | 
						|
				path, err)
 | 
						|
		}
 | 
						|
	}
 | 
						|
 | 
						|
	for _, r := range rs {
 | 
						|
		r.SortForJSONOutput()
 | 
						|
 | 
						|
		path := filepath.Join(w.CurrentDir, r.ReportFileName())
 | 
						|
		if w.FormatJSON {
 | 
						|
			p := path + ".json"
 | 
						|
			if w.DiffPlus || w.DiffMinus {
 | 
						|
				p = path + "_diff.json"
 | 
						|
			}
 | 
						|
			var b []byte
 | 
						|
			if b, err = json.MarshalIndent(r, "", "    "); err != nil {
 | 
						|
				return xerrors.Errorf("Failed to Marshal to JSON: %w", err)
 | 
						|
			}
 | 
						|
			if err := w.writeFile(p, b, 0600); err != nil {
 | 
						|
				return xerrors.Errorf("Failed to write JSON. path: %s, err: %w", p, err)
 | 
						|
			}
 | 
						|
		}
 | 
						|
 | 
						|
		if w.FormatList {
 | 
						|
			p := path + "_short.txt"
 | 
						|
			if w.DiffPlus || w.DiffMinus {
 | 
						|
				p = path + "_short_diff.txt"
 | 
						|
			}
 | 
						|
			if err := w.writeFile(
 | 
						|
				p, []byte(formatList(r)), 0600); err != nil {
 | 
						|
				return xerrors.Errorf(
 | 
						|
					"Failed to write text files. path: %s, err: %w", p, err)
 | 
						|
			}
 | 
						|
		}
 | 
						|
 | 
						|
		if w.FormatFullText {
 | 
						|
			p := path + "_full.txt"
 | 
						|
			if w.DiffPlus || w.DiffMinus {
 | 
						|
				p = path + "_full_diff.txt"
 | 
						|
			}
 | 
						|
 | 
						|
			if err := w.writeFile(
 | 
						|
				p, []byte(formatFullPlainText(r)), 0600); err != nil {
 | 
						|
				return xerrors.Errorf(
 | 
						|
					"Failed to write text files. path: %s, err: %w", p, err)
 | 
						|
			}
 | 
						|
		}
 | 
						|
 | 
						|
		if w.FormatCsv {
 | 
						|
			p := path + ".csv"
 | 
						|
			if w.DiffPlus || w.DiffMinus {
 | 
						|
				p = path + "_diff.csv"
 | 
						|
			}
 | 
						|
			if err := formatCsvList(r, p); err != nil {
 | 
						|
				return xerrors.Errorf("Failed to write CSV: %s, %w", p, err)
 | 
						|
			}
 | 
						|
		}
 | 
						|
 | 
						|
	}
 | 
						|
	return nil
 | 
						|
}
 | 
						|
 | 
						|
func (w LocalFileWriter) writeFile(path string, data []byte, perm os.FileMode) (err error) {
 | 
						|
	if w.Gzip {
 | 
						|
		data, err = gz(data)
 | 
						|
		if err != nil {
 | 
						|
			return err
 | 
						|
		}
 | 
						|
		path += ".gz"
 | 
						|
	}
 | 
						|
	return ioutil.WriteFile(path, []byte(data), perm)
 | 
						|
}
 |