Move plugin config to main config package.

This commit is contained in:
Jeffrey Regan
2019-11-01 16:23:54 -07:00
parent b6b8f4396f
commit 69d1699963
14 changed files with 61 additions and 62 deletions

6
api/pgmconfig/doc.go Normal file
View File

@@ -0,0 +1,6 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
// Package pgmconfig provides configuration methods and constants
// for the kustomize API.
package pgmconfig

View File

@@ -23,11 +23,11 @@ const (
// An environment variable to consult for kustomization
// configuration data. See:
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
XdgConfigHome = "XDG_CONFIG_HOME"
XdgConfigHomeEnv = "XDG_CONFIG_HOME"
// Use this when XdgConfigHome not defined.
DefaultConfigSubdir = ".config"
// Use this when XdgConfigHomeEnv not defined.
XdgConfigHomeEnvDefault = ".config"
// Program name, for help, finding the XDG_CONFIG_DIR, etc.
// A program name, for use in help, finding the XDG_CONFIG_DIR, etc.
ProgramName = "kustomize"
)

View File

@@ -0,0 +1,96 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package pgmconfig
import (
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/spf13/pflag"
"sigs.k8s.io/kustomize/api/types"
)
const (
// Symbol that must be used inside Go plugins.
PluginSymbol = "KustomizePlugin"
// Location of builtins.
BuiltinPluginPackage = "builtin"
// The value of kubernetes ApiVersion to use in configuration
// files for builtin plugins.
// The value for non-builtins can be anything.
BuiltinPluginApiVersion = BuiltinPluginPackage
// Domain from which kustomize code is imported, for locating
// plugin source code under $GOPATH when GOPATH is defined.
DomainName = "sigs.k8s.io"
// Name of directory housing all plugins.
PluginRoot = "plugin"
flagEnablePluginsName = "enable_alpha_plugins"
flagEnablePluginsHelp = `enable plugins, an alpha feature.
See https://github.com/kubernetes-sigs/kustomize/blob/master/docs/plugins/README.md
`
flagErrorFmt = `
unable to load external plugin %s because plugins disabled
specify the flag
--%s
to %s`
)
func ActivePluginConfig() *types.PluginConfig {
pc := DefaultPluginConfig()
pc.Enabled = true
return pc
}
func DefaultPluginConfig() *types.PluginConfig {
return &types.PluginConfig{
Enabled: false,
DirectoryPath: filepath.Join(configRoot(), PluginRoot),
}
}
func NotEnabledErr(name string) error {
return fmt.Errorf(
flagErrorFmt,
name,
flagEnablePluginsName,
flagEnablePluginsHelp)
}
func AddFlagEnablePlugins(set *pflag.FlagSet, v *bool) {
set.BoolVar(
v, flagEnablePluginsName,
false, flagEnablePluginsHelp)
}
// Use https://github.com/kirsle/configdir instead?
func configRoot() string {
dir := os.Getenv(XdgConfigHomeEnv)
if len(dir) == 0 {
dir = filepath.Join(
HomeDir(), XdgConfigHomeEnvDefault)
}
return filepath.Join(dir, 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,43 @@
// Copyright 2019 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package pgmconfig
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestConfigDirNoXdg(t *testing.T) {
xdg, isSet := os.LookupEnv(XdgConfigHomeEnv)
if isSet {
os.Unsetenv(XdgConfigHomeEnv)
}
s := configRoot()
if isSet {
os.Setenv(XdgConfigHomeEnv, xdg)
}
if !strings.HasSuffix(
s,
rootedPath(XdgConfigHomeEnvDefault, 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(XdgConfigHomeEnv)
os.Setenv(XdgConfigHomeEnv, rootedPath("blah"))
s := configRoot()
if isSet {
os.Setenv(XdgConfigHomeEnv, xdg)
}
if s != rootedPath("blah", ProgramName) {
t.Fatalf("unexpected config dir: %s", s)
}
}