Skip to content

Commit

Permalink
Merge pull request #242 from brndnmtthws/master
Browse files Browse the repository at this point in the history
Improve temporary dir handling.
  • Loading branch information
Sami Alajrami authored May 6, 2019
2 parents dad6d64 + 9c18722 commit 51039c5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
4 changes: 4 additions & 0 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ func init() {
}
}

// wipe & create a temporary directory
os.RemoveAll(tempFilesDir)
_ = os.MkdirAll(tempFilesDir, 0755)

// read the TOML/YAML desired state file
var fileState state
for _, f := range files {
Expand Down
10 changes: 5 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,15 @@ var destroy bool
var showDiff bool
var suppressDiffSecrets bool

const tempFilesDir = "helmsman-temp-files"
const tempFilesDir = ".helmsman-tmp"
const stableHelmRepo = "https://kubernetes-charts.storage.googleapis.com"
const incubatorHelmRepo = "http://storage.googleapis.com/kubernetes-charts-incubator"

func main() {
// delete temp files with substituted env vars when the program terminates
defer os.RemoveAll(tempFilesDir)
defer cleanup()

// set the kubecontext to be used Or create it if it does not exist
if !setKubeContext(s.Settings.KubeContext) {
if r, msg := createContext(); !r {
Expand Down Expand Up @@ -111,8 +115,6 @@ func main() {
p.execPlan()
}

cleanup()

log.Println("INFO: completed successfully!")
}

Expand Down Expand Up @@ -166,6 +168,4 @@ func cleanup() {
}
}

// delete temp files with substituted env vars
os.RemoveAll(tempFilesDir)
}
12 changes: 5 additions & 7 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -151,16 +152,13 @@ func substituteEnvInYaml(file string) string {
}
yamlFile := substituteEnv(string(rawYamlFile))

// create a temp directory
if _, err := os.Stat(tempFilesDir); os.IsNotExist(err) {
err = os.MkdirAll(tempFilesDir, 0755)
if err != nil {
logError(err.Error())
}
dir, err := ioutil.TempDir(tempFilesDir, "tmp")
if err != nil {
logError(err.Error())
}

// output file contents with env variables substituted into temp files
outFile := tempFilesDir + string(os.PathSeparator) + filepath.Base(file)
outFile := path.Join(dir, filepath.Base(file))
err = ioutil.WriteFile(outFile, []byte(yamlFile), 0644)
if err != nil {
logError(err.Error())
Expand Down

0 comments on commit 51039c5

Please sign in to comment.