Improve readability of ifc.Loader's Repo() method (#4857)

* Improve ldr Repo() method readability

* Change Repo() implementations and calls

* Improve readability of conditions in ldr.New()

* Fix details
This commit is contained in:
Anna Song
2022-11-08 14:48:14 -08:00
committed by GitHub
parent 6d9b54004e
commit beb2825f82
7 changed files with 35 additions and 35 deletions

View File

@@ -116,18 +116,12 @@ func (ll *locLoader) Load(path string) ([]byte, error) {
// New returns a Loader at path if path is a valid localize root.
// Otherwise, New returns an error.
func (ll *locLoader) New(path string) (ifc.Loader, error) {
repoSpec, err := git.NewRepoSpecFromURL(path)
if err == nil && repoSpec.Ref == "" {
return nil, errors.Errorf("localize remote root '%s' missing ref query string parameter", path)
}
ldr, err := ll.Loader.New(path)
if err != nil {
return nil, errors.WrapPrefixf(err, "invalid root reference")
}
var isRemote bool
if _, isRemote = ldr.Repo(); !isRemote {
if repo := ldr.Repo(); repo == "" {
if ll.local && !filesys.ConfirmedDir(ldr.Root()).HasPrefix(ll.args.Scope) {
return nil, errors.Errorf("root '%s' outside localize scope '%s'", ldr.Root(), ll.args.Scope)
}
@@ -135,12 +129,14 @@ func (ll *locLoader) New(path string) (ifc.Loader, error) {
return nil, errors.Errorf(
"root '%s' references into localize destination '%s'", ldr.Root(), ll.args.NewDir)
}
} else if !hasRef(path) {
return nil, errors.Errorf("localize remote root %q missing ref query string parameter", path)
}
return &locLoader{
fSys: ll.fSys,
args: ll.args,
Loader: ldr,
local: ll.local && !isRemote,
local: ll.local && ldr.Repo() == "",
}, nil
}

View File

@@ -38,9 +38,7 @@ func checkNewLocLoader(req *require.Assertions, ldr ifc.Loader, args *lclzr.LocA
func checkLoader(req *require.Assertions, ldr ifc.Loader, root string) {
req.Equal(root, ldr.Root())
repo, isRemote := ldr.Repo()
req.Equal(false, isRemote)
req.Equal("", repo)
req.Empty(ldr.Repo())
}
func checkLocArgs(req *require.Assertions, args *lclzr.LocArgs, target string, scope string, newDir string, fSys filesys.FileSystem) {

View File

@@ -16,7 +16,7 @@ import (
// establishScope returns the scope given localize arguments and targetLdr at targetArg
func establishScope(scopeArg string, targetArg string, targetLdr ifc.Loader, fSys filesys.FileSystem) (filesys.ConfirmedDir, error) {
if _, isRemote := targetLdr.Repo(); isRemote {
if targetLdr.Repo() != "" {
if scopeArg != "" {
return "", errors.Errorf("scope '%s' specified for remote localize target '%s'", scopeArg, targetArg)
}
@@ -65,7 +65,7 @@ func createNewDir(newDirArg string, targetLdr ifc.Loader, spec *git.RepoSpec, fS
// and spec of target, which is nil if target is local
func defaultNewDir(targetLdr ifc.Loader, spec *git.RepoSpec) string {
targetDir := filepath.Base(targetLdr.Root())
if repo, isRemote := targetLdr.Repo(); isRemote {
if repo := targetLdr.Repo(); repo != "" {
// kustomize doesn't download repo into repo-named folder
// must find repo folder name from url
if repo == targetLdr.Root() {
@@ -89,3 +89,12 @@ func urlBase(url string) string {
}
return cleaned[i+1:]
}
// hasRef checks if repoURL has ref query string parameter
func hasRef(repoURL string) bool {
repoSpec, err := git.NewRepoSpecFromURL(repoURL)
if err != nil {
log.Fatalf("unable to parse validated root url: %s", err.Error())
}
return repoSpec.Ref != ""
}