mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 05:40:11 +01:00
Factor command line parsing into an internal package for reuse.
This commit is contained in:
parent
27ec66353c
commit
95bd705036
6 changed files with 281 additions and 272 deletions
|
|
@ -10,84 +10,11 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
_ "embed" // Used for embedding.
|
||||
"encoding/json"
|
||||
"html/template"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/csaf-poc/csaf_distribution/v2/internal/options"
|
||||
)
|
||||
|
||||
//go:embed tmpl/report.html
|
||||
var reportHTML string
|
||||
|
||||
// writeJSON writes the JSON encoding of the given report to the given stream.
|
||||
// It returns nil, otherwise an error.
|
||||
func writeJSON(report *Report, w io.WriteCloser) error {
|
||||
enc := json.NewEncoder(w)
|
||||
enc.SetIndent("", " ")
|
||||
err := enc.Encode(report)
|
||||
if e := w.Close(); err != nil {
|
||||
err = e
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// writeHTML writes the given report to the given writer, it uses the template
|
||||
// in the "reportHTML" variable. It returns nil, otherwise an error.
|
||||
func writeHTML(report *Report, w io.WriteCloser) error {
|
||||
tmpl, err := template.New("Report HTML").Parse(reportHTML)
|
||||
if err != nil {
|
||||
w.Close()
|
||||
return err
|
||||
}
|
||||
buf := bufio.NewWriter(w)
|
||||
|
||||
if err := tmpl.Execute(buf, report); err != nil {
|
||||
w.Close()
|
||||
return err
|
||||
}
|
||||
|
||||
err = buf.Flush()
|
||||
if e := w.Close(); err == nil {
|
||||
err = e
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
type nopCloser struct{ io.Writer }
|
||||
|
||||
func (nc *nopCloser) Close() error { return nil }
|
||||
|
||||
// writeReport defines where to write the report according to the "output" flag option.
|
||||
// It calls also the "writeJSON" or "writeHTML" function according to the "format" flag option.
|
||||
func writeReport(report *Report, cfg *config) error {
|
||||
|
||||
var w io.WriteCloser
|
||||
|
||||
if cfg.Output == "" {
|
||||
w = &nopCloser{os.Stdout}
|
||||
} else {
|
||||
f, err := os.Create(cfg.Output)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
w = f
|
||||
}
|
||||
|
||||
var writer func(*Report, io.WriteCloser) error
|
||||
|
||||
switch cfg.Format {
|
||||
case "json":
|
||||
writer = writeJSON
|
||||
default:
|
||||
writer = writeHTML
|
||||
}
|
||||
|
||||
return writer(report, w)
|
||||
}
|
||||
|
||||
// run uses a processor to check all the given domains or direct urls
|
||||
// and generates a report.
|
||||
func run(cfg *config, domains []string) (*Report, error) {
|
||||
|
|
@ -101,8 +28,8 @@ func run(cfg *config, domains []string) (*Report, error) {
|
|||
|
||||
func main() {
|
||||
domains, cfg, err := parseArgsConfig()
|
||||
|
||||
errCheck(cfg.prepare())
|
||||
options.ErrorCheck(err)
|
||||
options.ErrorCheck(cfg.prepare())
|
||||
|
||||
if len(domains) == 0 {
|
||||
log.Println("No domain or direct url given.")
|
||||
|
|
@ -110,7 +37,7 @@ func main() {
|
|||
}
|
||||
|
||||
report, err := run(cfg, domains)
|
||||
errCheck(err)
|
||||
options.ErrorCheck(err)
|
||||
|
||||
errCheck(writeReport(report, cfg))
|
||||
options.ErrorCheck(report.write(cfg.Format, cfg.Output))
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue