add subcommands `cache info` `cache cleanup`
Signed-off-by: Quan TRAN <account@itscaro.me>
This commit is contained in:
		
							parent
							
								
									cf02442591
								
							
						
					
					
						commit
						242e601898
					
				
							
								
								
									
										21
									
								
								main.go
								
								
								
								
							
							
						
						
									
										21
									
								
								main.go
								
								
								
								
							|  | @ -690,6 +690,27 @@ func main() { | ||||||
| 				return a.ListReleases(c) | 				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", | 			Name:      "version", | ||||||
| 			Usage:     "Show the version for Helmfile.", | 			Usage:     "Show the version for Helmfile.", | ||||||
|  |  | ||||||
|  | @ -2078,3 +2078,38 @@ func (c context) wrapErrs(errs ...error) error { | ||||||
| 	} | 	} | ||||||
| 	return nil | 	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 | ||||||
|  | } | ||||||
|  |  | ||||||
|  | @ -17,7 +17,7 @@ import ( | ||||||
| 
 | 
 | ||||||
| const defaultCacheDir = "helmfile" | const defaultCacheDir = "helmfile" | ||||||
| 
 | 
 | ||||||
| func cacheDir() string { | func CacheDir() string { | ||||||
| 	dir, err := os.UserCacheDir() | 	dir, err := os.UserCacheDir() | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		// fall back to relative path with hidden directory
 | 		// 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
 | 	// e.g. https_github_com_cloudposse_helmfiles_git.ref=0.xx.0
 | ||||||
| 	getterDst := filepath.Join(cacheBaseDir, cacheKey) | 	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) | 	cacheDirPath := filepath.Join(r.Home, getterDst) | ||||||
| 
 | 
 | ||||||
| 	r.Logger.Debugf("home: %s", r.Home) | 	r.Logger.Debugf("home: %s", r.Home) | ||||||
|  | @ -292,7 +292,7 @@ func NewRemote(logger *zap.SugaredLogger, homeDir string, readFile func(string) | ||||||
| 
 | 
 | ||||||
| 	if remote.Home == "" { | 	if remote.Home == "" { | ||||||
| 		// Use for remote charts
 | 		// Use for remote charts
 | ||||||
| 		remote.Home = cacheDir() | 		remote.Home = CacheDir() | ||||||
| 	} else { | 	} else { | ||||||
| 		// Use for remote helmfiles, this case Home is relative to the processing file
 | 		// Use for remote helmfiles, this case Home is relative to the processing file
 | ||||||
| 		remote.Home = filepath.Join(remote.Home, relativeCacheDir()) | 		remote.Home = filepath.Join(remote.Home, relativeCacheDir()) | ||||||
|  |  | ||||||
|  | @ -13,10 +13,10 @@ import ( | ||||||
| 
 | 
 | ||||||
| func TestRemote_HttpsGitHub(t *testing.T) { | func TestRemote_HttpsGitHub(t *testing.T) { | ||||||
| 	cleanfs := map[string]string{ | 	cleanfs := map[string]string{ | ||||||
| 		cacheDir(): "", | 		CacheDir(): "", | ||||||
| 	} | 	} | ||||||
| 	cachefs := map[string]string{ | 	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 { | 	type testcase struct { | ||||||
|  | @ -38,7 +38,7 @@ func TestRemote_HttpsGitHub(t *testing.T) { | ||||||
| 			hit := true | 			hit := true | ||||||
| 
 | 
 | ||||||
| 			get := func(wd, src, dst string) error { | 			get := func(wd, src, dst string) error { | ||||||
| 				if wd != cacheDir() { | 				if wd != CacheDir() { | ||||||
| 					return fmt.Errorf("unexpected wd: %s", wd) | 					return fmt.Errorf("unexpected wd: %s", wd) | ||||||
| 				} | 				} | ||||||
| 				if src != "git::https://github.com/cloudposse/helmfiles.git?ref=0.40.0" { | 				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{ | 			remote := &Remote{ | ||||||
| 				Logger:     helmexec.NewLogger(os.Stderr, "debug"), | 				Logger:     helmexec.NewLogger(os.Stderr, "debug"), | ||||||
| 				Home:       cacheDir(), | 				Home:       CacheDir(), | ||||||
| 				Getter:     getter, | 				Getter:     getter, | ||||||
| 				ReadFile:   testfs.ReadFile, | 				ReadFile:   testfs.ReadFile, | ||||||
| 				FileExists: testfs.FileExistsAt, | 				FileExists: testfs.FileExistsAt, | ||||||
|  | @ -74,7 +74,7 @@ func TestRemote_HttpsGitHub(t *testing.T) { | ||||||
| 				t.Fatalf("unexpected error: %v", err) | 				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 { | 			if file != expectedFile { | ||||||
| 				t.Errorf("unexpected file located: %s vs expected: %s", 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) { | func TestRemote_SShGitHub(t *testing.T) { | ||||||
| 	cleanfs := map[string]string{ | 	cleanfs := map[string]string{ | ||||||
| 		cacheDir(): "", | 		CacheDir(): "", | ||||||
| 	} | 	} | ||||||
| 	cachefs := map[string]string{ | 	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 { | 	type testcase struct { | ||||||
|  | @ -116,7 +116,7 @@ func TestRemote_SShGitHub(t *testing.T) { | ||||||
| 			hit := true | 			hit := true | ||||||
| 
 | 
 | ||||||
| 			get := func(wd, src, dst string) error { | 			get := func(wd, src, dst string) error { | ||||||
| 				if wd != cacheDir() { | 				if wd != CacheDir() { | ||||||
| 					return fmt.Errorf("unexpected wd: %s", wd) | 					return fmt.Errorf("unexpected wd: %s", wd) | ||||||
| 				} | 				} | ||||||
| 				if src != "git::ssh://git@github.com/cloudposse/helmfiles.git?ref=0.40.0" { | 				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{ | 			remote := &Remote{ | ||||||
| 				Logger:     helmexec.NewLogger(os.Stderr, "debug"), | 				Logger:     helmexec.NewLogger(os.Stderr, "debug"), | ||||||
| 				Home:       cacheDir(), | 				Home:       CacheDir(), | ||||||
| 				Getter:     getter, | 				Getter:     getter, | ||||||
| 				ReadFile:   testfs.ReadFile, | 				ReadFile:   testfs.ReadFile, | ||||||
| 				FileExists: testfs.FileExistsAt, | 				FileExists: testfs.FileExistsAt, | ||||||
|  | @ -146,7 +146,7 @@ func TestRemote_SShGitHub(t *testing.T) { | ||||||
| 				t.Fatalf("unexpected error: %v", err) | 				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 { | 			if file != expectedFile { | ||||||
| 				t.Errorf("unexpected file located: %s vs expected: %s", file, expectedFile) | 				t.Errorf("unexpected file located: %s vs expected: %s", file, expectedFile) | ||||||
| 			} | 			} | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue