Skip to content

Commit

Permalink
Add temp dir cleanup on startup
Browse files Browse the repository at this point in the history
Adding a reliable way of cleaning up the contents of temp dir to make
sure it doesn't pile up terraform binaries and grow in size with every
restart.
  • Loading branch information
DTLP committed Oct 9, 2023
1 parent 7ac7651 commit e9aa1c3
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"fmt"
"io"
"log"
"os"
"os/signal"
"path/filepath"
Expand Down Expand Up @@ -411,6 +412,22 @@ preferences: {}

}

func cleanupTmpDir() {
tmpDir := os.TempDir()

files, err := os.ReadDir(tmpDir)
if err != nil {
log.Fatalf("Error reading directory: %v", err)
}

for _, file := range files {
filePath := fmt.Sprintf("%s/%s", tmpDir, file.Name())
if err := os.RemoveAll(filePath); err != nil {
log.Printf("Error deleting file %s: %v", filePath, err)
}
}
}

func main() {
app := &cli.App{
Name: "terraform-applier",
Expand Down Expand Up @@ -482,6 +499,8 @@ func run(c *cli.Context) {
}
}

cleanupTmpDir()

// Find the requested version of terraform and log the version
// information
execPath, cleanup, err := findTerraformExecPath(ctx, terraformPath, terraformVersion)
Expand Down

0 comments on commit e9aa1c3

Please sign in to comment.