...
1
2
3
4 package konfig
5
6 import (
7 "os"
8 "path/filepath"
9 "runtime"
10
11 "sigs.k8s.io/kustomize/api/types"
12 "sigs.k8s.io/kustomize/kyaml/filesys"
13 )
14
15 const (
16
17 PluginSymbol = "KustomizePlugin"
18
19
20
21 KustomizePluginHomeEnv = "KUSTOMIZE_PLUGIN_HOME"
22
23
24
25 RelPluginHome = "plugin"
26
27
28 BuiltinPluginPackage = "builtin"
29
30
31
32
33 BuiltinPluginApiVersion = BuiltinPluginPackage
34
35
36
37 DomainName = "sigs.k8s.io"
38
39
40
41 NoPluginHomeSentinal = "/No/non-builtin/plugins!"
42 )
43
44 type NotedFunc struct {
45 Note string
46 F func() string
47 }
48
49
50
51
52 func DefaultAbsPluginHome(fSys filesys.FileSystem) (string, error) {
53 return FirstDirThatExistsElseError(
54 "plugin root", fSys, []NotedFunc{
55 {
56 Note: "homed in $" + KustomizePluginHomeEnv,
57 F: func() string {
58 return os.Getenv(KustomizePluginHomeEnv)
59 },
60 },
61 {
62 Note: "homed in $" + XdgConfigHomeEnv,
63 F: func() string {
64 if root := os.Getenv(XdgConfigHomeEnv); root != "" {
65 return filepath.Join(root, ProgramName, RelPluginHome)
66 }
67
68 return ""
69 },
70 },
71 {
72 Note: "homed in default value of $" + XdgConfigHomeEnv,
73 F: func() string {
74 return filepath.Join(
75 HomeDir(), XdgConfigHomeEnvDefault,
76 ProgramName, RelPluginHome)
77 },
78 },
79 {
80 Note: "homed in home directory",
81 F: func() string {
82 return filepath.Join(
83 HomeDir(), ProgramName, RelPluginHome)
84 },
85 },
86 })
87 }
88
89
90
91 func FirstDirThatExistsElseError(
92 what string,
93 fSys filesys.FileSystem,
94 pathFuncs []NotedFunc) (string, error) {
95 var nope []types.Pair
96 for _, dt := range pathFuncs {
97 if dir := dt.F(); dir != "" {
98 if fSys.Exists(dir) {
99 return dir, nil
100 }
101 nope = append(nope, types.Pair{Key: dt.Note, Value: dir})
102 } else {
103 nope = append(nope, types.Pair{Key: dt.Note, Value: "<no value>"})
104 }
105 }
106 return "", types.NewErrUnableToFind(what, nope)
107 }
108
109 func HomeDir() string {
110 home := os.Getenv(homeEnv())
111 if len(home) > 0 {
112 return home
113 }
114 return "~"
115 }
116
117 func homeEnv() string {
118 if runtime.GOOS == "windows" {
119 return "USERPROFILE"
120 }
121 return "HOME"
122 }
123
124 func CurrentWorkingDir() string {
125
126 pwd := os.Getenv(pwdEnv())
127 if len(pwd) > 0 {
128 return pwd
129 }
130 return filesys.SelfDir
131 }
132
133 func pwdEnv() string {
134 if runtime.GOOS == "windows" {
135 return "CD"
136 }
137 return "PWD"
138 }
139
View as plain text