1package pathdriver
2
3import (
4	"path/filepath"
5)
6
7// PathDriver provides all of the path manipulation functions in a common
8// interface. The context should call these and never use the `filepath`
9// package or any other package to manipulate paths.
10type PathDriver interface {
11	Join(paths ...string) string
12	IsAbs(path string) bool
13	Rel(base, target string) (string, error)
14	Base(path string) string
15	Dir(path string) string
16	Clean(path string) string
17	Split(path string) (dir, file string)
18	Separator() byte
19	Abs(path string) (string, error)
20	Walk(string, filepath.WalkFunc) error
21	FromSlash(path string) string
22	ToSlash(path string) string
23	Match(pattern, name string) (matched bool, err error)
24}
25
26// pathDriver is a simple default implementation calls the filepath package.
27type pathDriver struct{}
28
29// LocalPathDriver is the exported pathDriver struct for convenience.
30var LocalPathDriver PathDriver = &pathDriver{}
31
32func (*pathDriver) Join(paths ...string) string {
33	return filepath.Join(paths...)
34}
35
36func (*pathDriver) IsAbs(path string) bool {
37	return filepath.IsAbs(path)
38}
39
40func (*pathDriver) Rel(base, target string) (string, error) {
41	return filepath.Rel(base, target)
42}
43
44func (*pathDriver) Base(path string) string {
45	return filepath.Base(path)
46}
47
48func (*pathDriver) Dir(path string) string {
49	return filepath.Dir(path)
50}
51
52func (*pathDriver) Clean(path string) string {
53	return filepath.Clean(path)
54}
55
56func (*pathDriver) Split(path string) (dir, file string) {
57	return filepath.Split(path)
58}
59
60func (*pathDriver) Separator() byte {
61	return filepath.Separator
62}
63
64func (*pathDriver) Abs(path string) (string, error) {
65	return filepath.Abs(path)
66}
67
68// Note that filepath.Walk calls os.Stat, so if the context wants to
69// to call Driver.Stat() for Walk, they need to create a new struct that
70// overrides this method.
71func (*pathDriver) Walk(root string, walkFn filepath.WalkFunc) error {
72	return filepath.Walk(root, walkFn)
73}
74
75func (*pathDriver) FromSlash(path string) string {
76	return filepath.FromSlash(path)
77}
78
79func (*pathDriver) ToSlash(path string) string {
80	return filepath.ToSlash(path)
81}
82
83func (*pathDriver) Match(pattern, name string) (bool, error) {
84	return filepath.Match(pattern, name)
85}
86