Skip to content

Commit da87b06

Browse files
committed
feats: add 3 functions for filepaths
1 parent 855df3c commit da87b06

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

funcs.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ package main
22

33
import (
44
"bytes"
5+
"fmt"
56
"io/ioutil"
67
"os"
78
"path/filepath"
89
"strconv"
10+
"strings"
911
"text/template"
1012

1113
"github.com/BurntSushi/toml"
@@ -33,6 +35,10 @@ func getFuncMap() template.FuncMap {
3335
f["fileSize"] = fileSize
3436
f["isDir"] = isDir
3537
f["isFile"] = isFile
38+
f["joinPath"] = joinPath
39+
f["toBackslash"] = toBackslash
40+
f["toOsPath"] = toOsPath
41+
f["toSlash"] = toSlash
3642

3743
return f
3844
}
@@ -170,3 +176,23 @@ func isFile(path string) (bool, error) {
170176
}
171177
return info.Mode().IsRegular(), nil
172178
}
179+
180+
func joinPath(segments []any) string {
181+
string_segments := make([]string, 0, len(segments))
182+
for _, v := range segments {
183+
string_segments = append(string_segments, fmt.Sprint(v))
184+
}
185+
return filepath.Join(string_segments...)
186+
}
187+
188+
func toBackslash(path string) string {
189+
return strings.ReplaceAll(path, "/", "\\")
190+
}
191+
192+
func toOsPath(path string) string {
193+
return strings.ReplaceAll(path, "\\", string(filepath.Separator))
194+
}
195+
196+
func toSlash(path string) string {
197+
return strings.ReplaceAll(path, "\\", "/")
198+
}

0 commit comments

Comments
 (0)