-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
48 lines (38 loc) · 1.11 KB
/
main.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
package main
import (
"io"
"log"
"os"
"github.com/kubewarden/cel-policy/internal/settings"
"github.com/kubewarden/cel-policy/internal/validate"
)
const expectedArgsCount = 2
func main() {
// Since we use log.Fatal* functions to write to stderr and exit with a non-zero status code,
// we need to disable the default log prefix that includes the date and time, as it would
// be bubbled up to the message of the response returned to the caller.
log.SetFlags(0)
if len(os.Args) != expectedArgsCount {
log.Fatalln("Wrong usage, expected either 'validate' or 'validate-settings'")
}
input, err := io.ReadAll(os.Stdin)
if err != nil {
log.Panicf("Cannot read input: %v", err)
}
var response []byte
switch os.Args[1] {
case "validate":
response, err = validate.Validate(input)
case "validate-settings":
response, err = settings.ValidateSettings(input)
default:
log.Fatalf("wrong subcommand: '%s' - use either 'validate' or 'validate-settings'", os.Args[1])
}
if err != nil {
log.Fatal(err)
}
_, err = os.Stdout.Write(response)
if err != nil {
log.Fatalf("Cannot write response: %v", err)
}
}