Skip to content

Commit

Permalink
fix normalize path err, if path does not contain . or ~ at the beginn…
Browse files Browse the repository at this point in the history
…ing -> returns path as is
  • Loading branch information
Max Maximov committed Oct 14, 2022
1 parent b686503 commit b69bcce
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
# vendor/

#other
cfg.yaml
cfg.yaml
.DS_Store
10 changes: 9 additions & 1 deletion pkg/filesystem/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package filesystem

import (
"errors"
"os"
"path"
)
Expand All @@ -12,6 +13,11 @@ type (
)

func NormalizePath(dir string) (newPath string, err error) {
if len(dir) == 0 {
err = errors.New("unable to normalize empty path")
return
}

if dir[0] == '~' {
var homeDir string
homeDir, err = os.UserHomeDir()
Expand All @@ -20,14 +26,16 @@ func NormalizePath(dir string) (newPath string, err error) {
}

newPath = path.Join(homeDir, dir[1:])
} else if dir[0] == '.' {
} else if dir[0] == '.' && (len(dir) == 1 || dir[2] == '/') {
var curPath string
curPath, err = os.Getwd()
if err != nil {
return
}

newPath = path.Join(curPath, dir[1:])
} else {
newPath = dir
}

return
Expand Down

0 comments on commit b69bcce

Please sign in to comment.