Skip to content

Commit

Permalink
helper utility to sanitize windows paths (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: Dawid Ciepiela <[email protected]>
  • Loading branch information
sarumaj and Dawid Ciepiela authored Oct 6, 2023
1 parent bd9d8c5 commit 539c33a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pkg/commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ func runInit(conf *configfile.Configuration, update bool) {
dir = strings.ReplaceAll(dir, "/", "_")
dir = strings.ReplaceAll(dir, conf.Username+"_", "")
}
dir = filepath.ToSlash(filepath.Join(conf.BaseDirectory, filepath.FromSlash(dir)))
dir = filepath.Join(conf.BaseDirectory, filepath.FromSlash(dir))
util.PathSanitize(&dir)

logger.Debugf("Adding repository: %s", dir)
conf.Repositories.Append(configfile.Repository{
Expand Down
3 changes: 3 additions & 0 deletions pkg/commands/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ func getUpdater() (updater *selfupdate.Updater, err error) {
}

func isRepoDir(path string, repos []configfile.Repository) bool {
util.PathSanitize(&path)
for _, r := range repos {
util.PathSanitize(&r.Directory)
if strings.HasPrefix(r.Directory+"/", path+"/") {
return true
}
Expand Down Expand Up @@ -228,6 +230,7 @@ func updateConfigFlags() {

func runLocalStatus() error {
conf := configfile.Load()
util.PathSanitize(&conf.BaseDirectory)

files, err := filepath.Glob(conf.BaseDirectory + "/*")
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/configfile/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ func (conf Configuration) Display(format string, export bool) {

func (conf *Configuration) GetProgressbarDescriptionForVerb(verb string, repo Repository) string {
trim := func(in string) string {
return strings.TrimPrefix(filepath.ToSlash(in), conf.BaseDirectory+"/")
util.PathSanitize(&in, &conf.BaseDirectory)
return strings.TrimPrefix(in, conf.BaseDirectory+"/")
}

maxLength := len(fmt.Sprintf("%s %s", verb, trim(conf.Repositories.LongestName())))
Expand Down
19 changes: 19 additions & 0 deletions pkg/util/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package util

import (
"os"
"path/filepath"
"regexp"
"strings"
)

var hostRegex = regexp.MustCompile(`(?:[^:]+://|[^/]*//)?(?P<Hostname>[^/:]+).*`)
Expand All @@ -24,3 +26,20 @@ func PathExists(path string) bool {
FatalIfError(err)
return false
}

func PathSanitize(paths ...*string) {
for _, path := range paths {
if path == nil {
continue
}

*path = filepath.Clean(*path)

if volume := filepath.VolumeName(*path); volume == "C:" || volume == "c:" {
*path = strings.Replace(*path, volume, "", 1)
}

*path = filepath.ToSlash(*path)
*path = strings.TrimSuffix(*path, "/")
}
}
21 changes: 21 additions & 0 deletions pkg/util/path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,24 @@ func TestGetHostnameFromPath(t *testing.T) {
})
}
}

func TestPathSanitize(t *testing.T) {
for _, tt := range []struct {
name string
args string
want string
}{
{"test#1", "C:\\Users\\admin\\github", "/Users/admin/github"},
{"test#2", "\\home\\dir\\github\\", "/home/dir/github"},
{"test#3", "home\\dir\\github\\", "home/dir/github"},
{"test#4", "D:\\Users\\admin\\github", "D:/Users/admin/github"},
} {
t.Run(tt.name, func(t *testing.T) {
got := tt.args
PathSanitize(&got)
if got != tt.want {
t.Errorf(`PathSanitize(&%q) failed: got: %q, want: %q`, tt.args, got, tt.want)
}
})
}
}

0 comments on commit 539c33a

Please sign in to comment.