/
parser.go
45 lines (40 loc) · 986 Bytes
/
parser.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
package parser
import (
"path/filepath"
"github.com/instrumenta/conftest/pkg/parser/cue"
"github.com/instrumenta/conftest/pkg/parser/ini"
"github.com/instrumenta/conftest/pkg/parser/terraform"
"github.com/instrumenta/conftest/pkg/parser/toml"
"github.com/instrumenta/conftest/pkg/parser/yaml"
)
// Parser is the interface implemented by objects that can unmarshal
// bytes into a golang interface
type Parser interface {
Unmarshal(p []byte, v interface{}) error
}
// GetParser returns a Parser for the given file type. Defaults to returning the YAML parser.
func GetParser(fileName string) Parser {
suffix := filepath.Ext(fileName)
switch suffix {
case ".toml":
return &toml.Parser{
FileName: fileName,
}
case ".tf":
return &terraform.Parser{
FileName: fileName,
}
case ".cue":
return &cue.Parser{
FileName: fileName,
}
case ".ini":
return &ini.Parser{
FileName: fileName,
}
default:
return &yaml.Parser{
FileName: fileName,
}
}
}