Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions pkg/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"os"
"path/filepath"
"regexp"
"runtime"
"strings"

"github.com/chainguard-dev/clog"
Expand Down Expand Up @@ -166,22 +167,34 @@ func removeRules(data []byte, rulesToRemove []string) []byte {
return newlinePattern.ReplaceAll(modified, []byte("\n\n"))
}

// findRoot locates the repository root on the fly.
func findRoot(start string) string {
current := start
// findRoot locates the packages's root directory on the fly.
func findRoot() (string, error) {
_, here, _, ok := runtime.Caller(0)
if !ok {
return "", fmt.Errorf("failed to get current file path")
}

dir := filepath.Dir(here)
current := dir
for {
next := filepath.Join(current, "rules")
if _, err := os.Stat(next); err == nil {
return current
rulesPath := filepath.Join(current, "rules")
if fi, err := os.Stat(rulesPath); err == nil && fi.IsDir() {
return current, nil
}

parent := filepath.Dir(current)
if parent == current {
return ""
break
}

current = parent
}

rulesPath := filepath.Join(filepath.Dir(dir), "rules")
if fi, err := os.Stat(rulesPath); err == nil && fi.IsDir() {
return filepath.Dir(dir), nil
}

return "", fmt.Errorf("could not find rules directory from %s", dir)
}

// replaceGlobal updates the include string to reference the absolute path of rules/global/global.yara
Expand All @@ -205,17 +218,10 @@ func Recursive(ctx context.Context, fss []fs.FS) (*yarax.Rules, error) {
return nil, fmt.Errorf("yarax compiler: %w", err)
}

// use the current working directory to determine the root path
// this only needs to be done once
cwd, err := os.Getwd()
if err != nil {
return nil, err
}
abs, err := filepath.Abs(cwd)
rootPath, err := findRoot()
if err != nil {
return nil, err
}
rootPath := findRoot(abs)

rulesToRemove := getRulesToRemove()

Expand Down
Loading