From 53ee477e20648c98d56dd3d6c92a87a7ddae1b3e Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Sat, 23 Aug 2025 03:07:04 +0000 Subject: [PATCH] Implement temp file cleanup for chartify and helmfile temp files Co-authored-by: yxxhero <11087727+yxxhero@users.noreply.github.com> --- cmd/root.go | 9 +++++++++ pkg/app/app.go | 34 ++++++++++++++++++++++++++++++++++ pkg/config/global.go | 2 ++ pkg/state/helmx.go | 12 ++++++++++-- pkg/state/state.go | 4 ++++ 5 files changed, 59 insertions(+), 2 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index ed629d12..ad14ff2e 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -68,6 +68,14 @@ func NewRootCmd(globalConfig *config.GlobalOptions) (*cobra.Command, error) { } logger = helmexec.NewLogger(logOut, logLevel) globalConfig.SetLogger(logger) + + // Clean up old temporary files if enabled + if globalConfig.CleanupTempFiles { + if err := app.CleanupTempFiles(logger); err != nil { + logger.Warnf("Failed to cleanup temporary files: %v", err) + } + } + return nil }, } @@ -141,6 +149,7 @@ The name of a release can be used as a label: "--selector name=myrelease"`) fs.BoolVar(&globalOptions.EnableLiveOutput, "enable-live-output", globalOptions.EnableLiveOutput, `Show live output from the Helm binary Stdout/Stderr into Helmfile own Stdout/Stderr. It only applies for the Helm CLI commands, Stdout/Stderr for Hooks are still displayed only when it's execution finishes.`) fs.BoolVarP(&globalOptions.Interactive, "interactive", "i", false, "Request confirmation before attempting to modify clusters") + fs.BoolVar(&globalOptions.CleanupTempFiles, "cleanup-temp-files", true, "Clean up temporary files and directories from previous runs on startup") // avoid 'pflag: help requested' error (#251) fs.BoolP("help", "h", false, "help for helmfile") } diff --git a/pkg/app/app.go b/pkg/app/app.go index 19024459..8576f12a 100644 --- a/pkg/app/app.go +++ b/pkg/app/app.go @@ -2270,3 +2270,37 @@ func GetArgs(args string, state *state.HelmState) []string { return state.HelmDefaults.Args } + +// CleanupTempFiles removes leftover temporary files and directories from previous runs +func CleanupTempFiles(logger *zap.SugaredLogger) error { + if logger == nil { + return nil + } + + tmpDir := os.TempDir() + entries, err := os.ReadDir(tmpDir) + if err != nil { + return fmt.Errorf("reading temp directory %s: %v", tmpDir, err) + } + + var cleanupCount int + for _, entry := range entries { + name := entry.Name() + // Clean up old helmfile and chartify temporary files/directories + if strings.HasPrefix(name, "helmfile") || strings.HasPrefix(name, "chartify") { + fullPath := filepath.Join(tmpDir, name) + if err := os.RemoveAll(fullPath); err != nil { + logger.Warnf("Failed to remove temporary file/directory %s: %v", fullPath, err) + } else { + logger.Debugf("Cleaned up temporary file/directory: %s", fullPath) + cleanupCount++ + } + } + } + + if cleanupCount > 0 { + logger.Debugf("Cleaned up %d temporary files/directories", cleanupCount) + } + + return nil +} diff --git a/pkg/config/global.go b/pkg/config/global.go index fe906195..753f1537 100644 --- a/pkg/config/global.go +++ b/pkg/config/global.go @@ -70,6 +70,8 @@ type GlobalOptions struct { Args string // LogOutput is the writer to use for writing logs. LogOutput io.Writer + // CleanupTempFiles enables cleanup of old temporary files on startup. + CleanupTempFiles bool } // Logger returns the logger to use. diff --git a/pkg/state/helmx.go b/pkg/state/helmx.go index eca4b2cb..4482a399 100644 --- a/pkg/state/helmx.go +++ b/pkg/state/helmx.go @@ -249,8 +249,9 @@ func (st *HelmState) appendShowOnlyFlags(flags []string, showOnly []string) []st } type Chartify struct { - Opts *chartify.ChartifyOpts - Clean func() + Opts *chartify.ChartifyOpts + Clean func() + AddToCleanup func(string) // Add function to track additional files/dirs for cleanup } func (st *HelmState) downloadChartWithGoGetter(r *ReleaseSpec) (string, error) { @@ -312,6 +313,13 @@ func (st *HelmState) PrepareChartify(helm helmexec.Interface, release *ReleaseSp st.removeFiles(filesNeedCleaning) } + // Add function to track additional files/directories for cleanup + c.AddToCleanup = func(path string) { + filesNeedCleaning = append(filesNeedCleaning, path) + } + + c.Clean = clean + var shouldRun bool dir := chart diff --git a/pkg/state/state.go b/pkg/state/state.go index 60289117..40ef885a 100644 --- a/pkg/state/state.go +++ b/pkg/state/state.go @@ -1342,6 +1342,10 @@ func (st *HelmState) PrepareCharts(helm helmexec.Interface, dir string, concurre return } else { chartPath = out + // Track the chartify output directory for cleanup + if chartification != nil && chartification.AddToCleanup != nil { + chartification.AddToCleanup(out) + } } // Skip `helm dep build` and `helm dep up` altogether when the chart is from remote or the dep is