Simplified Loader.New() to always take a directory (not a file)
This commit is contained in:
parent
177dbb7b01
commit
9bad8083a6
|
|
@ -45,20 +45,10 @@ func (l *fileLoader) IsScheme(root string, location string) bool {
|
||||||
return filepath.IsAbs(fullFilePath)
|
return filepath.IsAbs(fullFilePath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the directory of the calculated full file path.
|
|
||||||
// Example: "/home/seans/project", "subdir/file.txt" -> "/home/seans/project/subdir".
|
|
||||||
func (l *fileLoader) Root(root string, location string) (string, error) {
|
|
||||||
fullFilePath, err := l.FullLocation(root, location)
|
|
||||||
if err != nil {
|
|
||||||
return "", err
|
|
||||||
}
|
|
||||||
return filepath.Dir(fullFilePath), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// If location is a full file path, then ignore root. If location is relative, then
|
// 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,
|
// join the root path with the location path. Either root or location can be empty,
|
||||||
// but not both.
|
// but not both. Special case for ".": Expands to current working directory.
|
||||||
// Example: "/home/seans/project", "subdir/file.txt" -> "/home/seans/project/subdir/file.txt".
|
// 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
|
// First, validate the parameters
|
||||||
if len(root) == 0 && len(location) == 0 {
|
if len(root) == 0 && len(location) == 0 {
|
||||||
|
|
@ -72,7 +62,7 @@ func (l *fileLoader) FullLocation(root string, location string) (string, error)
|
||||||
}
|
}
|
||||||
location = currentDir
|
location = currentDir
|
||||||
}
|
}
|
||||||
// Assume the location is an full file path. If not, then join root with location.
|
// Assume the location is a full file path. If not, then join root with location.
|
||||||
fullLocation := location
|
fullLocation := location
|
||||||
if !filepath.IsAbs(location) {
|
if !filepath.IsAbs(location) {
|
||||||
fullLocation = filepath.Join(root, location)
|
fullLocation = filepath.Join(root, location)
|
||||||
|
|
|
||||||
|
|
@ -39,8 +39,6 @@ type loaderImpl struct {
|
||||||
type SchemeLoader interface {
|
type SchemeLoader interface {
|
||||||
// Does this location correspond to this scheme.
|
// Does this location correspond to this scheme.
|
||||||
IsScheme(root string, location string) bool
|
IsScheme(root string, location string) bool
|
||||||
// Calulcate a new root.
|
|
||||||
Root(root string, location string) (string, error)
|
|
||||||
// Combines the root and path into a full location string.
|
// Combines the root and path into a full location string.
|
||||||
FullLocation(root string, path string) (string, error)
|
FullLocation(root string, path string) (string, error)
|
||||||
// Load bytes at scheme-specific location or an error.
|
// Load bytes at scheme-specific location or an error.
|
||||||
|
|
@ -60,13 +58,17 @@ func (l *loaderImpl) Root() string {
|
||||||
return l.root
|
return l.root
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a new Loader rooted at newRoot.
|
// Returns a new Loader rooted at newRoot. "newRoot" MUST be
|
||||||
|
// a directory (not a file). The directory can have a trailing
|
||||||
|
// slash or not.
|
||||||
|
// Example: "/home/seans/project" or "/home/seans/project/"
|
||||||
|
// NOT "/home/seans/project/file.yaml".
|
||||||
func (l *loaderImpl) New(newRoot string) (Loader, error) {
|
func (l *loaderImpl) New(newRoot string) (Loader, error) {
|
||||||
scheme, err := l.getSchemeLoader(newRoot)
|
scheme, err := l.getSchemeLoader(newRoot)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
root, err := scheme.Root(l.root, newRoot)
|
root, err := scheme.FullLocation(l.root, newRoot)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -74,6 +76,8 @@ func (l *loaderImpl) New(newRoot string) (Loader, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Load returns all the bytes read from scheme-specific location or an error.
|
// Load returns all the bytes read from scheme-specific 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) {
|
func (l *loaderImpl) Load(location string) ([]byte, error) {
|
||||||
scheme, err := l.getSchemeLoader(location)
|
scheme, err := l.getSchemeLoader(location)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -49,15 +49,16 @@ func TestLoader_Root(t *testing.T) {
|
||||||
t.Fatalf("Expected error for unknown scheme not returned")
|
t.Fatalf("Expected error for unknown scheme not returned")
|
||||||
}
|
}
|
||||||
|
|
||||||
loader, err := rootLoader.New("/home/seans/project/file.yaml")
|
// Test with trailing slash in directory.
|
||||||
|
loader, err := rootLoader.New("/home/seans/project/")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unexpected in New(): %v\n", err)
|
t.Fatalf("Unexpected in New(): %v\n", err)
|
||||||
}
|
}
|
||||||
if "/home/seans/project" != loader.Root() {
|
if "/home/seans/project/" != loader.Root() {
|
||||||
t.Fatalf("Incorrect Loader Root: %s\n", loader.Root())
|
t.Fatalf("Incorrect Loader Root: %s\n", loader.Root())
|
||||||
}
|
}
|
||||||
|
|
||||||
subLoader, err := loader.New("subdir/file.yaml")
|
subLoader, err := loader.New("subdir")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unexpected in New(): %v\n", err)
|
t.Fatalf("Unexpected in New(): %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
@ -65,7 +66,8 @@ func TestLoader_Root(t *testing.T) {
|
||||||
t.Fatalf("Incorrect Loader Root: %s\n", subLoader.Root())
|
t.Fatalf("Incorrect Loader Root: %s\n", subLoader.Root())
|
||||||
}
|
}
|
||||||
|
|
||||||
anotherLoader, err := loader.New("/home/seans/project2/file.yaml")
|
// Test without trailing slash in directory.
|
||||||
|
anotherLoader, err := loader.New("/home/seans/project2")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unexpected in New(): %v\n", err)
|
t.Fatalf("Unexpected in New(): %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
@ -92,7 +94,7 @@ func TestLoader_Load(t *testing.T) {
|
||||||
fakefs.WriteFile("/home/seans/project2/file.yaml", []byte("This is another yaml file"))
|
fakefs.WriteFile("/home/seans/project2/file.yaml", []byte("This is another yaml file"))
|
||||||
rootLoader := initializeRootLoader(fakefs)
|
rootLoader := initializeRootLoader(fakefs)
|
||||||
|
|
||||||
loader, err := rootLoader.New("/home/seans/project/file.yaml")
|
loader, err := rootLoader.New("/home/seans/project")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Unexpected in New(): %v\n", err)
|
t.Fatalf("Unexpected in New(): %v\n", err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,6 @@ package loadertest
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"k8s.io/kubectl/pkg/kinflate/util/fs"
|
"k8s.io/kubectl/pkg/kinflate/util/fs"
|
||||||
"k8s.io/kubectl/pkg/loader"
|
"k8s.io/kubectl/pkg/loader"
|
||||||
|
|
@ -31,19 +30,15 @@ type FakeLoader struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFakeLoader returns a Loader that delegates calls, and encapsulates
|
// NewFakeLoader returns a Loader that delegates calls, and encapsulates
|
||||||
// a fake file system that the Loader reads from. "initialFilePath" parameter
|
// a fake file system that the Loader reads from. "initialDir" parameter
|
||||||
// must be an absolute path, and it can be a directory if it has a
|
// must be an full, absolute directory (trailing slash doesn't matter).
|
||||||
// trailing slash: "/home/seans/project/" (dir) -- "/home/seans/project" (file)
|
func NewFakeLoader(initialDir string) FakeLoader {
|
||||||
func NewFakeLoader(initialFilePath string) FakeLoader {
|
|
||||||
// Create fake filesystem and inject it into initial Loader.
|
// Create fake filesystem and inject it into initial Loader.
|
||||||
fakefs := fs.MakeFakeFS()
|
fakefs := fs.MakeFakeFS()
|
||||||
if strings.HasSuffix(initialFilePath, "/") {
|
|
||||||
fakefs.Mkdir(initialFilePath, 0x777)
|
|
||||||
}
|
|
||||||
var schemes []loader.SchemeLoader
|
var schemes []loader.SchemeLoader
|
||||||
schemes = append(schemes, loader.NewFileLoader(fakefs))
|
schemes = append(schemes, loader.NewFileLoader(fakefs))
|
||||||
rootLoader := loader.Init(schemes)
|
rootLoader := loader.Init(schemes)
|
||||||
loader, _ := rootLoader.New(initialFilePath)
|
loader, _ := rootLoader.New(initialDir)
|
||||||
return FakeLoader{fs: fakefs, delegate: loader}
|
return FakeLoader{fs: fakefs, delegate: loader}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue