From 94e86f1497a033803aaf3fc4696e1cf199077a10 Mon Sep 17 00:00:00 2001 From: Ahsan Barkati Date: Tue, 9 Mar 2021 18:33:20 +0530 Subject: [PATCH 1/4] Add GetPath method in superflags --- z/flags.go | 33 +++++++++++++++++++++++++++++++++ z/flags_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/z/flags.go b/z/flags.go index 55b294ea..1cb684d7 100644 --- a/z/flags.go +++ b/z/flags.go @@ -3,6 +3,8 @@ package z import ( "fmt" "log" + "os/user" + "path/filepath" "sort" "strconv" "strings" @@ -250,3 +252,34 @@ func (sf *SuperFlag) GetString(opt string) string { } return sf.m[opt] } + +func (sf *SuperFlag) GetPath(opt string) string { + p := sf.GetString(opt) + path, err := expandPath(p) + if err != nil { + log.Fatalf("Failed to get path: %+v", err) + } + return path +} + +// expandPath expands the paths containing ~ to /home/user. It also computes the ablosule path +// from the relative paths. For example: ~/abc/../cef will be transformed to /home/user/cef. +func expandPath(path string) (string, error) { + if len(path) == 0 { + return "", nil + } + if path[0] == '~' { + usr, err := user.Current() + if err != nil { + return "", errors.Wrap(err, "Failed to get the home directory of the user") + } + path = filepath.Join(usr.HomeDir, path[1:]) + } + + var err error + path, err = filepath.Abs(path) + if err != nil { + return "", errors.Wrap(err, "Failed to generate absolute path") + } + return path, nil +} diff --git a/z/flags_test.go b/z/flags_test.go index 23983c87..33ae264a 100644 --- a/z/flags_test.go +++ b/z/flags_test.go @@ -1,6 +1,9 @@ package z import ( + "fmt" + "os/user" + "path/filepath" "testing" "time" @@ -35,3 +38,44 @@ func TestFlag(t *testing.T) { require.Equal(t, time.Hour*12, sf.GetDuration("duration-hours")) require.Equal(t, time.Hour*24*30, sf.GetDuration("duration-days")) } + +func TestGetPath(t *testing.T) { + + usr, err := user.Current() + require.NoError(t, err) + homeDir := usr.HomeDir + + tests := []struct { + path string + expected string + }{ + { + "/home/user/file.txt", + "/home/user/file.txt", + }, + { + "~/file.txt", + filepath.Join(homeDir, "file.txt"), + }, + { + "~/abc/../file.txt", + filepath.Join(homeDir, "file.txt"), + }, + { + "~/", + homeDir, + }, + } + + get := func(p string) string { + opt := fmt.Sprintf("file=%s", p) + sf := NewSuperFlag(opt) + return sf.GetPath("file") + } + + for _, tc := range tests { + actual := get(tc.path) + require.Equalf(t, tc.expected, actual, "Filed on testcase: %s", tc.path) + } + +} From ea28ba027f37e91f0e20e2f9c6ae110c16a806fd Mon Sep 17 00:00:00 2001 From: Ahsan Barkati Date: Tue, 9 Mar 2021 18:47:23 +0530 Subject: [PATCH 2/4] Fix typo --- z/flags_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/z/flags_test.go b/z/flags_test.go index 95376419..53bc5cf8 100644 --- a/z/flags_test.go +++ b/z/flags_test.go @@ -79,6 +79,6 @@ func TestGetPath(t *testing.T) { for _, tc := range tests { actual := get(tc.path) - require.Equalf(t, tc.expected, actual, "Filed on testcase: %s", tc.path) + require.Equalf(t, tc.expected, actual, "Failed on testcase: %s", tc.path) } } From f36347f2e28cfde4ad96b17fcdafa127680c36b3 Mon Sep 17 00:00:00 2001 From: Ahsan Barkati Date: Wed, 10 Mar 2021 14:09:19 +0530 Subject: [PATCH 3/4] Fix case when filename starts with a tilde --- z/flags.go | 4 ++-- z/flags_test.go | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/z/flags.go b/z/flags.go index 637f669b..6ebcc0ba 100644 --- a/z/flags.go +++ b/z/flags.go @@ -259,13 +259,13 @@ func (sf *SuperFlag) GetPath(opt string) string { return path } -// expandPath expands the paths containing ~ to /home/user. It also computes the ablosule path +// expandPath expands the paths containing ~ to /home/user. It also computes the absolute path // from the relative paths. For example: ~/abc/../cef will be transformed to /home/user/cef. func expandPath(path string) (string, error) { if len(path) == 0 { return "", nil } - if path[0] == '~' { + if path[0] == '~' && (len(path) == 1 || path[1] == '/') { usr, err := user.Current() if err != nil { return "", errors.Wrap(err, "Failed to get the home directory of the user") diff --git a/z/flags_test.go b/z/flags_test.go index 53bc5cf8..93f55705 100644 --- a/z/flags_test.go +++ b/z/flags_test.go @@ -2,6 +2,7 @@ package z import ( "fmt" + "os" "os/user" "path/filepath" "testing" @@ -48,6 +49,8 @@ func TestGetPath(t *testing.T) { usr, err := user.Current() require.NoError(t, err) homeDir := usr.HomeDir + cwd, err := os.Getwd() + require.NoError(t, err) tests := []struct { path string @@ -69,6 +72,10 @@ func TestGetPath(t *testing.T) { "~/", homeDir, }, + { + "~filename", + filepath.Join(cwd, "~filename"), + }, } get := func(p string) string { From b6ef326b502601fb6be8a23f511ab1ef74b4b401 Mon Sep 17 00:00:00 2001 From: Ahsan Barkati Date: Wed, 10 Mar 2021 15:32:40 +0530 Subject: [PATCH 4/4] use os.IspathSeparator --- z/flags.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/z/flags.go b/z/flags.go index 6ebcc0ba..c5f48fb9 100644 --- a/z/flags.go +++ b/z/flags.go @@ -3,6 +3,7 @@ package z import ( "fmt" "log" + "os" "os/user" "path/filepath" "sort" @@ -265,7 +266,7 @@ func expandPath(path string) (string, error) { if len(path) == 0 { return "", nil } - if path[0] == '~' && (len(path) == 1 || path[1] == '/') { + if path[0] == '~' && (len(path) == 1 || os.IsPathSeparator(path[1])) { usr, err := user.Current() if err != nil { return "", errors.Wrap(err, "Failed to get the home directory of the user")