add subcommands `cache info` `cache cleanup`

Signed-off-by: Quan TRAN <account@itscaro.me>
This commit is contained in:
Quan TRAN 2022-02-11 22:52:38 +01:00 committed by Yusuke Kuoka
parent cf02442591
commit 242e601898
4 changed files with 69 additions and 13 deletions

21
main.go
View File

@ -690,6 +690,27 @@ func main() {
return a.ListReleases(c)
}),
},
{
Name: "cache",
Usage: "cache management",
ArgsUsage: "[command]",
Subcommands: []cli.Command{
{
Name: "info",
Usage: "cache info",
Action: action(func(a *app.App, c configImpl) error {
return a.ShowCacheDir(c)
}),
},
{
Name: "cleanup",
Usage: "clean up cache directory",
Action: action(func(a *app.App, c configImpl) error {
return a.CleanCacheDir(c)
}),
},
},
},
{
Name: "version",
Usage: "Show the version for Helmfile.",

View File

@ -2078,3 +2078,38 @@ func (c context) wrapErrs(errs ...error) error {
}
return nil
}
func (a *App) ShowCacheDir(c ListConfigProvider) error {
fmt.Printf("Cache directory: %s\n", remote.CacheDir())
if !directoryExistsAt(remote.CacheDir()) {
return nil
}
dirs, err := os.ReadDir(remote.CacheDir())
if err != nil {
return err
}
for _, e := range dirs {
fmt.Printf("- %s\n", e.Name())
}
return nil
}
func (a *App) CleanCacheDir(c ListConfigProvider) error {
if !directoryExistsAt(remote.CacheDir()) {
fmt.Printf("Nothing to remove in cache directory: %s\n", remote.CacheDir())
return nil
}
fmt.Printf("Cleaning up cache directory: %s\n", remote.CacheDir())
dirs, err := os.ReadDir(remote.CacheDir())
if err != nil {
return err
}
for _, e := range dirs {
fmt.Printf("- %s\n", e.Name())
os.RemoveAll(filepath.Join(remote.CacheDir(), e.Name()))
}
return nil
}

View File

@ -17,7 +17,7 @@ import (
const defaultCacheDir = "helmfile"
func cacheDir() string {
func CacheDir() string {
dir, err := os.UserCacheDir()
if err != nil {
// fall back to relative path with hidden directory
@ -204,7 +204,7 @@ func (r *Remote) Fetch(goGetterSrc string, cacheDirOpt ...string) (string, error
// e.g. https_github_com_cloudposse_helmfiles_git.ref=0.xx.0
getterDst := filepath.Join(cacheBaseDir, cacheKey)
// e.g. os.cacheDir()/helmfile/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)
@ -292,7 +292,7 @@ func NewRemote(logger *zap.SugaredLogger, homeDir string, readFile func(string)
if remote.Home == "" {
// Use for remote charts
remote.Home = cacheDir()
remote.Home = CacheDir()
} else {
// Use for remote helmfiles, this case Home is relative to the processing file
remote.Home = filepath.Join(remote.Home, relativeCacheDir())

View File

@ -13,10 +13,10 @@ import (
func TestRemote_HttpsGitHub(t *testing.T) {
cleanfs := map[string]string{
cacheDir(): "",
CacheDir(): "",
}
cachefs := map[string]string{
filepath.Join(cacheDir(), "https_github_com_cloudposse_helmfiles_git.ref=0.40.0/releases/kiam.yaml"): "foo: bar",
filepath.Join(CacheDir(), "https_github_com_cloudposse_helmfiles_git.ref=0.40.0/releases/kiam.yaml"): "foo: bar",
}
type testcase struct {
@ -38,7 +38,7 @@ func TestRemote_HttpsGitHub(t *testing.T) {
hit := true
get := func(wd, src, dst string) error {
if wd != cacheDir() {
if wd != CacheDir() {
return fmt.Errorf("unexpected wd: %s", wd)
}
if src != "git::https://github.com/cloudposse/helmfiles.git?ref=0.40.0" {
@ -55,7 +55,7 @@ func TestRemote_HttpsGitHub(t *testing.T) {
}
remote := &Remote{
Logger: helmexec.NewLogger(os.Stderr, "debug"),
Home: cacheDir(),
Home: CacheDir(),
Getter: getter,
ReadFile: testfs.ReadFile,
FileExists: testfs.FileExistsAt,
@ -74,7 +74,7 @@ func TestRemote_HttpsGitHub(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
expectedFile := filepath.Join(cacheDir(), "https_github_com_cloudposse_helmfiles_git.ref=0.40.0/releases/kiam.yaml")
expectedFile := filepath.Join(CacheDir(), "https_github_com_cloudposse_helmfiles_git.ref=0.40.0/releases/kiam.yaml")
if file != expectedFile {
t.Errorf("unexpected file located: %s vs expected: %s", file, expectedFile)
}
@ -91,10 +91,10 @@ func TestRemote_HttpsGitHub(t *testing.T) {
func TestRemote_SShGitHub(t *testing.T) {
cleanfs := map[string]string{
cacheDir(): "",
CacheDir(): "",
}
cachefs := map[string]string{
filepath.Join(cacheDir(), "ssh_github_com_cloudposse_helmfiles_git.ref=0.40.0/releases/kiam.yaml"): "foo: bar",
filepath.Join(CacheDir(), "ssh_github_com_cloudposse_helmfiles_git.ref=0.40.0/releases/kiam.yaml"): "foo: bar",
}
type testcase struct {
@ -116,7 +116,7 @@ func TestRemote_SShGitHub(t *testing.T) {
hit := true
get := func(wd, src, dst string) error {
if wd != cacheDir() {
if wd != CacheDir() {
return fmt.Errorf("unexpected wd: %s", wd)
}
if src != "git::ssh://git@github.com/cloudposse/helmfiles.git?ref=0.40.0" {
@ -133,7 +133,7 @@ func TestRemote_SShGitHub(t *testing.T) {
}
remote := &Remote{
Logger: helmexec.NewLogger(os.Stderr, "debug"),
Home: cacheDir(),
Home: CacheDir(),
Getter: getter,
ReadFile: testfs.ReadFile,
FileExists: testfs.FileExistsAt,
@ -146,7 +146,7 @@ func TestRemote_SShGitHub(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
expectedFile := filepath.Join(cacheDir(), "ssh_github_com_cloudposse_helmfiles_git.ref=0.40.0/releases/kiam.yaml")
expectedFile := filepath.Join(CacheDir(), "ssh_github_com_cloudposse_helmfiles_git.ref=0.40.0/releases/kiam.yaml")
if file != expectedFile {
t.Errorf("unexpected file located: %s vs expected: %s", file, expectedFile)
}