aboutsummaryrefslogtreecommitdiff
path: root/pkg/u/file.go
blob: 5010b3eef2a8ea0c37b6bbeb23b7ab721197a93c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package u

import (
	"errors"
	"log/slog"
	"os"
	"strings"
)

func FileExist(filename string) bool {
	if _, err := os.Stat(filename); err == nil {
		return true

	} else if errors.Is(err, os.ErrNotExist) {
		return false
	} else {
		slog.Warn("Schrödinger's file: it may or may not exist", "file", filename) 
		// Schrodinger: file may or may not exist. To be extra safe it will
		// report the file doest not exist
		return false
	}
}

// This is just a slin wraper to make easier to compose path in the template
type Pathing struct {
	sb strings.Builder
}

func NewPathing() *Pathing {
	return &Pathing{}
}

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 {
	for _, v := range p {
		s.AddPath(v)
	}

	return s
}

func (s *Pathing) Done() string {
	return s.sb.String()
}