Skip to content

Commit

Permalink
Identify the type of file and call the respective parser
Browse files Browse the repository at this point in the history
Since bunny wants to keep backwards compatibility with pun, we need to
keep the handling of Dockerfile-like files. Hence, we need to identofy
the format of the given file between a Dockerfile and a bunnyfile. For
that purpose, we check the first non-empty line of the file under
@syntax and if it starts with FROM we assume a Dockerfile and call the
respective function. Otherwise, we call the bunnyfile handler.

Signed-off-by: Charalampos Mainas <[email protected]>
  • Loading branch information
cmainas committed Jan 13, 2025
1 parent f4271ff commit 3449c30
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func bunnyBuilder(ctx context.Context, c client.Client) (*client.Result, error)
}

// Parse packaging/building instructions
packInst, err := hops.ParseDockerFile(fileBytes)
packInst, err := hops.ParseFile(fileBytes)
if err != nil {
return nil, fmt.Errorf("Error parsing building instructions: %v", err)
}
Expand Down Expand Up @@ -218,7 +218,7 @@ func main() {
}

// Parse file with packaging/building instructions
packInst, err = hops.ParseDockerFile(CntrFileContent)
packInst, err = hops.ParseFile(CntrFileContent)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: Could not parse building instructions: %v\n", err)
os.Exit(1)
Expand Down
26 changes: 26 additions & 0 deletions hops/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,32 @@ func ParseDockerFile(fileBytes []byte) (*PackInstructions, error) {
return instr, nil
}

// ParseFile identifies the format of the given file and either calls
// ParseDockerFile or ParseBunnyFile
func ParseFile(fileBytes []byte) (*PackInstructions, error) {
lines := bytes.Split(fileBytes, []byte("\n"))

// First line is always the #syntax
if len(lines) <= 1 {
return nil, fmt.Errorf("Invalid format of file")
}

// Simply check if the first non-empty line starts with FROM
// If it starts we assume a Dockerfile
// otherwise a bunnyfile
for _, line := range lines[1:] {
if len(bytes.TrimSpace(line)) > 0 {
if strings.HasPrefix(string(line), "FROM") {
return ParseDockerFile(fileBytes)
} else {
break
}
}
}

return ParseBunnyFile(fileBytes)
}

func copyIn(base llb.State, from string, src string, dst string) llb.State {
var copyState llb.State
var localSrc llb.State
Expand Down

0 comments on commit 3449c30

Please sign in to comment.