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

@@ -1,55 +0,0 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package commands holds the CLI glue mapping textual commands/args to method calls.
package pgmconfig
import (
"os"
"path/filepath"
"runtime"
)
//noinspection GoSnakeCaseUsage
const (
XDG_CONFIG_HOME = "XDG_CONFIG_HOME"
defaultConfigSubdir = ".config"
)
// Use https://github.com/kirsle/configdir instead?
func ConfigRoot() string {
dir := os.Getenv(XDG_CONFIG_HOME)
if len(dir) == 0 {
dir = filepath.Join(
HomeDir(), defaultConfigSubdir)
}
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

@@ -1,56 +0,0 @@
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package pgmconfig
import (
"os"
"path/filepath"
"strings"
"testing"
)
func TestConfigDirNoXdg(t *testing.T) {
xdg, isSet := os.LookupEnv(XDG_CONFIG_HOME)
if isSet {
os.Unsetenv(XDG_CONFIG_HOME)
}
s := ConfigRoot()
if isSet {
os.Setenv(XDG_CONFIG_HOME, xdg)
}
if !strings.HasSuffix(
s,
rootedPath(defaultConfigSubdir, 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(XDG_CONFIG_HOME)
os.Setenv(XDG_CONFIG_HOME, rootedPath("blah"))
s := ConfigRoot()
if isSet {
os.Setenv(XDG_CONFIG_HOME, xdg)
}
if s != rootedPath("blah", ProgramName) {
t.Fatalf("unexpected config dir: %s", s)
}
}

View File

@@ -19,10 +19,26 @@ const (
KustomizationFileName0 = "kustomization.yaml"
KustomizationFileName1 = "kustomization.yml"
KustomizationFileName2 = "Kustomization"
// An environment variable to consult for kustomization
// configuration data. See:
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
XdgConfigHome = "XDG_CONFIG_HOME"
// Use this when XdgConfigHome not defined.
DefaultConfigSubdir = ".config"
// Program name, for help, finding the XDG_CONFIG_DIR, etc.
ProgramName = "kustomize"
// Domain from which kustomize code is imported, for locating
// plugin source code under $GOPATH.
// TODO: move to pgk/plugin/config.go or equivalent
// as part of v4 release. Cannot move till then
// because of pluginator dependence at v3.
DomainName = "sigs.k8s.io"
// Name of directory housing all plugins.
// TODO: move to pgk/plugin/config.go or equivalent
PluginRoot = "plugin"
)