diff --git a/algo/util.go b/algo/util.go index f1ede0c..821dcc7 100644 --- a/algo/util.go +++ b/algo/util.go @@ -3,8 +3,28 @@ package algo -import "strings" +import ( + "errors" + "log" + "os" + "strings" +) func sanitiseFName(s string) string { return strings.ReplaceAll(s, " ", "_") } + +// createFolder creates a folder at the specified path, if it doesn't already +// exist. +func createFolder(path string) error { + var err error + + if _, err = os.Stat(path); errors.Is(err, os.ErrNotExist) { + err = os.Mkdir(path, os.ModePerm) + if err != nil { + log.Println(err) + } + } + + return err +} diff --git a/algo/util_test.go b/algo/util_test.go new file mode 100644 index 0000000..7ef307e --- /dev/null +++ b/algo/util_test.go @@ -0,0 +1,24 @@ +// Copyright 2022 wanderer +// SPDX-License-Identifier: GPL-3.0-or-later + +package algo + +import ( + "os" + "testing" +) + +// nolint: ifshort +func TestCreateFolder(t *testing.T) { + // let's assume this will never exist in a clean project... + testPath := "whatever-path" + got := createFolder(testPath) + + var want error + + if want != got { + t.Errorf("issue creating folder: got %q", got.Error()) + } + + os.RemoveAll(testPath) +}