summaryrefslogtreecommitdiff
path: root/internal/funcs/filepath.go
blob: 726a1d7cbb1138b49bd356966e68da479736bb00 (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package funcs

import (
	"context"
	"path/filepath"

	"github.com/hairyhenderson/gomplate/v4/conv"
)

// FilePathNS - the Path namespace
//
// Deprecated: don't use
func FilePathNS() *FilePathFuncs {
	return &FilePathFuncs{}
}

// AddFilePathFuncs -
//
// Deprecated: use [CreateFilePathFuncs] instead
func AddFilePathFuncs(f map[string]interface{}) {
	for k, v := range CreateFilePathFuncs(context.Background()) {
		f[k] = v
	}
}

// CreateFilePathFuncs -
func CreateFilePathFuncs(ctx context.Context) map[string]interface{} {
	ns := &FilePathFuncs{ctx}

	return map[string]interface{}{
		"filepath": func() interface{} { return ns },
	}
}

// FilePathFuncs -
type FilePathFuncs struct {
	ctx context.Context
}

// Base -
func (f *FilePathFuncs) Base(in interface{}) string {
	return filepath.Base(conv.ToString(in))
}

// Clean -
func (f *FilePathFuncs) Clean(in interface{}) string {
	return filepath.Clean(conv.ToString(in))
}

// Dir -
func (f *FilePathFuncs) Dir(in interface{}) string {
	return filepath.Dir(conv.ToString(in))
}

// Ext -
func (f *FilePathFuncs) Ext(in interface{}) string {
	return filepath.Ext(conv.ToString(in))
}

// FromSlash -
func (f *FilePathFuncs) FromSlash(in interface{}) string {
	return filepath.FromSlash(conv.ToString(in))
}

// IsAbs -
func (f *FilePathFuncs) IsAbs(in interface{}) bool {
	return filepath.IsAbs(conv.ToString(in))
}

// Join -
func (f *FilePathFuncs) Join(elem ...interface{}) string {
	s := conv.ToStrings(elem...)
	return filepath.Join(s...)
}

// Match -
func (f *FilePathFuncs) Match(pattern, name interface{}) (matched bool, err error) {
	return filepath.Match(conv.ToString(pattern), conv.ToString(name))
}

// Rel -
func (f *FilePathFuncs) Rel(basepath, targpath interface{}) (string, error) {
	return filepath.Rel(conv.ToString(basepath), conv.ToString(targpath))
}

// Split -
func (f *FilePathFuncs) Split(in interface{}) []string {
	dir, file := filepath.Split(conv.ToString(in))
	return []string{dir, file}
}

// ToSlash -
func (f *FilePathFuncs) ToSlash(in interface{}) string {
	return filepath.ToSlash(conv.ToString(in))
}

// VolumeName -
func (f *FilePathFuncs) VolumeName(in interface{}) string {
	return filepath.VolumeName(conv.ToString(in))
}