Skip to content

Commit 2e0a0fb

Browse files
committed
fix: XDG_CONFIG_HOME external plugin discovery
1 parent 140883c commit 2e0a0fb

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

pkg/cli/options.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -171,26 +171,44 @@ func parseExternalPluginArgs() (args []string) {
171171
return args
172172
}
173173

174+
// isHostSupported checks whether the host system is supported or not.
175+
func isHostSupported(host string) bool {
176+
for _, platform := range supportedPlatforms {
177+
if host == platform {
178+
return true
179+
}
180+
}
181+
return false
182+
}
183+
174184
// getPluginsRoot detects the host system and gets the plugins root based on the host.
175185
func getPluginsRoot(host string) (pluginsRoot string, err error) {
186+
if !isHostSupported(host) {
187+
// freebsd, openbsd, windows...
188+
return "", fmt.Errorf("host not supported: %v", host)
189+
}
190+
191+
pluginsRelativePath := filepath.Join("kubebuilder", "plugins")
192+
if xdgHome := os.Getenv("XDG_CONFIG_HOME"); xdgHome != "" {
193+
return filepath.Join(xdgHome, pluginsRelativePath), nil
194+
}
195+
176196
switch host {
177197
case "darwin":
178198
logrus.Debugf("Detected host is macOS.")
179-
pluginsRoot = filepath.Join("Library", "Application Support", "kubebuilder", "plugins")
199+
pluginsRoot = filepath.Join("Library", "Application Support", pluginsRelativePath)
180200
case "linux":
181201
logrus.Debugf("Detected host is Linux.")
182-
pluginsRoot = filepath.Join(".config", "kubebuilder", "plugins")
183-
default:
184-
// freebsd, openbsd, windows...
185-
return "", fmt.Errorf("Host not supported: %v", host)
202+
pluginsRoot = filepath.Join(".config", pluginsRelativePath)
186203
}
187-
userHomeDir, err := getHomeDir()
204+
205+
userHomeDir, err := os.UserHomeDir()
188206
if err != nil {
189207
return "", fmt.Errorf("error retrieving home dir: %v", err)
190208
}
191209
pluginsRoot = filepath.Join(userHomeDir, pluginsRoot)
192210

193-
return pluginsRoot, nil
211+
return
194212
}
195213

196214
// DiscoverExternalPlugins discovers the external plugins in the plugins root directory
@@ -286,16 +304,3 @@ func DiscoverExternalPlugins(fs afero.Fs) (ps []plugin.Plugin, err error) {
286304
func isPluginExectuable(mode fs.FileMode) bool {
287305
return mode&0111 != 0
288306
}
289-
290-
// getHomeDir returns $XDG_CONFIG_HOME if set, otherwise $HOME.
291-
func getHomeDir() (string, error) {
292-
var err error
293-
xdgHome := os.Getenv("XDG_CONFIG_HOME")
294-
if xdgHome == "" {
295-
xdgHome, err = os.UserHomeDir()
296-
if err != nil {
297-
return "", err
298-
}
299-
}
300-
return xdgHome, nil
301-
}

pkg/cli/options_test.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ var _ = Describe("Discover external plugins", func() {
228228

229229
_, err = getPluginsRoot("random")
230230
Expect(err).ToNot(BeNil())
231-
Expect(err.Error()).To(ContainSubstring("Host not supported"))
231+
Expect(err.Error()).To(ContainSubstring("host not supported"))
232232
})
233233

234234
It("should skip parsing of directories if plugins root is not a directory", func() {
@@ -249,7 +249,17 @@ var _ = Describe("Discover external plugins", func() {
249249

250250
_, err = getPluginsRoot("random")
251251
Expect(err).ToNot(BeNil())
252-
Expect(err.Error()).To(ContainSubstring("Host not supported"))
252+
Expect(err.Error()).To(ContainSubstring("host not supported"))
253+
})
254+
255+
It("should return full path to the external plugins", func() {
256+
err = os.Setenv("XDG_CONFIG_HOME", "/some/random/path")
257+
Expect(err).To(BeNil())
258+
259+
pluginsRoot, err := getPluginsRoot(runtime.GOOS)
260+
Expect(err).To(BeNil())
261+
Expect(pluginsRoot).To(Equal("/some/random/path/kubebuilder/plugins"))
262+
253263
})
254264

255265
It("should return error when home directory is set to empty", func() {

pkg/cli/root.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ const (
2929
projectVersionsHeader = "Supported project versions"
3030
)
3131

32+
var (
33+
supportedPlatforms = []string{"linux", "darwin"}
34+
)
35+
3236
func (c CLI) newRootCmd() *cobra.Command {
3337
cmd := &cobra.Command{
3438
Use: c.commandName,

0 commit comments

Comments
 (0)