mirror of
https://github.com/gocsaf/csaf.git
synced 2025-12-22 18:15:42 +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
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/jessevdk/go-flags"
|
||||
|
|
@ -8,23 +11,80 @@ import (
|
|||
|
||||
type options struct {
|
||||
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 e, ok := err.(*flags.Error); ok && e.Type == flags.ErrHelp {
|
||||
os.Exit(0)
|
||||
}
|
||||
os.Exit(1)
|
||||
log.Fatalf("error: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
var opts options
|
||||
|
||||
args, err := flags.Parse(&opts)
|
||||
checkParser(err)
|
||||
|
||||
_ = args
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
type requirement struct {
|
||||
// Requirement a single requirement report of a domain.
|
||||
type Requirement struct {
|
||||
Num int `json:"num"`
|
||||
Description string `json:"description"`
|
||||
Messages []string `json:"messages"`
|
||||
}
|
||||
|
||||
type domain struct {
|
||||
Name string `json:"name"`
|
||||
requirements []requirement `json:"requirements"`
|
||||
// Domain are the results of a domain.
|
||||
type Domain struct {
|
||||
Name string `json:"name"`
|
||||
requirements []*Requirement `json:"requirements"`
|
||||
}
|
||||
|
||||
type report struct {
|
||||
Domains []domain `json:"domains"`
|
||||
// Report is the overall report.
|
||||
type Report struct {
|
||||
Domains []*Domain `json:"domains"`
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue