-
Notifications
You must be signed in to change notification settings - Fork 526
/
config.go
63 lines (51 loc) · 1.7 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package presenter
import (
"errors"
"fmt"
"os"
"text/template"
presenterTemplate "github.com/anchore/grype/grype/presenter/template"
)
// Config is the presenter domain's configuration data structure.
type Config struct {
format format
templateFilePath string
}
// ValidatedConfig returns a new, validated presenter.Config. If a valid Config cannot be created using the given input,
// an error is returned.
func ValidatedConfig(output, outputTemplateFile string) (Config, error) {
format := parse(output)
if format == unknownFormat {
return Config{}, fmt.Errorf("unsupported output format %q, supported formats are: %+v", output,
AvailableFormats)
}
if format == templateFormat {
if outputTemplateFile == "" {
return Config{}, fmt.Errorf("must specify path to template file when using %q output format",
templateFormat)
}
if _, err := os.Stat(outputTemplateFile); errors.Is(err, os.ErrNotExist) {
// file does not exist
return Config{}, fmt.Errorf("template file %q does not exist",
outputTemplateFile)
}
if _, err := os.ReadFile(outputTemplateFile); err != nil {
return Config{}, fmt.Errorf("unable to read template file: %w", err)
}
if _, err := template.New("").Funcs(presenterTemplate.FuncMap).ParseFiles(outputTemplateFile); err != nil {
return Config{}, fmt.Errorf("unable to parse template: %w", err)
}
return Config{
format: format,
templateFilePath: outputTemplateFile,
}, nil
}
if outputTemplateFile != "" {
return Config{}, fmt.Errorf("specified template file %q, but "+
"%q output format must be selected in order to use a template file",
outputTemplateFile, templateFormat)
}
return Config{
format: format,
}, nil
}