From a44c25c2f1c059fda8b1b4e3c678a61481e69794 Mon Sep 17 00:00:00 2001
From: Sebastiaan van Stijn <github@gone.nl>
Date: Tue, 18 Jul 2023 11:47:40 +0200
Subject: [PATCH] pkg/plugins: split exported from implementation

Split the exported SpecsPaths from the platform-specific implementations,
so that documentation can be maintained in a single location.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
---
 pkg/plugins/discovery.go         | 21 ++++++++++++++++++++-
 pkg/plugins/discovery_unix.go    | 17 ++++-------------
 pkg/plugins/discovery_windows.go |  8 ++------
 3 files changed, 26 insertions(+), 20 deletions(-)

diff --git a/pkg/plugins/discovery.go b/pkg/plugins/discovery.go
index 609459bd13..76eced31d8 100644
--- a/pkg/plugins/discovery.go
+++ b/pkg/plugins/discovery.go
@@ -26,7 +26,7 @@ type LocalRegistry struct {
 
 func NewLocalRegistry() LocalRegistry {
 	return LocalRegistry{
-		SpecsPaths,
+		SpecsPaths: specsPaths,
 	}
 }
 
@@ -111,6 +111,25 @@ func (l *LocalRegistry) Plugin(name string) (*Plugin, error) {
 	return nil, errors.Wrapf(ErrNotFound, "could not find plugin %s in v1 plugin registry", name)
 }
 
+// SpecsPaths returns paths in which to look for plugins, in order of priority.
+//
+// On Windows:
+//
+//   - "%programdata%\docker\plugins"
+//
+// On Unix in non-rootless mode:
+//
+//   - "/etc/docker/plugins"
+//   - "/usr/lib/docker/plugins"
+//
+// On Unix in rootless-mode:
+//
+//   - "$XDG_CONFIG_HOME/docker/plugins" (or "/etc/docker/plugins" if $XDG_CONFIG_HOME is not set)
+//   - "$HOME/.local/lib/docker/plugins" (pr "/usr/lib/docker/plugins" if $HOME is set)
+func SpecsPaths() []string {
+	return specsPaths()
+}
+
 func readPluginInfo(name, path string) (*Plugin, error) {
 	content, err := os.ReadFile(path)
 	if err != nil {
diff --git a/pkg/plugins/discovery_unix.go b/pkg/plugins/discovery_unix.go
index 8caa8053fa..1a05307b74 100644
--- a/pkg/plugins/discovery_unix.go
+++ b/pkg/plugins/discovery_unix.go
@@ -9,32 +9,23 @@ import (
 )
 
 func rootlessConfigPluginsPath() string {
-	configHome, err := homedir.GetConfigHome()
-	if err == nil {
+	if configHome, err := homedir.GetConfigHome(); err != nil {
 		return filepath.Join(configHome, "docker/plugins")
 	}
-
 	return "/etc/docker/plugins"
 }
 
 func rootlessLibPluginsPath() string {
-	libHome, err := homedir.GetLibHome()
-	if err == nil {
+	if libHome, err := homedir.GetLibHome(); err == nil {
 		return filepath.Join(libHome, "docker/plugins")
 	}
-
 	return "/usr/lib/docker/plugins"
 }
 
-// SpecsPaths returns
-// { "%programdata%\docker\plugins" } on Windows,
-// { "/etc/docker/plugins", "/usr/lib/docker/plugins" } on Unix in non-rootless mode,
-// { "$XDG_CONFIG_HOME/docker/plugins", "$HOME/.local/lib/docker/plugins" } on Unix in rootless mode
-// with fallback to the corresponding path in non-rootless mode if $XDG_CONFIG_HOME or $HOME is not set.
-func SpecsPaths() []string {
+// specsPaths is the non-Windows implementation of [SpecsPaths].
+func specsPaths() []string {
 	if rootless.RunningWithRootlessKit() {
 		return []string{rootlessConfigPluginsPath(), rootlessLibPluginsPath()}
 	}
-
 	return []string{"/etc/docker/plugins", "/usr/lib/docker/plugins"}
 }
diff --git a/pkg/plugins/discovery_windows.go b/pkg/plugins/discovery_windows.go
index ea5d4be375..fe825792ba 100644
--- a/pkg/plugins/discovery_windows.go
+++ b/pkg/plugins/discovery_windows.go
@@ -5,11 +5,7 @@ import (
 	"path/filepath"
 )
 
-// SpecsPaths returns
-// { "%programdata%\docker\plugins" } on Windows,
-// { "/etc/docker/plugins", "/usr/lib/docker/plugins" } on Unix in non-rootless mode,
-// { "$XDG_CONFIG_HOME/docker/plugins", "$HOME/.local/lib/docker/plugins" } on Unix in rootless mode
-// with fallback to the corresponding path in non-rootless mode if $XDG_CONFIG_HOME or $HOME is not set.
-func SpecsPaths() []string {
+// specsPaths is the Windows implementation of [SpecsPaths].
+func specsPaths() []string {
 	return []string{filepath.Join(os.Getenv("programdata"), "docker", "plugins")}
 }