From da87b06b4164c96f91401eb0a14b7b232df11d03 Mon Sep 17 00:00:00 2001 From: krako Date: Wed, 14 Jun 2023 21:59:02 +0200 Subject: [PATCH] feats: add 3 functions for filepaths --- funcs.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/funcs.go b/funcs.go index f37589e..16036c0 100644 --- a/funcs.go +++ b/funcs.go @@ -2,10 +2,12 @@ package main import ( "bytes" + "fmt" "io/ioutil" "os" "path/filepath" "strconv" + "strings" "text/template" "github.com/BurntSushi/toml" @@ -33,6 +35,10 @@ func getFuncMap() template.FuncMap { f["fileSize"] = fileSize f["isDir"] = isDir f["isFile"] = isFile + f["joinPath"] = joinPath + f["toBackslash"] = toBackslash + f["toOsPath"] = toOsPath + f["toSlash"] = toSlash return f } @@ -170,3 +176,23 @@ func isFile(path string) (bool, error) { } return info.Mode().IsRegular(), nil } + +func joinPath(segments []any) string { + string_segments := make([]string, 0, len(segments)) + for _, v := range segments { + string_segments = append(string_segments, fmt.Sprint(v)) + } + return filepath.Join(string_segments...) +} + +func toBackslash(path string) string { + return strings.ReplaceAll(path, "/", "\\") +} + +func toOsPath(path string) string { + return strings.ReplaceAll(path, "\\", string(filepath.Separator)) +} + +func toSlash(path string) string { + return strings.ReplaceAll(path, "\\", "/") +}