mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-14 02:20:53 +00:00
Drop useless or duplicative code.
This commit is contained in:
@@ -26,18 +26,19 @@ import (
|
||||
|
||||
const currentDir = "."
|
||||
|
||||
// Internal implementation of SchemeLoader interface.
|
||||
type fileLoader struct {
|
||||
// FileLoader loads files from a file system.
|
||||
type FileLoader struct {
|
||||
fs fs.FileSystem
|
||||
}
|
||||
|
||||
// NewFileLoader returns a SchemeLoader to handle a file system.
|
||||
func NewFileLoader(fs fs.FileSystem) SchemeLoader {
|
||||
return &fileLoader{fs: fs}
|
||||
// NewFileLoader returns a new FileLoader.
|
||||
func NewFileLoader(fs fs.FileSystem) *FileLoader {
|
||||
return &FileLoader{fs: fs}
|
||||
}
|
||||
|
||||
// Is the location calculated with the root and location params a full file path.
|
||||
func (l *fileLoader) IsScheme(root string, location string) bool {
|
||||
// IsAbsPath return true if the location calculated with the root
|
||||
// and location params a full file path.
|
||||
func (l *FileLoader) IsAbsPath(root string, location string) bool {
|
||||
fullFilePath, err := l.FullLocation(root, location)
|
||||
if err != nil {
|
||||
return false
|
||||
@@ -45,11 +46,12 @@ func (l *fileLoader) IsScheme(root string, location string) bool {
|
||||
return filepath.IsAbs(fullFilePath)
|
||||
}
|
||||
|
||||
// FullLocation returns some notion of a full path.
|
||||
// If location is a full file path, then ignore root. If location is relative, then
|
||||
// join the root path with the location path. Either root or location can be empty,
|
||||
// but not both. Special case for ".": Expands to current working directory.
|
||||
// Example: "/home/seans/project", "subdir/bar" -> "/home/seans/project/subdir/bar".
|
||||
func (l *fileLoader) FullLocation(root string, location string) (string, error) {
|
||||
func (l *FileLoader) FullLocation(root string, location string) (string, error) {
|
||||
// First, validate the parameters
|
||||
if len(root) == 0 && len(location) == 0 {
|
||||
return "", fmt.Errorf("unable to calculate full location: root and location empty")
|
||||
@@ -72,20 +74,12 @@ func (l *fileLoader) FullLocation(root string, location string) (string, error)
|
||||
|
||||
// Load returns the bytes from reading a file at fullFilePath.
|
||||
// Implements the Loader interface.
|
||||
func (l *fileLoader) Load(fullFilePath string) ([]byte, error) {
|
||||
// Validate path to load from is a full file path.
|
||||
if !filepath.IsAbs(fullFilePath) {
|
||||
return nil, fmt.Errorf("attempting to load file without full file path: %s\n", fullFilePath)
|
||||
}
|
||||
return l.fs.ReadFile(fullFilePath)
|
||||
func (l *FileLoader) Load(p string) ([]byte, error) {
|
||||
return l.fs.ReadFile(p)
|
||||
}
|
||||
|
||||
// GlobLoad returns the map from path to bytes from reading a glob path.
|
||||
// Implements the Loader interface.
|
||||
func (l *fileLoader) GlobLoad(fullFilePath string) (map[string][]byte, error) {
|
||||
// Validate path to load from is a full file path.
|
||||
if !filepath.IsAbs(fullFilePath) {
|
||||
return nil, fmt.Errorf("Attempting to load file without full file path: %s\n", fullFilePath)
|
||||
}
|
||||
return l.fs.ReadFiles(fullFilePath)
|
||||
func (l *FileLoader) GlobLoad(p string) (map[string][]byte, error) {
|
||||
return l.fs.ReadFiles(p)
|
||||
}
|
||||
|
||||
@@ -19,10 +19,9 @@ package loader
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Loader interface exposes methods to read bytes in a scheme-agnostic manner.
|
||||
// The Loader encapsulating a root location to calculate where to read from.
|
||||
// Loader interface exposes methods to read bytes.
|
||||
type Loader interface {
|
||||
// Root returns the scheme-specific string representing the root location for this Loader.
|
||||
// Root returns the root location for this Loader.
|
||||
Root() string
|
||||
// New returns Loader located at newRoot.
|
||||
New(newRoot string) (Loader, error)
|
||||
@@ -32,33 +31,20 @@ type Loader interface {
|
||||
GlobLoad(location string) (map[string][]byte, error)
|
||||
}
|
||||
|
||||
// Private implmentation of Loader interface.
|
||||
// Private implementation of Loader interface.
|
||||
type loaderImpl struct {
|
||||
root string
|
||||
schemes []SchemeLoader
|
||||
}
|
||||
|
||||
// SchemeLoader is the interface for different types of loaders (e.g. fileLoader, httpLoader, etc.)
|
||||
type SchemeLoader interface {
|
||||
// Does this location correspond to this scheme.
|
||||
IsScheme(root string, location string) bool
|
||||
// Combines the root and path into a full location string.
|
||||
FullLocation(root string, path string) (string, error)
|
||||
// Load bytes at scheme-specific location or an error.
|
||||
Load(location string) ([]byte, error)
|
||||
// GlobLoad returns the bytes read from a glob path or an error.
|
||||
GlobLoad(location string) (map[string][]byte, error)
|
||||
fLoader *FileLoader
|
||||
}
|
||||
|
||||
const emptyRoot = ""
|
||||
|
||||
// Init initializes the first loader with the supported schemes.
|
||||
// Example schemes: fileLoader, httpLoader, gitLoader.
|
||||
func Init(schemes []SchemeLoader) Loader {
|
||||
return &loaderImpl{root: emptyRoot, schemes: schemes}
|
||||
// NewLoader initializes the first loader with the supported fLoader.
|
||||
func NewLoader(fl *FileLoader) Loader {
|
||||
return &loaderImpl{root: emptyRoot, fLoader: fl}
|
||||
}
|
||||
|
||||
// Root returns the scheme-specific root location for this Loader.
|
||||
// Root returns the root location for this Loader.
|
||||
func (l *loaderImpl) Root() string {
|
||||
return l.root
|
||||
}
|
||||
@@ -69,53 +55,35 @@ func (l *loaderImpl) Root() string {
|
||||
// Example: "/home/seans/project" or "/home/seans/project/"
|
||||
// NOT "/home/seans/project/file.yaml".
|
||||
func (l *loaderImpl) New(newRoot string) (Loader, error) {
|
||||
scheme, err := l.getSchemeLoader(newRoot)
|
||||
if !l.fLoader.IsAbsPath(l.root, newRoot) {
|
||||
return nil, fmt.Errorf("Not abs path: l.root='%s', loc='%s'\n", l.root, newRoot)
|
||||
}
|
||||
root, err := l.fLoader.FullLocation(l.root, newRoot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
root, err := scheme.FullLocation(l.root, newRoot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &loaderImpl{root: root, schemes: l.schemes}, nil
|
||||
return &loaderImpl{root: root, fLoader: l.fLoader}, nil
|
||||
}
|
||||
|
||||
// Load returns all the bytes read from scheme-specific location or an error.
|
||||
// Load returns all the bytes read from location or an error.
|
||||
// "location" can be an absolute path, or if relative, full location is
|
||||
// calculated from the Root().
|
||||
func (l *loaderImpl) Load(location string) ([]byte, error) {
|
||||
scheme, err := l.getSchemeLoader(location)
|
||||
fullLocation, err := l.fLoader.FullLocation(l.root, location)
|
||||
if err != nil {
|
||||
fmt.Printf("Trouble in fulllocation: %v\n", err)
|
||||
return nil, err
|
||||
}
|
||||
fullLocation, err := scheme.FullLocation(l.root, location)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return scheme.Load(fullLocation)
|
||||
return l.fLoader.Load(fullLocation)
|
||||
}
|
||||
|
||||
// GlobLoad returns a map from path to bytes read from scheme-specific location or an error.
|
||||
// GlobLoad returns a map from path to bytes read from the location or an error.
|
||||
// "location" can be an absolute path, or if relative, full location is
|
||||
// calculated from the Root().
|
||||
func (l *loaderImpl) GlobLoad(location string) (map[string][]byte, error) {
|
||||
scheme, err := l.getSchemeLoader(location)
|
||||
fullLocation, err := l.fLoader.FullLocation(l.root, location)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fullLocation, err := scheme.FullLocation(l.root, location)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return scheme.GlobLoad(fullLocation)
|
||||
}
|
||||
|
||||
// Helper function to parse scheme from location parameter.
|
||||
func (l *loaderImpl) getSchemeLoader(location string) (SchemeLoader, error) {
|
||||
for _, scheme := range l.schemes {
|
||||
if scheme.IsScheme(l.root, location) {
|
||||
return scheme, nil
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("Unknown Scheme: %s, %s\n", l.root, location)
|
||||
return l.fLoader.GlobLoad(fullLocation)
|
||||
}
|
||||
|
||||
@@ -25,10 +25,7 @@ import (
|
||||
)
|
||||
|
||||
func initializeRootLoader(fakefs fs.FileSystem) Loader {
|
||||
var schemes []SchemeLoader
|
||||
schemes = append(schemes, NewFileLoader(fakefs))
|
||||
rootLoader := Init(schemes)
|
||||
return rootLoader
|
||||
return NewLoader(NewFileLoader(fakefs))
|
||||
}
|
||||
|
||||
func TestLoader_Root(t *testing.T) {
|
||||
@@ -46,7 +43,7 @@ func TestLoader_Root(t *testing.T) {
|
||||
}
|
||||
_, err = rootLoader.New("https://google.com/project")
|
||||
if err == nil {
|
||||
t.Fatalf("Expected error for unknown scheme not returned")
|
||||
t.Fatalf("Expected error")
|
||||
}
|
||||
|
||||
// Test with trailing slash in directory.
|
||||
|
||||
Reference in New Issue
Block a user