aboutsummaryrefslogtreecommitdiff
path: root/pkg/u/file.go
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2024-07-07 20:28:42 +0200
committerGabriel A. Giovanini <mail@gabrielgio.me>2024-07-07 21:44:29 +0200
commit44bc8e4078a09857ad86691a83e7ba7d4e3a69c4 (patch)
tree6788cafeaac286d381cc69e1917dab6ae58d1f85 /pkg/u/file.go
parent8dff852753a1c4a708fd87e3cbb0f4844803aa95 (diff)
downloadcerrado-44bc8e4078a09857ad86691a83e7ba7d4e3a69c4.tar.gz
cerrado-44bc8e4078a09857ad86691a83e7ba7d4e3a69c4.tar.bz2
cerrado-44bc8e4078a09857ad86691a83e7ba7d4e3a69c4.zip
ref: Simplify path builder code
Diffstat (limited to 'pkg/u/file.go')
-rw-r--r--pkg/u/file.go44
1 files changed, 33 insertions, 11 deletions
diff --git a/pkg/u/file.go b/pkg/u/file.go
index fafe0fb..5010b3e 100644
--- a/pkg/u/file.go
+++ b/pkg/u/file.go
@@ -4,7 +4,7 @@ import (
"errors"
"log/slog"
"os"
- "path/filepath"
+ "strings"
)
func FileExist(filename string) bool {
@@ -22,21 +22,43 @@ func FileExist(filename string) bool {
}
// This is just a slin wraper to make easier to compose path in the template
-type Pathing string
+type Pathing struct {
+ sb strings.Builder
+}
-func Root() Pathing {
- return "/"
+func NewPathing() *Pathing {
+ return &Pathing{}
}
-func (s Pathing) AddPath(p string) Pathing {
- return Pathing(filepath.Join(string(s), p))
+func (s *Pathing) AddPath(p string) *Pathing {
+ if len(p) == 0 {
+ return s
+ }
+
+ // if it has trailing / remove it
+ if p[len(p)-1] == '/' {
+ p = p[:len(p)-1]
+ return s.AddPath(p)
+ }
+
+ // if it does not have it so add
+ if p[0] == '/' {
+ s.sb.WriteString(p)
+ } else {
+ s.sb.WriteString("/" + p)
+ }
+
+ return s
}
-func (s Pathing) AddPaths(p []string) Pathing {
- f := filepath.Join(p...)
- return Pathing(filepath.Join(string(s), f))
+func (s *Pathing) AddPaths(p []string) *Pathing {
+ for _, v := range p {
+ s.AddPath(v)
+ }
+
+ return s
}
-func (s Pathing) Done() string {
- return string(s)
+func (s *Pathing) Done() string {
+ return s.sb.String()
}