-
Notifications
You must be signed in to change notification settings - Fork 483
Knownhosts tilde aware path expansion - fixes #1107 #1115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
memetb
wants to merge
15
commits into
dmacvicar:main
Choose a base branch
from
memetb:knownhosts-tilde-aware-expansion
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
7e2e673
add utility function for path expansion
memetb e825964
add unit test for ExpandEnvExt utility function
memetb f1950b4
update expandenv.go to use variables that can be mock patched
memetb f8b55be
DRY up code by making use of util.ExpandEnvExt
memetb f516c36
Merge branch 'main' into knownhosts-tilde-aware-expansion
memetb 14c3ddb
remove spurious import
memetb 5d2759f
fix comment spelling mistake
memetb d286cf5
fix bug with tilde interpretation (~foo != ~/foo)
memetb ceee215
add test case for if os.UserHomeDir returns an error
memetb 4b260c8
restore previous redaction
memetb 2f99592
fix comment style
memetb 206bf26
update unit tests so that they are platform independent
memetb a5ef98d
bugfix: be platform independent when dealing with filenames
memetb bf8e95a
bugfix: make unittests pass on windows
memetb d366da8
rename ExpandEnvExt to ExpandPath
memetb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,34 @@ | ||
package util | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
var ( | ||
userHomeDir = os.UserHomeDir | ||
expandEnv = os.ExpandEnv | ||
) | ||
|
||
// ExpandPath expands environment variables and resolves ~ to the home directory | ||
// this is a drop-in replacement for os.ExpandEnv but is additionally '~' aware. | ||
func ExpandPath(path string) string { | ||
path = filepath.Clean(expandEnv(path)) | ||
tilde := filepath.FromSlash("~/") | ||
|
||
// note to maintainers: tilde without a following slash character is simply | ||
// interpreted as part of the filename (e.g. ~foo/bar != ~/foo/bar). However, | ||
// when running on windows, the filepath will be represented by backslashes ('\'), | ||
// therefore we need to convert "~/" to the platform specific format to test for | ||
// it, otherwise on windows systems the prefix test will always fail. | ||
if strings.HasPrefix(path, tilde) { | ||
home, err := userHomeDir() | ||
if err != nil { | ||
return path // return path as-is if unable to resolve home directory | ||
} | ||
// Replace ~ with home directory | ||
path = filepath.Join(home, strings.TrimPrefix(path, tilde)) | ||
} | ||
return path | ||
} |
This file contains hidden or 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 util | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestExpandPath(t *testing.T) { | ||
userHomeDir = func() (string, error) { | ||
return "/home/mock", nil | ||
} | ||
expandEnv = func(s string) string { | ||
return strings.Replace(s, "${HOME}", "/home/mock", 1) | ||
} | ||
|
||
|
||
assert.Equal(t, filepath.FromSlash("foo/bar/baz"), ExpandPath("foo/bar/baz")) | ||
assert.Equal(t, filepath.FromSlash("/home/mock/foo/bar/baz"), ExpandPath("~/foo/bar/baz")) | ||
assert.Equal(t, filepath.FromSlash("/home/mock/foo/bar/baz"), ExpandPath("${HOME}/foo/bar/baz")) | ||
assert.Equal(t, filepath.FromSlash("~foo/bar/baz"), ExpandPath("~foo/bar/baz")) | ||
|
||
userHomeDir = func() (string, error) { | ||
return "", fmt.Errorf("some failure") | ||
} | ||
|
||
// failure to get home expansion should leave string unchanged | ||
assert.Equal(t, filepath.FromSlash("~/foo/bar/baz"), ExpandPath("~/foo/bar/baz")) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.