Skip to content

Commit

Permalink
Update documentation for file extensions (open-policy-agent#563)
Browse files Browse the repository at this point in the history
Signed-off-by: John Reese <[email protected]>
  • Loading branch information
jpreese authored May 10, 2021
1 parent a3ab727 commit a901976
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
1 change: 0 additions & 1 deletion parser/docker/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,4 @@ RUN go build -o conftest`
if stage != 1 {
t.Errorf("expected command to be in stage 1, not stage: %v", stage)
}
t.Logf("%v", commands)
}
24 changes: 12 additions & 12 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,32 +89,32 @@ func New(parser string) (Parser, error) {
// NewFromPath returns a file parser based on the file type
// that exists at the given path.
func NewFromPath(path string) (Parser, error) {

// We use the YAML parser as the default when passing in configuration
// data through standard input. This can be overridden by using the parser flag.
if path == "-" {
return New(YAML)
}

fileName := strings.ToLower(filepath.Base(path))
if fileName == "dockerfile" || strings.Contains(fileName, "dockerfile.") {
return New(Dockerfile)
}

// The yml file extension is the default when a file extension
// was not found.
fileExtension := "yml"
if len(filepath.Ext(path)) > 0 {
fileExtension = filepath.Ext(path)[1:]
fileExtension = strings.ToLower(filepath.Ext(path)[1:])
}

if fileExtension == "yml" || fileExtension == "yaml" {
return New(YAML)
// A Dockerfile can either be a file named Dockerfile, be prefixed with
// Dockerfile, or have Dockerfile as its extension.
//
// For example: Dockerfile, Dockerfile.debug, dev.Dockerfile
if fileName == "dockerfile" || strings.HasPrefix(fileName, "dockerfile.") || fileExtension == "dockerfile" {
return New(Dockerfile)
}

if strings.EqualFold(fileExtension, "dockerfile") {
return New(Dockerfile)
if fileExtension == "yml" || fileExtension == "yaml" {
return New(YAML)
}

// When parsing Terraform files, the default parser to use
// should be the latest HCL parser.
if fileExtension == "tf" || fileExtension == "tfvars" {
return New(HCL2)
}
Expand Down

0 comments on commit a901976

Please sign in to comment.