handle home directory prefix (#1247)

* handle home directory prefix

Signed-off-by: tharun634 <tharun634@gmail.com>

* add test

Signed-off-by: tharun634 <tharun634@gmail.com>

* lint + remove $HOME prefix

Signed-off-by: tharun634 <tharun634@gmail.com>

* windows check + homeDirPrefix const

Signed-off-by: tharun634 <tharun634@gmail.com>

---------

Signed-off-by: tharun634 <tharun634@gmail.com>
Co-authored-by: Pravin Pushkar <ppushkar@microsoft.com>
Co-authored-by: Shubham Sharma <shubhash@microsoft.com>
Co-authored-by: Mukundan Sundararajan <65565396+mukundansundar@users.noreply.github.com>
This commit is contained in:
Tharun K 2023-04-05 12:03:12 +05:30 committed by GitHub
parent 808defb16c
commit 756b541ae4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 89 additions and 0 deletions

View File

@ -151,6 +151,10 @@ func (a *RunFileConfig) resolvePathToAbsAndValidate(baseDir string, paths ...*st
if *path == "" {
continue
}
*path, err = utils.ResolveHomeDir(*path)
if err != nil {
return err
}
absPath := utils.GetAbsPath(baseDir, *path)
if err != nil {
return err

View File

@ -24,6 +24,7 @@ import (
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"time"
@ -44,6 +45,9 @@ const (
marinerImageVariantName = "mariner"
socketFormat = "%s/dapr-%s-%s.socket"
windowsOsType = "windows"
homeDirPrefix = "~/"
)
// IsValidContainerRuntime checks if the input is a valid container runtime.
@ -350,6 +354,24 @@ func GetAbsPath(baseDir, path string) string {
return absPath
}
// ResolveHomeDir resolves prefix of the given path, if present, to the user's home directory and returns it.
func ResolveHomeDir(filePath string) (string, error) {
if filePath == "" {
return "", nil
}
// Resolve the home directory prefix, if present. This is only supported on non-Windows platforms.
if runtime.GOOS != windowsOsType && strings.HasPrefix(filePath, homeDirPrefix) {
homeDir, err := os.UserHomeDir()
if err != nil {
return "", fmt.Errorf("error in getting the home directory for %s: %w", filePath, err)
}
filePath = filepath.Join(homeDir, filePath[len(homeDirPrefix):])
}
return filePath, nil
}
func ReadFile(filePath string) ([]byte, error) {
bytes, err := os.ReadFile(filePath)
if err != nil {

View File

@ -18,6 +18,7 @@ import (
"math"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/stretchr/testify/assert"
@ -252,6 +253,68 @@ func TestGetAbsPath(t *testing.T) {
}
}
func TestResolveHomeDir(t *testing.T) {
homeDir, err := os.UserHomeDir()
assert.NoError(t, err)
testcases := []struct {
name string
input string
expected string
skipWindows bool
}{
{
name: "empty path",
input: "",
expected: "",
skipWindows: false,
},
{
name: "home directory prefix with ~/",
input: "~/home/path",
expected: filepath.Join(homeDir, "home", "path"),
skipWindows: true,
},
{
name: "home directory prefix with ~/.",
input: "~/./home/path",
expected: filepath.Join(homeDir, ".", "home", "path"),
skipWindows: true,
},
{
name: "home directory prefix with ~/..",
input: "~/../home/path",
expected: filepath.Join(homeDir, "..", "home", "path"),
skipWindows: true,
},
{
name: "no home directory prefix",
input: "../home/path",
expected: "../home/path",
skipWindows: false,
},
{
name: "absolute path",
input: "/absolute/path",
expected: "/absolute/path",
skipWindows: false,
},
}
for _, tc := range testcases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
if tc.skipWindows && runtime.GOOS == "windows" {
t.Skip("Skipping test on Windows")
}
t.Parallel()
actual, err := ResolveHomeDir(tc.input)
assert.NoError(t, err)
assert.Equal(t, tc.expected, actual)
})
}
}
func TestReadFile(t *testing.T) {
fileName := createTempFile(t, "", "test_read_file")
defer cleanupTempDir(t, fileName)