mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-29 17:41:13 +00:00
Move demandDirectoryRoot into kyaml/filesys (#4677)
* Move demandDirectoryRoot into kyaml/filesys * Make root directory platform-agnostic Support windows root directory. Dogfood own error package. * Use cleaner windows support implementation * Address feedback * Address feedback x2 * Re-apply go.sum changes to avoid CI errors
This commit is contained in:
@@ -3,6 +3,9 @@
|
||||
|
||||
package loader
|
||||
|
||||
import "fmt"
|
||||
import "sigs.k8s.io/kustomize/kyaml/errors"
|
||||
|
||||
var ErrorHTTP = fmt.Errorf("HTTP Error")
|
||||
var (
|
||||
ErrHTTP = errors.Errorf("HTTP Error")
|
||||
ErrRtNotDir = errors.Errorf("must build at directory")
|
||||
)
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
package loader
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
@@ -15,6 +14,7 @@ import (
|
||||
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/internal/git"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||
)
|
||||
|
||||
@@ -123,7 +123,7 @@ func (fl *fileLoader) Root() string {
|
||||
func newLoaderOrDie(
|
||||
lr LoadRestrictorFunc,
|
||||
fSys filesys.FileSystem, path string) *fileLoader {
|
||||
root, err := demandDirectoryRoot(fSys, path)
|
||||
root, err := filesys.ConfirmDir(fSys, path)
|
||||
if err != nil {
|
||||
log.Fatalf("unable to make loader at '%s'; %v", path, err)
|
||||
}
|
||||
@@ -146,30 +146,11 @@ func newLoaderAtConfirmedDir(
|
||||
}
|
||||
}
|
||||
|
||||
// Assure that the given path is in fact a directory.
|
||||
func demandDirectoryRoot(
|
||||
fSys filesys.FileSystem, path string) (filesys.ConfirmedDir, error) {
|
||||
if path == "" {
|
||||
return "", fmt.Errorf(
|
||||
"loader root cannot be empty")
|
||||
}
|
||||
d, f, err := fSys.CleanedAbs(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if f != "" {
|
||||
return "", fmt.Errorf(
|
||||
"'%s' must be a directory so that it can used as a build root",
|
||||
path)
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
// New returns a new Loader, rooted relative to current loader,
|
||||
// or rooted in a temp directory holding a git repo clone.
|
||||
func (fl *fileLoader) New(path string) (ifc.Loader, error) {
|
||||
if path == "" {
|
||||
return nil, fmt.Errorf("new root cannot be empty")
|
||||
return nil, errors.Errorf("new root cannot be empty")
|
||||
}
|
||||
|
||||
repoSpec, err := git.NewRepoSpecFromURL(path)
|
||||
@@ -185,9 +166,9 @@ func (fl *fileLoader) New(path string) (ifc.Loader, error) {
|
||||
if filepath.IsAbs(path) {
|
||||
return nil, fmt.Errorf("new root '%s' cannot be absolute", path)
|
||||
}
|
||||
root, err := demandDirectoryRoot(fl.fSys, fl.root.Join(path))
|
||||
root, err := filesys.ConfirmDir(fl.fSys, fl.root.Join(path))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.WrapPrefixf(err, ErrRtNotDir.Error())
|
||||
}
|
||||
if err = fl.errIfGitContainmentViolation(root); err != nil {
|
||||
return nil, err
|
||||
@@ -317,9 +298,9 @@ func (fl *fileLoader) Load(path string) ([]byte, error) {
|
||||
if resp.StatusCode < 200 || resp.StatusCode > 299 {
|
||||
_, err := git.NewRepoSpecFromURL(path)
|
||||
if err == nil {
|
||||
return nil, errors.New("URL is a git repository")
|
||||
return nil, errors.Errorf("URL is a git repository")
|
||||
}
|
||||
return nil, fmt.Errorf("%w: status code %d (%s)", ErrorHTTP, resp.StatusCode, http.StatusText(resp.StatusCode))
|
||||
return nil, fmt.Errorf("%w: status code %d (%s)", ErrHTTP, resp.StatusCode, http.StatusText(resp.StatusCode))
|
||||
}
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
|
||||
@@ -15,7 +15,6 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/internal/git"
|
||||
"sigs.k8s.io/kustomize/api/konfig"
|
||||
@@ -408,11 +407,8 @@ func TestLocalLoaderReferencingGitBase(t *testing.T) {
|
||||
fSys.MkdirAll(topDir)
|
||||
fSys.MkdirAll(cloneRoot + "/foo/base")
|
||||
|
||||
root, err := demandDirectoryRoot(fSys, topDir)
|
||||
require.NoError(err)
|
||||
|
||||
l1 := newLoaderAtConfirmedDir(
|
||||
RestrictionRootOnly, root, fSys, nil,
|
||||
RestrictionRootOnly, filesys.ConfirmedDir(topDir), fSys, nil,
|
||||
git.DoNothingCloner(filesys.ConfirmedDir(cloneRoot)))
|
||||
require.Equal(topDir, l1.Root())
|
||||
|
||||
@@ -430,11 +426,8 @@ func TestRepoDirectCycleDetection(t *testing.T) {
|
||||
fSys.MkdirAll(topDir)
|
||||
fSys.MkdirAll(cloneRoot)
|
||||
|
||||
root, err := demandDirectoryRoot(fSys, topDir)
|
||||
require.NoError(err)
|
||||
|
||||
l1 := newLoaderAtConfirmedDir(
|
||||
RestrictionRootOnly, root, fSys, nil,
|
||||
RestrictionRootOnly, filesys.ConfirmedDir(topDir), fSys, nil,
|
||||
git.DoNothingCloner(filesys.ConfirmedDir(cloneRoot)))
|
||||
p1 := "github.com/someOrg/someRepo/foo"
|
||||
rs1, err := git.NewRepoSpecFromURL(p1)
|
||||
@@ -455,11 +448,8 @@ func TestRepoIndirectCycleDetection(t *testing.T) {
|
||||
fSys.MkdirAll(topDir)
|
||||
fSys.MkdirAll(cloneRoot)
|
||||
|
||||
root, err := demandDirectoryRoot(fSys, topDir)
|
||||
require.NoError(err)
|
||||
|
||||
l0 := newLoaderAtConfirmedDir(
|
||||
RestrictionRootOnly, root, fSys, nil,
|
||||
RestrictionRootOnly, filesys.ConfirmedDir(topDir), fSys, nil,
|
||||
git.DoNothingCloner(filesys.ConfirmedDir(cloneRoot)))
|
||||
|
||||
p1 := "github.com/someOrg/someRepo1"
|
||||
|
||||
@@ -7,6 +7,7 @@ package loader
|
||||
import (
|
||||
"sigs.k8s.io/kustomize/api/ifc"
|
||||
"sigs.k8s.io/kustomize/api/internal/git"
|
||||
"sigs.k8s.io/kustomize/kyaml/errors"
|
||||
"sigs.k8s.io/kustomize/kyaml/filesys"
|
||||
)
|
||||
|
||||
@@ -25,9 +26,9 @@ func NewLoader(
|
||||
return newLoaderAtGitClone(
|
||||
repoSpec, fSys, nil, git.ClonerUsingGitExec)
|
||||
}
|
||||
root, err := demandDirectoryRoot(fSys, target)
|
||||
root, err := filesys.ConfirmDir(fSys, target)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.WrapPrefixf(err, ErrRtNotDir.Error())
|
||||
}
|
||||
return newLoaderAtConfirmedDir(
|
||||
lr, root, fSys, nil, git.ClonerUsingGitExec), nil
|
||||
|
||||
Reference in New Issue
Block a user