go(algo/util): add createFolder func
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
surtur 2022-06-28 23:40:35 +02:00
parent 35d433e847
commit b24f7db46f
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 45 additions and 1 deletions

@ -3,8 +3,28 @@
package algo package algo
import "strings" import (
"errors"
"log"
"os"
"strings"
)
func sanitiseFName(s string) string { func sanitiseFName(s string) string {
return strings.ReplaceAll(s, " ", "_") 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
}

24
algo/util_test.go Normal file

@ -0,0 +1,24 @@
// Copyright 2022 wanderer <a_mirre at utb dot cz>
// 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)
}