1
0
Fork 0
mirror of https://github.com/gocsaf/csaf.git synced 2025-12-22 11:55:40 +01:00

Be more rigid specifying format in config file.

This commit is contained in:
Sascha L. Teichmann 2023-07-25 11:32:18 +02:00
parent 1bdaf5854a
commit 655b8f4db1
2 changed files with 25 additions and 10 deletions

View file

@ -11,6 +11,7 @@ package main
import ( import (
"crypto/tls" "crypto/tls"
"errors" "errors"
"fmt"
"net/http" "net/http"
"github.com/csaf-poc/csaf_distribution/v2/internal/options" "github.com/csaf-poc/csaf_distribution/v2/internal/options"
@ -18,10 +19,12 @@ import (
const defaultPreset = "mandatory" const defaultPreset = "mandatory"
type outputFormat string
type config struct { type config struct {
Output string `short:"o" long:"output" description:"File name of the generated report" value-name:"REPORT-FILE" toml:"output"` Output string `short:"o" long:"output" description:"File name of the generated report" value-name:"REPORT-FILE" toml:"output"`
//lint:ignore SA5008 We are using choice twice: json, html. //lint:ignore SA5008 We are using choice twice: json, html.
Format string `short:"f" long:"format" choice:"json" choice:"html" description:"Format of report" default:"json" toml:"format"` Format outputFormat `short:"f" long:"format" choice:"json" choice:"html" description:"Format of report" default:"json" toml:"format"`
Insecure bool `long:"insecure" description:"Do not check TLS certificates from provider" toml:"insecure"` Insecure bool `long:"insecure" description:"Do not check TLS certificates from provider" toml:"insecure"`
ClientCert *string `long:"client-cert" description:"TLS client certificate file (PEM encoded data)" value-name:"CERT-FILE" toml:"client_cert"` ClientCert *string `long:"client-cert" description:"TLS client certificate file (PEM encoded data)" value-name:"CERT-FILE" toml:"client_cert"`
ClientKey *string `long:"client-key" description:"TLS client private key file (PEM encoded data)" value-name:"KEY-FILE" toml:"client_key"` ClientKey *string `long:"client-key" description:"TLS client private key file (PEM encoded data)" value-name:"KEY-FILE" toml:"client_key"`
@ -47,6 +50,18 @@ var configPaths = []string{
"csaf_checker.toml", "csaf_checker.toml",
} }
// UnmarshalText implements [encoding/text.TextUnmarshaler].
func (of *outputFormat) UnmarshalText(text []byte) error {
s := string(text)
switch s {
case "html", "json":
*of = outputFormat(s)
default:
return fmt.Errorf(`%q is neither "html" nor "json"`, s)
}
return nil
}
// parseArgsConfig parse the command arguments and loads configuration // parseArgsConfig parse the command arguments and loads configuration
// from a configuration file. // from a configuration file.
func parseArgsConfig() ([]string, *config, error) { func parseArgsConfig() ([]string, *config, error) {

View file

@ -154,7 +154,7 @@ func (nc *nopCloser) Close() error { return nil }
// write defines where to write the report according to the "output" flag option. // write 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. // It calls also the "writeJSON" or "writeHTML" function according to the "format" flag option.
func (r *Report) write(format, output string) error { func (r *Report) write(format outputFormat, output string) error {
var w io.WriteCloser var w io.WriteCloser