Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
fix: mkdirall must create the dirs for a path, not its parent
Browse files Browse the repository at this point in the history
  • Loading branch information
mdelapenya committed Feb 17, 2020
1 parent a181898 commit 244540e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 11 deletions.
3 changes: 2 additions & 1 deletion cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ func GetComposeFile(isStack bool, composeName string) (string, error) {
return "", err
}

io.MkdirAll(composeFilePath)
// create parent directory for the compose file
io.MkdirAll(filepath.Dir(composeFilePath))

err = io.WriteFile(composeBytes, composeFilePath)
if err != nil {
Expand Down
14 changes: 6 additions & 8 deletions cli/internal/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,15 @@ func Exists(path string) (bool, error) {
return true, err
}

// MkdirAll creates all directories in a path
func MkdirAll(file string) error {
// check if parent dir for the file exist, otherwise create it
parent := filepath.Dir(file)
if _, err := os.Stat(parent); os.IsNotExist(err) {
err = os.MkdirAll(parent, 0755)
// MkdirAll creates all directories for a directory path
func MkdirAll(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
err = os.MkdirAll(path, 0755)
if err != nil {
log.WithFields(log.Fields{
"error": err,
"path": parent,
}).Fatal("File cannot be created")
"path": path,
}).Fatal("Directory cannot be created")

return err
}
Expand Down
23 changes: 23 additions & 0 deletions cli/internal/io_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package internal

import (
"path"
"testing"

"github.com/Flaque/filet"
"github.com/stretchr/testify/assert"
)

func TestMkdirAll(t *testing.T) {
defer filet.CleanUp(t)

tmpDir := filet.TmpDir(t, "")

dir := path.Join(tmpDir, ".op", "compose", "services")

err := MkdirAll(dir)
assert.Nil(t, err)

e, _ := Exists(dir)
assert.True(t, e)
}
8 changes: 6 additions & 2 deletions cli/internal/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ func TestRecover(t *testing.T) {
"foo": "bar",
}

MkdirAll(workspace)

Update(ID, workspace, composeFiles, initialEnv)

runFile := filepath.Join(workspace, ID+".run")
MkdirAll(runFile)
e, _ := Exists(runFile)
assert.True(t, e)

Update(ID, workspace, composeFiles, initialEnv)
env := Recover(ID, workspace)

value, e := env["foo"]
Expand Down

0 comments on commit 244540e

Please sign in to comment.