Skip to content
This repository has been archived by the owner on Dec 29, 2024. It is now read-only.

Commit

Permalink
fix(path): added replacing tidal char by home directory path
Browse files Browse the repository at this point in the history
  • Loading branch information
Wittano committed Feb 12, 2024
1 parent 9108cca commit 2feacb6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
18 changes: 18 additions & 0 deletions path/path.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package path

import (
"github.com/mitchellh/go-homedir"
"os"
"path/filepath"
"regexp"
"strings"
)

Expand Down Expand Up @@ -37,6 +39,8 @@ func PathsFromPattern(src string) ([]string, error) {
}

func ReplaceEnvVariablesInPath(src string) string {
src = replaceTildeInPath(src)

sep := string(filepath.Separator)
parts := strings.Split(src, sep)

Expand All @@ -49,6 +53,20 @@ func ReplaceEnvVariablesInPath(src string) string {
return strings.Join(parts, sep)
}

func replaceTildeInPath(src string) string {
regex := regexp.MustCompile("^~*")
if regex.MatchString(src) {
home, err := homedir.Dir()
if err != nil {
home = os.Getenv("HOME")
}

return strings.ReplaceAll(src, "~", home)
}

return src
}

func PathsFromPatternRecursive(path string) ([]string, error) {
path = ReplaceEnvVariablesInPath(path)

Expand Down
11 changes: 11 additions & 0 deletions path/path_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package path

import (
"fmt"
"github.com/wittano/filebot/internal/test"
"os"
"path/filepath"
Expand Down Expand Up @@ -32,6 +33,16 @@ func TestPathsFromRegexWithEnvVariable(t *testing.T) {
}
}

func TestReplaceTildaInPath(t *testing.T) {
testPath := "~/test"
exp := fmt.Sprintf("%s/test", os.Getenv("HOME"))

res := replaceTildeInPath(testPath)
if res != exp {
t.Fatalf("Failed replace tilde char by absolute HOME directory path. Result: %s", res)
}
}

func TestPathsFromRegexButRegexStartFromStar(t *testing.T) {
p := test.CreateTempFile(t)
exp := strings.Replace(p, "test", "*est", 1)
Expand Down

0 comments on commit 2feacb6

Please sign in to comment.