Skip to content

Commit

Permalink
Ensure directories are created when process-specific env variables ar…
Browse files Browse the repository at this point in the history
…e used (#159)

* Ensure directories are created when process-specific env variables are used
* Add comment to explain the need for extra calls to makedirs

Signed-off-by: Daniel Mikusa <[email protected]>

Co-authored-by: Sambhav Kothari <[email protected]>
  • Loading branch information
Daniel Mikusa and sambhav authored Jul 15, 2022
1 parent 587c59c commit 2e5389a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions internal/environment_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ func (w EnvironmentWriter) Write(path string, environment map[string]string) err

for key, value := range environment {
f := filepath.Join(path, key)

// required to support process-specific environment variables
if err := os.MkdirAll(filepath.Dir(f), 0755); err != nil {
return fmt.Errorf("unable to mkdir from key %s\n%w", filepath.Dir(f), err)
}

// #nosec
if err := ioutil.WriteFile(f, []byte(value), 0644); err != nil {
return fmt.Errorf("unable to write file %s\n%w", f, err)
Expand Down
11 changes: 11 additions & 0 deletions internal/environment_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,17 @@ func testEnvironmentWriter(t *testing.T, context spec.G, it spec.S) {
Expect(string(content)).To(Equal("other-content"))
})

it("writes the given environment with process specific envs to a directory", func() {
err := writer.Write(path, map[string]string{
"some-proc/some-name": "some-content",
})
Expect(err).NotTo(HaveOccurred())

content, err := ioutil.ReadFile(filepath.Join(path, "some-proc", "some-name"))
Expect(err).NotTo(HaveOccurred())
Expect(string(content)).To(Equal("some-content"))
})

it("writes does not create a directory of the env map is empty", func() {
err := writer.Write(path, map[string]string{})
Expect(err).NotTo(HaveOccurred())
Expand Down

0 comments on commit 2e5389a

Please sign in to comment.