goGetterChart() downloads to user cache dir instead of working dir

This commit is contained in:
Quan TRAN
2022-02-12 17:52:52 +09:00
committed by Yusuke Kuoka
parent 766b03047c
commit cf02442591
3 changed files with 49 additions and 25 deletions
+32 -9
View File
@@ -4,23 +4,37 @@ import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/hashicorp/go-getter"
"github.com/hashicorp/go-getter/helper/url"
"go.uber.org/multierr"
"go.uber.org/zap"
"gopkg.in/yaml.v2"
"os"
"path/filepath"
"strings"
)
const DefaultCacheDir = ".helmfile/cache"
const defaultCacheDir = "helmfile"
func cacheDir() string {
dir, err := os.UserCacheDir()
if err != nil {
// fall back to relative path with hidden directory
return relativeCacheDir()
}
return filepath.Join(dir, defaultCacheDir)
}
// TODO remove this function when rework on caching of remote helmfiles
func relativeCacheDir() string {
return fmt.Sprintf(".%s", defaultCacheDir)
}
type Remote struct {
Logger *zap.SugaredLogger
// Home is the home directory for helmfile. Usually this points to $HOME of the user running helmfile.
// Helmfile saves fetched remote files into .helmfile/cache under home
// Home is the directory in which remote downloads files. If empty, user cache directory is used
Home string
// Getter is the underlying implementation of getter used for fetching remote files
@@ -166,7 +180,7 @@ func (r *Remote) Fetch(goGetterSrc string, cacheDirOpt ...string) (string, error
r.Logger.Debugf("file: %s", u.File)
// This should be shared across variant commands, so that they can share cache for the shared imports
cacheBaseDir := DefaultCacheDir
cacheBaseDir := ""
if len(cacheDirOpt) == 1 {
cacheBaseDir = cacheDirOpt[0]
} else if len(cacheDirOpt) > 0 {
@@ -187,10 +201,10 @@ func (r *Remote) Fetch(goGetterSrc string, cacheDirOpt ...string) (string, error
cached := false
// e.g. .helmfile/cache/https_github_com_cloudposse_helmfiles_git.ref=0.xx.0
// e.g. https_github_com_cloudposse_helmfiles_git.ref=0.xx.0
getterDst := filepath.Join(cacheBaseDir, cacheKey)
// e.g. $PWD/.helmfile/cache/https_github_com_cloudposse_helmfiles_git.ref=0.xx.0
// e.g. os.cacheDir()/helmfile/https_github_com_cloudposse_helmfiles_git.ref=0.xx.0
cacheDirPath := filepath.Join(r.Home, getterDst)
r.Logger.Debugf("home: %s", r.Home)
@@ -275,5 +289,14 @@ func NewRemote(logger *zap.SugaredLogger, homeDir string, readFile func(string)
DirExists: dirExists,
FileExists: fileExists,
}
if remote.Home == "" {
// Use for remote charts
remote.Home = cacheDir()
} else {
// Use for remote helmfiles, this case Home is relative to the processing file
remote.Home = filepath.Join(remote.Home, relativeCacheDir())
}
return remote
}