diff --git a/api/internal/plugins/compiler/compiler.go b/api/internal/plugins/compiler/compiler.go index 41ef8022a..75d3a961f 100644 --- a/api/internal/plugins/compiler/compiler.go +++ b/api/internal/plugins/compiler/compiler.go @@ -9,13 +9,10 @@ import ( "os" "os/exec" "path/filepath" - "runtime" "strings" "time" "github.com/pkg/errors" - "sigs.k8s.io/kustomize/api/filesys" - "sigs.k8s.io/kustomize/api/konfig" ) // Compiler creates Go plugin object files. @@ -30,70 +27,6 @@ type Compiler struct { objRoot string } -// DeterminePluginSrcRoot guesses where the user -// has her ${g}/${v}/$lower(${k})/${k}.go files. -func DeterminePluginSrcRoot(fSys filesys.FileSystem) (string, error) { - return konfig.FirstDirThatExistsElseError( - "source directory", fSys, []konfig.NotedFunc{ - { - Note: "relative to unit test", - F: func() string { - return filepath.Clean( - filepath.Join( - os.Getenv("PWD"), - "..", "..", - konfig.RelPluginHome)) - }, - }, - { - Note: "relative to unit test (internal pkg)", - F: func() string { - return filepath.Clean( - filepath.Join( - os.Getenv("PWD"), - "..", "..", "..", "..", - konfig.RelPluginHome)) - }, - }, - { - Note: "relative to api package", - F: func() string { - return filepath.Clean( - filepath.Join( - os.Getenv("PWD"), - "..", "..", "..", - konfig.RelPluginHome)) - }, - }, - { - Note: "old style $GOPATH", - F: func() string { - return filepath.Join( - os.Getenv("GOPATH"), - "src", konfig.DomainName, - konfig.ProgramName, konfig.RelPluginHome) - }, - }, - { - Note: "HOME with literal 'gopath'", - F: func() string { - return filepath.Join( - konfig.HomeDir(), "gopath", - "src", konfig.DomainName, - konfig.ProgramName, konfig.RelPluginHome) - }, - }, - { - Note: "home directory", - F: func() string { - return filepath.Join( - konfig.HomeDir(), konfig.DomainName, - konfig.ProgramName, konfig.RelPluginHome) - }, - }, - }) -} - // NewCompiler returns a new compiler instance. func NewCompiler(srcRoot, objRoot string) *Compiler { return &Compiler{srcRoot: srcRoot, objRoot: objRoot} @@ -109,10 +42,6 @@ func (b *Compiler) SrcRoot() string { return b.srcRoot } -func goBin() string { - return filepath.Join(runtime.GOROOT(), "bin", "go") -} - // Compile reads ${srcRoot}/${g}/${v}/${k}.go // and writes ${objRoot}/${g}/${v}/${k}.so func (b *Compiler) Compile(g, v, k string) error { @@ -158,23 +87,3 @@ func (b *Compiler) Compile(g, v, k string) error { } return nil } - -// FileYoungerThan returns true if the file age is <= the Duration argument. -func FileYoungerThan(path string, d time.Duration) bool { - fi, err := os.Stat(path) - if err != nil { - if os.IsNotExist(err) { - return false - } - } - return time.Since(fi.ModTime()) <= d -} - -func FileExists(name string) bool { - if _, err := os.Stat(name); err != nil { - if os.IsNotExist(err) { - return false - } - } - return true -} diff --git a/api/internal/plugins/compiler/utils.go b/api/internal/plugins/compiler/utils.go new file mode 100644 index 000000000..d117184ff --- /dev/null +++ b/api/internal/plugins/compiler/utils.go @@ -0,0 +1,102 @@ +// Copyright 2020 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package compiler + +import ( + "os" + "path/filepath" + "runtime" + "time" + + "sigs.k8s.io/kustomize/api/filesys" + "sigs.k8s.io/kustomize/api/konfig" +) + +func goBin() string { + return filepath.Join(runtime.GOROOT(), "bin", "go") +} + +// DeterminePluginSrcRoot guesses where the user +// has her ${g}/${v}/$lower(${k})/${k}.go files. +func DeterminePluginSrcRoot(fSys filesys.FileSystem) (string, error) { + return konfig.FirstDirThatExistsElseError( + "source directory", fSys, []konfig.NotedFunc{ + { + Note: "relative to unit test", + F: func() string { + return filepath.Clean( + filepath.Join( + os.Getenv("PWD"), + "..", "..", + konfig.RelPluginHome)) + }, + }, + { + Note: "relative to unit test (internal pkg)", + F: func() string { + return filepath.Clean( + filepath.Join( + os.Getenv("PWD"), + "..", "..", "..", "..", + konfig.RelPluginHome)) + }, + }, + { + Note: "relative to api package", + F: func() string { + return filepath.Clean( + filepath.Join( + os.Getenv("PWD"), + "..", "..", "..", + konfig.RelPluginHome)) + }, + }, + { + Note: "old style $GOPATH", + F: func() string { + return filepath.Join( + os.Getenv("GOPATH"), + "src", konfig.DomainName, + konfig.ProgramName, konfig.RelPluginHome) + }, + }, + { + Note: "HOME with literal 'gopath'", + F: func() string { + return filepath.Join( + konfig.HomeDir(), "gopath", + "src", konfig.DomainName, + konfig.ProgramName, konfig.RelPluginHome) + }, + }, + { + Note: "home directory", + F: func() string { + return filepath.Join( + konfig.HomeDir(), konfig.DomainName, + konfig.ProgramName, konfig.RelPluginHome) + }, + }, + }) +} + +// FileYoungerThan returns true if the file age is <= the Duration argument. +func FileYoungerThan(path string, d time.Duration) bool { + fi, err := os.Stat(path) + if err != nil { + if os.IsNotExist(err) { + return false + } + } + return time.Since(fi.ModTime()) <= d +} + +func FileExists(name string) bool { + if _, err := os.Stat(name); err != nil { + if os.IsNotExist(err) { + return false + } + } + return true +} diff --git a/api/internal/plugins/compiler/utils_test.go b/api/internal/plugins/compiler/utils_test.go new file mode 100644 index 000000000..0cb14eaa2 --- /dev/null +++ b/api/internal/plugins/compiler/utils_test.go @@ -0,0 +1,26 @@ +// Copyright 2020 The Kubernetes Authors. +// SPDX-License-Identifier: Apache-2.0 + +package compiler + +import ( + "path/filepath" + "strings" + "testing" + + "sigs.k8s.io/kustomize/api/filesys" +) + +func TestDeterminePluginSrcRoot(t *testing.T) { + actual, err := DeterminePluginSrcRoot(filesys.MakeFsOnDisk()) + if err != nil { + t.Error(err) + } + if !filepath.IsAbs(actual) { + t.Errorf("expected absolute path, but got '%s'", actual) + } + expectedSuffix := filepath.Join("sigs.k8s.io", "kustomize", "plugin") + if !strings.HasSuffix(actual, expectedSuffix) { + t.Errorf("expected suffix '%s' in '%s'", expectedSuffix, actual) + } +}