From a0e7abb48618775233ad0a7a05805896dc0cd265 Mon Sep 17 00:00:00 2001 From: Yujun Zhang Date: Tue, 24 Mar 2020 10:11:50 +0800 Subject: [PATCH] Rename `resourceGetter` to `remoteTargetGetter` for readability --- api/loader/fileloader.go | 12 ++--- api/loader/loader.go | 6 +-- api/loader/resource.go | 103 --------------------------------------- 3 files changed, 9 insertions(+), 112 deletions(-) delete mode 100644 api/loader/resource.go diff --git a/api/loader/fileloader.go b/api/loader/fileloader.go index 25552287040..8e5b3113a60 100644 --- a/api/loader/fileloader.go +++ b/api/loader/fileloader.go @@ -97,10 +97,10 @@ type fileLoader struct { // If this is non-nil, the files were // obtained from the given resource - rscSpec *resourceSpec + rscSpec *remoteTargetSpec // Used to get resources - getter resourceGetter + getter remoteTargetGetter // Used to clean up, as needed. cleaner func() error @@ -134,14 +134,14 @@ func newLoaderOrDie( log.Fatalf("unable to make loader at '%s'; %v", path, err) } return newLoaderAtConfirmedDir( - lr, root, fSys, nil, git.ClonerUsingGitExec, getResource) + lr, root, fSys, nil, git.ClonerUsingGitExec, getRemoteTarget) } // newLoaderAtConfirmedDir returns a new fileLoader with given root. func newLoaderAtConfirmedDir( lr LoadRestrictorFunc, root filesys.ConfirmedDir, fSys filesys.FileSystem, - referrer *fileLoader, cloner git.Cloner, getter resourceGetter) *fileLoader { + referrer *fileLoader, cloner git.Cloner, getter remoteTargetGetter) *fileLoader { return &fileLoader{ loadRestrictor: lr, root: root, @@ -179,7 +179,7 @@ func (fl *fileLoader) New(path string) (ifc.Loader, error) { return nil, fmt.Errorf("new root cannot be empty") } - ldr, errGet := newResourceGetter(path, fl.fSys, nil, fl.cloner, fl.getter) + ldr, errGet := newLoaderAtGetter(path, fl.fSys, nil, fl.cloner, fl.getter) if errGet == nil { return ldr, nil } @@ -215,7 +215,7 @@ func (fl *fileLoader) New(path string) (ifc.Loader, error) { // directory holding a cloned git repo. func newLoaderAtGitClone( repoSpec *git.RepoSpec, fSys filesys.FileSystem, - referrer *fileLoader, cloner git.Cloner, getter resourceGetter) (ifc.Loader, error) { + referrer *fileLoader, cloner git.Cloner, getter remoteTargetGetter) (ifc.Loader, error) { cleaner := repoSpec.Cleaner(fSys) err := cloner(repoSpec) if err != nil { diff --git a/api/loader/loader.go b/api/loader/loader.go index ad4040afe88..daab269436a 100644 --- a/api/loader/loader.go +++ b/api/loader/loader.go @@ -22,7 +22,7 @@ func NewLoader( lr LoadRestrictorFunc, target string, fSys filesys.FileSystem) (ifc.Loader, error) { - ldr, errGet := newResourceGetter(target, fSys, nil, git.ClonerUsingGitExec, getResource) + ldr, errGet := newLoaderAtGetter(target, fSys, nil, git.ClonerUsingGitExec, getRemoteTarget) if errGet == nil { return ldr, nil } @@ -31,12 +31,12 @@ func NewLoader( if errGit == nil { // The target qualifies as a remote git target. return newLoaderAtGitClone( - repoSpec, fSys, nil, git.ClonerUsingGitExec, getResource) + repoSpec, fSys, nil, git.ClonerUsingGitExec, getRemoteTarget) } root, errDir := demandDirectoryRoot(fSys, target) if errDir == nil { - return newLoaderAtConfirmedDir(lr, root, fSys, nil, git.ClonerUsingGitExec, getResource), nil + return newLoaderAtConfirmedDir(lr, root, fSys, nil, git.ClonerUsingGitExec, getRemoteTarget), nil } return nil, fmt.Errorf("Error creating new loader with git: %v, dir: %v, get: %v", errGit, errDir, errGet) diff --git a/api/loader/resource.go b/api/loader/resource.go deleted file mode 100644 index cf13d975fe3..00000000000 --- a/api/loader/resource.go +++ /dev/null @@ -1,103 +0,0 @@ -// Copyright 2020 The Kubernetes Authors. -// SPDX-License-Identifier: Apache-2.0 - -package loader - -import ( - "context" - "log" - "os" - - "github.com/yujunz/go-getter" - "sigs.k8s.io/kustomize/api/filesys" - "sigs.k8s.io/kustomize/api/ifc" - "sigs.k8s.io/kustomize/api/internal/git" -) - -type resourceSpec struct { - // Raw is the original resource in kustomization.yaml - Raw string - - // Dir is where the resource is saved - Dir filesys.ConfirmedDir -} - -// Getter is a function that can gets resource -type resourceGetter func(rs *resourceSpec) error - -func newResourceGetter(raw string, fSys filesys.FileSystem, referrer *fileLoader, cloner git.Cloner, getter resourceGetter) (ifc.Loader, error) { - rs := &resourceSpec{ - Raw: raw, - } - - cleaner := func() error { - return fSys.RemoveAll(rs.Dir.String()) - } - - if err := getter(rs); err != nil { - cleaner() - return nil, err - } - - // TODO(yujunz): check file or directory - - return &fileLoader{ - loadRestrictor: RestrictionRootOnly, - // TODO(yujunz): limit to getter root - root: rs.Dir, - referrer: referrer, - fSys: fSys, - cloner: cloner, - rscSpec: rs, - getter: getter, - cleaner: cleaner, - }, nil -} - -func getResource(rs *resourceSpec) error { - var err error - - rs.Dir, err = filesys.NewTmpConfirmedDir() - if err != nil { - return err - } - - // Get the pwd - pwd, err := os.Getwd() - if err != nil { - log.Fatalf("Error getting wd: %s", err) - } - - opts := []getter.ClientOption{} - client := &getter.Client{ - Ctx: context.TODO(), - Src: rs.Raw, - Dst: rs.Dir.String(), - Pwd: pwd, - Mode: getter.ClientModeAny, - Detectors: []getter.Detector{ - new(getter.GitHubDetector), - new(getter.GitDetector), - new(getter.BitBucketDetector), - }, - Options: opts, - } - return client.Get() -} - -func getNothing(rs *resourceSpec) error { - var err error - rs.Dir, err = filesys.NewTmpConfirmedDir() - if err != nil { - return err - } - - // Get the pwd - pwd, err := os.Getwd() - if err != nil { - log.Fatalf("Error getting wd: %s", err) - } - - _, err = getter.Detect(rs.Raw, pwd, []getter.Detector{}) - return err -}