forked from meltwater/drone-cache
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* expand env vars * Expand mount paths 1 Introduce config_utils to expand paths using Go's utility. 2. Handle '~' separately in the expansion logic, as this is not handled by os.ExpandEnv. 3. Add unit tests. * Remove Os.ExpandEnv() from rebuilder
- Loading branch information
1 parent
e604755
commit f332f67
Showing
3 changed files
with
53 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package plugin | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
) | ||
|
||
// For all mounts, map the `~` symbol to `$HOME` and expand it. | ||
func expandConfigPath(mounts []string) []string { | ||
expandedPaths := make([]string, len(mounts)) | ||
for i, mount := range mounts { | ||
// As `~` is not handled by `ExpandEnv()`, replace it with `$HOME`. | ||
mount = strings.Replace(mount, "~", "$HOME", 1) | ||
expandedPaths[i] = os.ExpandEnv(mount) | ||
} | ||
return expandedPaths | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package plugin | ||
|
||
import ( | ||
"fmt" | ||
"github.com/meltwater/drone-cache/test" | ||
"log" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestExpandConfigPath_Tilde(t *testing.T) { | ||
testMountTilde := []string{"~/test/path"} | ||
wantExpandedMountTilde := []string{fmt.Sprintf("%s/test/path", osExpand("$HOME"))} | ||
gotExpandedMountTilde := expandConfigPath(testMountTilde) | ||
test.Equals(t, wantExpandedMountTilde, gotExpandedMountTilde) | ||
|
||
} | ||
|
||
func TestExpandConfigPath_EnvVar(t *testing.T) { | ||
testMount := []string{"$HOME/test/path"} | ||
wantExpandedMount := []string{fmt.Sprintf("%s/test/path", osExpand("$HOME"))} | ||
gotExpandedMount := expandConfigPath(testMount) | ||
test.Equals(t, wantExpandedMount, gotExpandedMount) | ||
} | ||
|
||
func osExpand(symbol string) string { | ||
absolutePath := os.ExpandEnv(symbol) | ||
if absolutePath == "" { | ||
log.Fatalf("Could not find the absolute path for symbol/envVar: %s", symbol) | ||
} | ||
return absolutePath | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters