generated from moul/golang-repo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Manfred Touron <94029+moul@users.noreply.github.com>
- Loading branch information
Showing
5 changed files
with
194 additions
and
102 deletions.
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
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
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
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,70 @@ | ||
package u | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// ExpandPath performs various expansions on a given path. | ||
// | ||
// - Replaces ~/ with $HOME/. | ||
// - Returns absolute path. | ||
// - Expands env vars. | ||
// TODO: - Follow symlinks. | ||
func ExpandPath(path string) (string, error) { | ||
// expand variables | ||
path = os.ExpandEnv(path) | ||
|
||
// replace ~ with homedir | ||
if len(path) > 1 && path[:2] == "~/" { | ||
home := os.Getenv("HOME") // *nix | ||
if home == "" { | ||
home = os.Getenv("USERPROFILE") // windows | ||
} | ||
if home == "" { | ||
return "", errors.New("user home directory not found") | ||
} | ||
|
||
return strings.Replace(path, "~", home, 1), nil | ||
} | ||
|
||
// compute absolute path | ||
{ | ||
result, err := filepath.Abs(path) | ||
if err != nil { | ||
return "", fmt.Errorf("absolute path: %q: %w", path, err) | ||
} | ||
path = result | ||
} | ||
|
||
// expand env vars | ||
{ | ||
path = os.ExpandEnv(path) | ||
} | ||
|
||
// eval symlinks | ||
// TODO: do not fail if path does not exist | ||
/* | ||
{ | ||
result, err := filepath.EvalSymlinks(path) | ||
if err != nil { | ||
return "", fmt.Errorf("eval symlinks: %q: %w", path, err) | ||
} | ||
path = result | ||
} | ||
*/ | ||
|
||
return path, nil | ||
} | ||
|
||
// MustExpandPath wraps ExpandPath and panics if initialization fails. | ||
func MustExpandPath(path string) string { | ||
ret, err := ExpandPath(path) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return ret | ||
} |
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,65 @@ | ||
package u_test | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"moul.io/u" | ||
) | ||
|
||
func TestExpandPath(t *testing.T) { | ||
os.Setenv("HOME", "/home/foo") | ||
os.Setenv("USER", "foo") | ||
os.Unsetenv("FOOBAR") | ||
workdir, err := os.Getwd() | ||
require.NoError(t, err) | ||
|
||
tests := []struct { | ||
input string | ||
expected string | ||
shouldFails bool | ||
}{ | ||
{"/home/foo", "/home/foo", false}, | ||
{"/home/foo/", "/home/foo", false}, | ||
{"~", filepath.Join(workdir, "~"), false}, | ||
{"~/", "/home/foo/", false}, | ||
{"$HOME/hello", "/home/foo/hello", false}, | ||
{"/home/$USER/hello", "/home/foo/hello", false}, | ||
{"/tmp/$FOOBAR/hello", "/tmp/hello", false}, | ||
} | ||
for _, tc := range tests { | ||
name := strings.Replace(tc.input, "/", "-", -1) | ||
t.Run(name, func(t *testing.T) { | ||
result, err := u.ExpandPath(tc.input) | ||
if tc.shouldFails { | ||
assert.Error(t, err) | ||
assert.Empty(t, result) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.Equal(t, result, tc.expected) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func ExampleExpandPath() { | ||
os.Setenv("HOME", "/home/foo") // just for example | ||
ret, err := u.ExpandPath("~/hello-world.txt") | ||
if err != nil { | ||
panic(err) | ||
} | ||
fmt.Println(ret) | ||
// Output: /home/foo/hello-world.txt | ||
} | ||
|
||
func ExampleMustExpandPath() { | ||
os.Setenv("HOME", "/home/foo") // just for example | ||
ret := u.MustExpandPath("~/hello-world.txt") | ||
fmt.Println(ret) | ||
// Output: /home/foo/hello-world.txt | ||
} |