forked from ymomoi/goval-parser
-
Notifications
You must be signed in to change notification settings - Fork 11
/
example.go
48 lines (44 loc) · 960 Bytes
/
example.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 (
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/quay/goval-parser/oval"
)
func main() {
if len(os.Args) < 2 {
fmt.Printf("usage: %s file1 file2 ...\n", os.Args[0])
return
}
for _, f := range os.Args[1:len(os.Args)] {
fmt.Println(f + ":")
oval, err := readOval(f)
if err != nil {
log.Fatal(err)
}
out := json.NewEncoder(os.Stdout)
out.SetIndent("", " ")
if err := out.Encode(oval); err != nil {
log.Fatal(err)
}
if err := out.Encode(oval.Definitions.Definitions[0].Debian); err != nil {
log.Fatal(err)
}
}
}
// readOval : Read OVAL definitions from file
func readOval(file string) (*oval.Root, error) {
str, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("Can't open file: %s", err)
}
oval := &oval.Root{}
err = xml.Unmarshal([]byte(str), oval)
if err != nil {
return nil, fmt.Errorf("Can't parse XML: %s", err)
}
return oval, nil
}