From 60dbc8b12fd284f7402efd3ed781089be9955687 Mon Sep 17 00:00:00 2001 From: Mark DeCrane Date: Tue, 1 Jul 2025 15:37:42 -0400 Subject: [PATCH] Move stacksplugin cache to .terraform.d, allow for overriding of cache dir location --- internal/command/stacks.go | 60 ++++++++++++++++++++++++--- internal/pluginshared/stacksbinary.go | 2 + 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/internal/command/stacks.go b/internal/command/stacks.go index 253585c14e..ece3230606 100644 --- a/internal/command/stacks.go +++ b/internal/command/stacks.go @@ -76,7 +76,24 @@ var ( ) func (c *StacksCommand) realRun(args []string, stdout, stderr io.Writer) int { + var pluginCacheDirOverride string + args = c.Meta.process(args) + cmdFlags := c.Meta.defaultFlagSet("stacks") + cmdFlags.StringVar(&pluginCacheDirOverride, "plugin-cache-dir", "", "plugin cache directory") + cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } + if err := cmdFlags.Parse(args); err != nil { + c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error())) + return 1 + } + + if pluginCacheDirOverride != "" { + err := c.storeStacksPluginPath(path.Join(pluginCacheDirOverride, StacksPluginDataDir)) + if err != nil { + c.Ui.Error(fmt.Sprintf("Error storing cached stacks plugin path: %s\n", err)) + return 1 + } + } diags := c.initPlugin() if diags.HasWarnings() || diags.HasErrors() { @@ -305,19 +322,50 @@ func (c *StacksCommand) initPlugin() tfdiags.Diagnostics { } func (c *StacksCommand) initPackagesCache() (string, error) { - packagesPath := path.Join(c.WorkingDir.DataDir(), StacksPluginDataDir) + var pluginPath string + defaultPluginPath := path.Join(c.CLIConfigDir, StacksPluginDataDir) + cacheStorePath := path.Join(c.WorkingDir.DataDir(), ".stackspluginpath") + + if _, err := os.Stat(cacheStorePath); err != nil { + if os.IsNotExist(err) { + log.Printf("[TRACE] No stacksplugin cache path store at '%s`, using default '%s'", cacheStorePath, defaultPluginPath) + // Use the default plugin cache path if the file does not exist + pluginPath = defaultPluginPath + } else { + log.Printf("[TRACE] Failed to check the stacksplugin cache path store at '%s', using default '%s'", cacheStorePath, defaultPluginPath) + // Use the default plugin cache path if there was an error checking the file + pluginPath = defaultPluginPath + } + } else { + data, err := os.ReadFile(cacheStorePath) + if err != nil { + return "", fmt.Errorf("failed to read stacks plugin stored path file: %w", err) + } + pluginPath = string(data) + } - if info, err := os.Stat(packagesPath); err != nil || !info.IsDir() { - log.Printf("[TRACE] initialized stacksplugin cache directory at %q", packagesPath) - err = os.MkdirAll(packagesPath, 0755) + if info, err := os.Stat(pluginPath); err != nil || !info.IsDir() { + log.Printf("[TRACE] initialized stacksplugin cache directory at %q", pluginPath) + err = os.MkdirAll(pluginPath, 0755) if err != nil { return "", fmt.Errorf("failed to initialize stacksplugin cache directory: %w", err) } } else { - log.Printf("[TRACE] stacksplugin cache directory found at %q", packagesPath) + log.Printf("[TRACE] stacksplugin cache directory found at %q", pluginPath) + } + + return pluginPath, nil +} + +func (c *StacksCommand) storeStacksPluginPath(pluginCachePath string) error { + f, err := os.Create(path.Join(c.WorkingDir.DataDir(), ".stackspluginpath")) + if err != nil { + return fmt.Errorf("failed to create stacks plugin stored path file: %w", err) } + defer f.Close() + f.WriteString(pluginCachePath) - return packagesPath, nil + return nil } // Run runs the stacks command with the given arguments. diff --git a/internal/pluginshared/stacksbinary.go b/internal/pluginshared/stacksbinary.go index 87a30e0d10..4e2452d2a6 100644 --- a/internal/pluginshared/stacksbinary.go +++ b/internal/pluginshared/stacksbinary.go @@ -26,6 +26,8 @@ func NewStacksBinaryManager(ctx context.Context, stacksPluginDataDir, overridePa return nil, fmt.Errorf("could not initialize stacksplugin version manager: %w", err) } + // read from the data dir to find the cached stacksplugin binary location + return &StacksBinaryManager{ BinaryManager{ pluginDataDir: stacksPluginDataDir,