mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
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:
@@ -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
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user