math-optim/util/util.go
surtur 3401a2e58e
All checks were successful
continuous-integration/drone/push Build is passing
go(util): add custom print func
2022-07-13 23:18:31 +02:00

72 lines
1.4 KiB
Go

// Copyright 2022 wanderer <a_mirre at utb dot cz>
// SPDX-License-Identifier: GPL-3.0-or-later
package util
import (
"errors"
"fmt"
"log"
"os"
"strings"
)
// SanitiseFName clears spaces from the string that is supposed to be a file
// name.
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) {
printUtil("creating folder : " + path + " (if not exists)")
err = os.Mkdir(path, os.ModePerm)
if err != nil {
log.Println(err)
}
}
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) {
printUtil("creating path : " + path + " (if not exists)")
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
log.Println(err)
}
}
return err
}
func getUtilLogPrefix() string {
return " ***  util: "
}
func fmtUtilOut(input string) string {
return getUtilLogPrefix() + input
}
func printUtil(input string) {
if _, err := fmt.Fprintln(os.Stderr, fmtUtilOut(input)); err != nil {
fmt.Fprintf(
os.Stdout,
getUtilLogPrefix(),
"error while printing to stderr: %q\n * original message was: %q",
err, input,
)
}
}