From b9a4ff3192d366784de9c70938fa935502b3778d Mon Sep 17 00:00:00 2001 From: surtur Date: Wed, 29 Jun 2022 16:54:55 +0200 Subject: [PATCH] go(algo/util): add createPath func --- algo/util.go | 15 +++++++++++++++ algo/util_test.go | 15 +++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/algo/util.go b/algo/util.go index 821dcc7..6541710 100644 --- a/algo/util.go +++ b/algo/util.go @@ -28,3 +28,18 @@ func createFolder(path string) error { return err } + +// createPath creates all folders in the provided path, if they don't already +// exist. +func createPath(path string) error { + var err error + + if _, err = os.Stat(path); errors.Is(err, os.ErrNotExist) { + err = os.MkdirAll(path, os.ModePerm) + if err != nil { + log.Println(err) + } + } + + return err +} diff --git a/algo/util_test.go b/algo/util_test.go index 7ef307e..c86ad99 100644 --- a/algo/util_test.go +++ b/algo/util_test.go @@ -22,3 +22,18 @@ func TestCreateFolder(t *testing.T) { os.RemoveAll(testPath) } + +// nolint: ifshort +func TestCreatePath(t *testing.T) { + // let's assume this, too, will never exist in a clean project... + testPath := "whatever-path/whatever-subpath/subsubpath" + got := createPath(testPath) + + var want error + + if want != got { + t.Errorf("issue creating folders in path, testPath: %q, full cmd: %q", testPath, got.Error()) + } + + os.RemoveAll(testPath) +}