Skip to content

Commit

Permalink
fix: XDG_CONFIG_HOME external plugin discovery
Browse files Browse the repository at this point in the history
  • Loading branch information
em-r authored and Sajiyah-Salat committed Jun 11, 2023
1 parent 805e76e commit adf43b5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
45 changes: 25 additions & 20 deletions pkg/cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,26 +171,44 @@ func parseExternalPluginArgs() (args []string) {
return args
}

// isHostSupported checks whether the host system is supported or not.
func isHostSupported(host string) bool {
for _, platform := range supportedPlatforms {
if host == platform {
return true
}
}
return false
}

// getPluginsRoot detects the host system and gets the plugins root based on the host.
func getPluginsRoot(host string) (pluginsRoot string, err error) {
if !isHostSupported(host) {
// freebsd, openbsd, windows...
return "", fmt.Errorf("host not supported: %v", host)
}

pluginsRelativePath := filepath.Join("kubebuilder", "plugins")
if xdgHome := os.Getenv("XDG_CONFIG_HOME"); xdgHome != "" {
return filepath.Join(xdgHome, pluginsRelativePath), nil
}

switch host {
case "darwin":
logrus.Debugf("Detected host is macOS.")
pluginsRoot = filepath.Join("Library", "Application Support", "kubebuilder", "plugins")
pluginsRoot = filepath.Join("Library", "Application Support", pluginsRelativePath)
case "linux":
logrus.Debugf("Detected host is Linux.")
pluginsRoot = filepath.Join(".config", "kubebuilder", "plugins")
default:
// freebsd, openbsd, windows...
return "", fmt.Errorf("Host not supported: %v", host)
pluginsRoot = filepath.Join(".config", pluginsRelativePath)
}
userHomeDir, err := getHomeDir()

userHomeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("error retrieving home dir: %v", err)
}
pluginsRoot = filepath.Join(userHomeDir, pluginsRoot)

return pluginsRoot, nil
return
}

// DiscoverExternalPlugins discovers the external plugins in the plugins root directory
Expand Down Expand Up @@ -286,16 +304,3 @@ func DiscoverExternalPlugins(fs afero.Fs) (ps []plugin.Plugin, err error) {
func isPluginExectuable(mode fs.FileMode) bool {
return mode&0111 != 0
}

// getHomeDir returns $XDG_CONFIG_HOME if set, otherwise $HOME.
func getHomeDir() (string, error) {
var err error
xdgHome := os.Getenv("XDG_CONFIG_HOME")
if xdgHome == "" {
xdgHome, err = os.UserHomeDir()
if err != nil {
return "", err
}
}
return xdgHome, nil
}
14 changes: 12 additions & 2 deletions pkg/cli/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ var _ = Describe("Discover external plugins", func() {

_, err = getPluginsRoot("random")
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring("Host not supported"))
Expect(err.Error()).To(ContainSubstring("host not supported"))
})

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

_, err = getPluginsRoot("random")
Expect(err).ToNot(BeNil())
Expect(err.Error()).To(ContainSubstring("Host not supported"))
Expect(err.Error()).To(ContainSubstring("host not supported"))
})

It("should return full path to the external plugins", func() {
err = os.Setenv("XDG_CONFIG_HOME", "/some/random/path")
Expect(err).To(BeNil())

pluginsRoot, err := getPluginsRoot(runtime.GOOS)
Expect(err).To(BeNil())
Expect(pluginsRoot).To(Equal("/some/random/path/kubebuilder/plugins"))

})

It("should return error when home directory is set to empty", func() {
Expand Down
4 changes: 4 additions & 0 deletions pkg/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ const (
projectVersionsHeader = "Supported project versions"
)

var (
supportedPlatforms = []string{"linux", "darwin"}
)

func (c CLI) newRootCmd() *cobra.Command {
cmd := &cobra.Command{
Use: c.commandName,
Expand Down

0 comments on commit adf43b5

Please sign in to comment.