mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 11:55:40 +01:00
Continue with check framework
This commit is contained in:
parent
7b7a691f71
commit
c4a5aa1901
3 changed files with 114 additions and 16 deletions
35
cmd/csaf_checker/checks.go
Normal file
35
cmd/csaf_checker/checks.go
Normal file
|
|
@ -0,0 +1,35 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
type state struct {
|
||||||
|
domain string
|
||||||
|
}
|
||||||
|
|
||||||
|
func newState(domain string) *state {
|
||||||
|
return &state{domain: domain}
|
||||||
|
}
|
||||||
|
|
||||||
|
type check interface {
|
||||||
|
run(*state) error
|
||||||
|
report(*state, *Domain)
|
||||||
|
}
|
||||||
|
|
||||||
|
func run(domains []string, checks []check) (*Report, error) {
|
||||||
|
|
||||||
|
var report Report
|
||||||
|
|
||||||
|
for _, d := range domains {
|
||||||
|
state := newState(d)
|
||||||
|
for _, ch := range checks {
|
||||||
|
if err := ch.run(state); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
domain := new(Domain)
|
||||||
|
for _, ch := range checks {
|
||||||
|
ch.report(state, domain)
|
||||||
|
}
|
||||||
|
report.Domains = append(report.Domains, domain)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &report, nil
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/jessevdk/go-flags"
|
"github.com/jessevdk/go-flags"
|
||||||
|
|
@ -8,23 +11,80 @@ import (
|
||||||
|
|
||||||
type options struct {
|
type options struct {
|
||||||
Output string `short:"o" long:"output" description:"File name of the generated report" value-name:"REPORT-FILE"`
|
Output string `short:"o" long:"output" description:"File name of the generated report" value-name:"REPORT-FILE"`
|
||||||
|
Format string `short:"f" long:"format" choice:"json" choice:"html" description:"Format of report" default:"json"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkParser(err error) {
|
var checks = []check{
|
||||||
|
// TODO: Implement me!
|
||||||
|
}
|
||||||
|
|
||||||
|
func errCheck(err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
|
if e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
os.Exit(1)
|
log.Fatalf("error: %v\n", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func writeJSON(report *Report, w io.WriteCloser) error {
|
||||||
var opts options
|
enc := json.NewEncoder(w)
|
||||||
|
enc.SetIndent("", " ")
|
||||||
args, err := flags.Parse(&opts)
|
err := enc.Encode(report)
|
||||||
checkParser(err)
|
if e := w.Close(); err != nil {
|
||||||
|
err = e
|
||||||
_ = args
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeHTML(report *Report, w io.WriteCloser) error {
|
||||||
|
// TODO: Implement me!
|
||||||
|
return w.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
type nopCloser struct{ io.Writer }
|
||||||
|
|
||||||
|
func (nc *nopCloser) Close() error { return nil }
|
||||||
|
|
||||||
|
func writeReport(report *Report, opts *options) error {
|
||||||
|
|
||||||
|
var w io.WriteCloser
|
||||||
|
|
||||||
|
if opts.Output == "" {
|
||||||
|
w = &nopCloser{os.Stdout}
|
||||||
|
} else {
|
||||||
|
f, err := os.Create(opts.Output)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
w = f
|
||||||
|
}
|
||||||
|
|
||||||
|
var writer func(*Report, io.WriteCloser) error
|
||||||
|
|
||||||
|
switch opts.Format {
|
||||||
|
case "json":
|
||||||
|
writer = writeJSON
|
||||||
|
default:
|
||||||
|
writer = writeHTML
|
||||||
|
}
|
||||||
|
|
||||||
|
return writer(report, w)
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
opts := new(options)
|
||||||
|
|
||||||
|
domains, err := flags.Parse(opts)
|
||||||
|
errCheck(err)
|
||||||
|
|
||||||
|
if len(domains) == 0 {
|
||||||
|
log.Println("No domains given.")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
report, err := run(domains, checks)
|
||||||
|
errCheck(err)
|
||||||
|
|
||||||
|
errCheck(writeReport(report, opts))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,19 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
type requirement struct {
|
// Requirement a single requirement report of a domain.
|
||||||
|
type Requirement struct {
|
||||||
Num int `json:"num"`
|
Num int `json:"num"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Messages []string `json:"messages"`
|
Messages []string `json:"messages"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type domain struct {
|
// Domain are the results of a domain.
|
||||||
Name string `json:"name"`
|
type Domain struct {
|
||||||
requirements []requirement `json:"requirements"`
|
Name string `json:"name"`
|
||||||
|
requirements []*Requirement `json:"requirements"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type report struct {
|
// Report is the overall report.
|
||||||
Domains []domain `json:"domains"`
|
type Report struct {
|
||||||
|
Domains []*Domain `json:"domains"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue