Skip to content

Commit

Permalink
fsmodtime: ExpandEnv allow passing custom list of replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidGamba committed Sep 17, 2024
1 parent 7cc0ae2 commit ae9f763
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
13 changes: 13 additions & 0 deletions fsmodtime/changelog.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
= fsmodtime

== v0.3.0: breaking changes

Change signature of `fsmodtime.ExpandEnv` to accept a map of replacements:

[source, diff]
----
- func ExpandEnv(lines []string) ([]string, error)
+ func ExpandEnv(lines []string, replacements map[string]string) ([]string, error)
----

Since the replacements can be nil, adding nil to the second argument will maintain existing behavior.
13 changes: 10 additions & 3 deletions fsmodtime/paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,18 @@ import (

// ExpandEnv - like os.ExpandEnv on many lines, except that it reports error if any of the env vars is
// not found. It also expands ~/ to $HOME/
func ExpandEnv(lines []string) ([]string, error) {
//
// replacements is a list of additional replacements and can be nil.
func ExpandEnv(lines []string, replacements map[string]string) ([]string, error) {
expanded := []string{}
var err error
notFound := make(map[string]struct{})
mappingFn := func(name string) string {
if replacements != nil {
if value, ok := replacements[name]; ok {
return value
}
}
if value, ok := os.LookupEnv(name); ok {
return value
}
Expand Down Expand Up @@ -72,7 +79,7 @@ func Glob(fsys fs.FS, stop bool, patterns []string) (matches []string, stopped b
//
// Use fsmodtime.Recursive(true) to recurse into directories.
func Target(fsys fs.FS, targets []string, sources []string, opts ...WalkOpt) ([]string, bool, error) {
targets, err := ExpandEnv(targets)
targets, err := ExpandEnv(targets, nil)
if err != nil {
return nil, false, err
}
Expand All @@ -98,7 +105,7 @@ func Target(fsys fs.FS, targets []string, sources []string, opts ...WalkOpt) ([]
//
// Use fsmodtime.Recursive(true) to recurse into directories.
func TargetTime(fsys fs.FS, targetTime time.Time, sources []string, opts ...WalkOpt) ([]string, bool, error) {
sources, err := ExpandEnv(sources)
sources, err := ExpandEnv(sources, nil)
if err != nil {
return nil, false, err
}
Expand Down
11 changes: 10 additions & 1 deletion fsmodtime/paths_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,23 @@ func TestExpandEnv(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
setup(test.env)
out, err := ExpandEnv(test.input)
out, err := ExpandEnv(test.input, nil)
checkError(t, err, test.err)
if !reflect.DeepEqual(out, test.expected) {
t.Errorf("got: %#v, expected: %#v", out, test.expected)
}
cleanup(test.env)
})
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
out, err := ExpandEnv(test.input, test.env)
checkError(t, err, test.err)
if !reflect.DeepEqual(out, test.expected) {
t.Errorf("got: %#v, expected: %#v", out, test.expected)
}
})
}
}

func TestTarget(t *testing.T) {
Expand Down

0 comments on commit ae9f763

Please sign in to comment.