Use util.WithPrefix (#9005)

Tiny change to use this helper where we can.

Signed-off-by: Anders Eknert <anders.eknert@apple.com>
This commit is contained in:
Anders Eknert
2026-08-12 15:03:38 +02:00
committed by GitHub
parent 8a424f7652
commit 413f49f28c
2 changed files with 6 additions and 14 deletions
+3 -6
View File
@@ -7,7 +7,8 @@ import (
"encoding/json"
"errors"
"io"
"strings"
"github.com/open-policy-agent/opa/v1/util"
)
type TarGzWriter struct {
@@ -62,11 +63,7 @@ func MustWriteTarGz(files [][2]string) *bytes.Buffer {
defer tgw.Close()
for _, file := range files {
if !strings.HasPrefix(file[0], "/") {
file[0] = "/" + file[0]
}
if err := tgw.WriteFile(file[0], []byte(file[1])); err != nil {
if err := tgw.WriteFile(util.WithPrefix(file[0], "/"), []byte(file[1])); err != nil {
panic(err)
}
}
+3 -8
View File
@@ -14,6 +14,7 @@ import (
"sync"
"github.com/open-policy-agent/opa/v1/loader/filter"
"github.com/open-policy-agent/opa/v1/util"
"github.com/open-policy-agent/opa/v1/storage"
)
@@ -193,10 +194,7 @@ func (d *dirLoader) WithFollowSymlinks(followSymlinks bool) DirectoryLoader {
func formatPath(fileName string, root string, pathFormat PathFormat) string {
switch pathFormat {
case SlashRooted:
if !strings.HasPrefix(fileName, string(filepath.Separator)) {
return string(filepath.Separator) + fileName
}
return fileName
return util.WithPrefix(fileName, string(filepath.Separator))
case Chrooted:
// Trim off the root directory and return path as if chrooted
result := strings.TrimPrefix(fileName, filepath.FromSlash(root))
@@ -206,10 +204,7 @@ func formatPath(fileName string, root string, pathFormat PathFormat) string {
if root == "." && (filepath.Base(fileName) == ManifestExt || filepath.Base(fileName) == ManifestProtoExt) {
result = fileName
}
if !strings.HasPrefix(result, string(filepath.Separator)) {
result = string(filepath.Separator) + result
}
return result
return util.WithPrefix(result, string(filepath.Separator))
case Passthrough:
fallthrough
default: