1package u
2
3import (
4 "errors"
5 "log/slog"
6 "os"
7 "strings"
8)
9
10func FileExist(filename string) bool {
11 if _, err := os.Stat(filename); err == nil {
12 return true
13
14 } else if errors.Is(err, os.ErrNotExist) {
15 return false
16 } else {
17 slog.Warn("Schrödinger's file: it may or may not exist", "file", filename)
18 // Schrodinger: file may or may not exist. To be extra safe it will
19 // report the file doest not exist
20 return false
21 }
22}
23
24// This is just a slin wraper to make easier to compose path in the template
25type Pathing struct {
26 sb strings.Builder
27}
28
29func NewPathing() *Pathing {
30 return &Pathing{}
31}
32
33func (s *Pathing) AddPath(p string) *Pathing {
34 if len(p) == 0 {
35 return s
36 }
37
38 // if it has trailing / remove it
39 if p[len(p)-1] == '/' {
40 p = p[:len(p)-1]
41 return s.AddPath(p)
42 }
43
44 // if it does not have it so add
45 if p[0] == '/' {
46 s.sb.WriteString(p)
47 } else {
48 s.sb.WriteString("/" + p)
49 }
50
51 return s
52}
53
54func (s *Pathing) AddPaths(p []string) *Pathing {
55 for _, v := range p {
56 s.AddPath(v)
57 }
58
59 return s
60}
61
62func (s *Pathing) Done() string {
63 return s.sb.String()
64}