Reduce size of pgmconfig package

This commit is contained in:
Jeffrey Regan
2019-10-02 16:27:13 -07:00
committed by jregan
parent 14b0a65091
commit baa0296a12
53 changed files with 498 additions and 285 deletions

View File

@@ -63,7 +63,7 @@ func DefaultSrcRoot() (string, error) {
nope = append(nope, root)
root = filepath.Join(
pgmconfig.HomeDir(),
homeDir(),
pgmconfig.ProgramName, pgmconfig.PluginRoot)
if FileExists(root) {
return root, nil

View File

@@ -5,18 +5,27 @@ package plugins
import (
"fmt"
"github.com/spf13/pflag"
"os"
"path/filepath"
"runtime"
"github.com/spf13/pflag"
"sigs.k8s.io/kustomize/v3/pkg/pgmconfig"
"sigs.k8s.io/kustomize/v3/pkg/types"
)
const (
PluginSymbol = "KustomizePlugin"
BuiltinPluginPackage = "builtin"
// Used with Go plugins.
PluginSymbol = "KustomizePlugin"
// Location of builtins.
BuiltinPluginPackage = "builtin"
// ApiVersion of builtins.
BuiltinPluginApiVersion = BuiltinPluginPackage
flagEnablePluginsName = "enable_alpha_plugins"
flagEnablePluginsHelp = `enable plugins, an alpha feature.
flagEnablePluginsName = "enable_alpha_plugins"
flagEnablePluginsHelp = `enable plugins, an alpha feature.
See https://github.com/kubernetes-sigs/kustomize/blob/master/docs/plugins/README.md
`
flagErrorFmt = `
@@ -36,11 +45,11 @@ func DefaultPluginConfig() *types.PluginConfig {
return &types.PluginConfig{
Enabled: false,
DirectoryPath: filepath.Join(
pgmconfig.ConfigRoot(), pgmconfig.PluginRoot),
configRoot(), pgmconfig.PluginRoot),
}
}
func NotEnabledErr(name string) error {
func notEnabledErr(name string) error {
return fmt.Errorf(
flagErrorFmt,
name,
@@ -53,3 +62,28 @@ func AddFlagEnablePlugins(set *pflag.FlagSet, v *bool) {
v, flagEnablePluginsName,
false, flagEnablePluginsHelp)
}
// Use https://github.com/kirsle/configdir instead?
func configRoot() string {
dir := os.Getenv(pgmconfig.XdgConfigHome)
if len(dir) == 0 {
dir = filepath.Join(
homeDir(), pgmconfig.DefaultConfigSubdir)
}
return filepath.Join(dir, pgmconfig.ProgramName)
}
func homeDir() string {
home := os.Getenv(homeEnv())
if len(home) > 0 {
return home
}
return "~"
}
func homeEnv() string {
if runtime.GOOS == "windows" {
return "USERPROFILE"
}
return "HOME"
}

View File

@@ -0,0 +1,45 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package plugins
import (
"os"
"path/filepath"
"strings"
"testing"
"sigs.k8s.io/kustomize/v3/pkg/pgmconfig"
)
func TestConfigDirNoXdg(t *testing.T) {
xdg, isSet := os.LookupEnv(pgmconfig.XdgConfigHome)
if isSet {
os.Unsetenv(pgmconfig.XdgConfigHome)
}
s := configRoot()
if isSet {
os.Setenv(pgmconfig.XdgConfigHome, xdg)
}
if !strings.HasSuffix(
s,
rootedPath(pgmconfig.DefaultConfigSubdir, pgmconfig.ProgramName)) {
t.Fatalf("unexpected config dir: %s", s)
}
}
func rootedPath(elem ...string) string {
return string(filepath.Separator) + filepath.Join(elem...)
}
func TestConfigDirWithXdg(t *testing.T) {
xdg, isSet := os.LookupEnv(pgmconfig.XdgConfigHome)
os.Setenv(pgmconfig.XdgConfigHome, rootedPath("blah"))
s := configRoot()
if isSet {
os.Setenv(pgmconfig.XdgConfigHome, xdg)
}
if s != rootedPath("blah", pgmconfig.ProgramName) {
t.Fatalf("unexpected config dir: %s", s)
}
}

View File

@@ -114,7 +114,7 @@ func (l *Loader) loadAndConfigurePlugin(
} else if l.pc.Enabled {
c, err = l.loadPlugin(res.OrgId())
} else {
err = NotEnabledErr(res.OrgId().Kind)
err = notEnabledErr(res.OrgId().Kind)
}
if err != nil {
return nil, err

View File

@@ -9,7 +9,7 @@ import (
"sigs.k8s.io/kustomize/v3/internal/loadertest"
"sigs.k8s.io/kustomize/v3/k8sdeps/kunstruct"
. "sigs.k8s.io/kustomize/v3/pkg/plugins"
plugins_test "sigs.k8s.io/kustomize/v3/pkg/plugins/test"
"sigs.k8s.io/kustomize/v3/pkg/plugins/testenv"
"sigs.k8s.io/kustomize/v3/pkg/resmap"
"sigs.k8s.io/kustomize/v3/pkg/resource"
)
@@ -42,7 +42,7 @@ port: "12345"
)
func TestLoader(t *testing.T) {
tc := plugins_test.NewEnvForTest(t).Set()
tc := testenv.NewEnvForTest(t).Set()
defer tc.Reset()
tc.BuildGoPlugin(

View File

@@ -1,7 +1,7 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package test
package testenv
import (
"io/ioutil"
@@ -98,14 +98,14 @@ func (x *EnvForTest) removeWorkDir() {
}
func (x *EnvForTest) setEnv() {
x.oldXdg, x.wasSet = os.LookupEnv(pgmconfig.XDG_CONFIG_HOME)
os.Setenv(pgmconfig.XDG_CONFIG_HOME, x.workDir)
x.oldXdg, x.wasSet = os.LookupEnv(pgmconfig.XdgConfigHome)
os.Setenv(pgmconfig.XdgConfigHome, x.workDir)
}
func (x *EnvForTest) resetEnv() {
if x.wasSet {
os.Setenv(pgmconfig.XDG_CONFIG_HOME, x.oldXdg)
os.Setenv(pgmconfig.XdgConfigHome, x.oldXdg)
} else {
os.Unsetenv(pgmconfig.XDG_CONFIG_HOME)
os.Unsetenv(pgmconfig.XdgConfigHome)
}
}