Skip to content

Commit 3f47fc6

Browse files
authored
Merge pull request #60 from LandonTClipp/options
Add functional options to NewPath
2 parents 6241f9d + 9b6615a commit 3f47fc6

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

path.go

+27-7
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,40 @@ type Path struct {
2626
Sep string
2727
}
2828

29-
// NewPath returns a new OS path
30-
func NewPath(path string) *Path {
31-
return NewPathAfero(path, afero.NewOsFs())
29+
type PathOpts func(p *Path)
30+
31+
func PathWithAfero(fs afero.Fs) PathOpts {
32+
return func(p *Path) {
33+
p.fs = fs
34+
}
3235
}
3336

34-
// NewPathAfero returns a Path object with the given Afero object
35-
func NewPathAfero(path string, fs afero.Fs) *Path {
36-
return &Path{
37+
func PathWithSeperator(sep string) PathOpts {
38+
return func(p *Path) {
39+
p.Sep = sep
40+
}
41+
}
42+
43+
// NewPath returns a new OS path
44+
func NewPath(path string, opts ...PathOpts) *Path {
45+
p := &Path{
3746
path: path,
38-
fs: fs,
47+
fs: afero.NewOsFs(),
3948
DefaultFileMode: DefaultFileMode,
4049
DefaultDirMode: DefaultDirMode,
4150
Sep: string(os.PathSeparator),
4251
}
52+
for _, opt := range opts {
53+
opt(p)
54+
}
55+
return p
56+
}
57+
58+
// NewPathAfero returns a Path object with the given Afero object
59+
//
60+
// Deprecated: Use the PathWithAfero option in Newpath instead.
61+
func NewPathAfero(path string, fs afero.Fs) *Path {
62+
return NewPath(path, PathWithAfero(fs))
4363
}
4464

4565
// Glob returns all of the path objects matched by the given pattern

0 commit comments

Comments
 (0)