Improve filepath resolution for relative paths
This commit is contained in:
@@ -22,7 +22,7 @@ func (instruction *LinkInstruction) String() string {
|
||||
return fmt.Sprintf("%s%s%s%s%s%s%s%s%s%s%s%s%s", SourceColor, instruction.Source, DefaultColor, deliminer, TargetColor, instruction.Target, DefaultColor, deliminer, strconv.FormatBool(instruction.Force), deliminer, strconv.FormatBool(instruction.Hard), deliminer, strconv.FormatBool(instruction.Delete))
|
||||
}
|
||||
|
||||
func ParseInstruction(line string) (LinkInstruction, error) {
|
||||
func ParseInstruction(line, workdir string) (LinkInstruction, error) {
|
||||
line = strings.TrimSpace(line)
|
||||
parts := strings.Split(line, deliminer)
|
||||
instruction := LinkInstruction{}
|
||||
@@ -51,8 +51,8 @@ func ParseInstruction(line string) (LinkInstruction, error) {
|
||||
instruction.Source, _ = ConvertHome(instruction.Source)
|
||||
instruction.Target, _ = ConvertHome(instruction.Target)
|
||||
|
||||
instruction.Source = NormalizePath(instruction.Source)
|
||||
instruction.Target = NormalizePath(instruction.Target)
|
||||
instruction.Source = NormalizePath(instruction.Source, workdir)
|
||||
instruction.Target = NormalizePath(instruction.Target, workdir)
|
||||
|
||||
return instruction, nil
|
||||
}
|
||||
|
16
main.go
16
main.go
@@ -6,6 +6,7 @@ import (
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
@@ -105,7 +106,8 @@ func ReadFromFilesRecursively(input string, output chan *LinkInstruction, status
|
||||
defer close(output)
|
||||
defer close(status)
|
||||
|
||||
input = NormalizePath(input)
|
||||
workdir, _ := os.Getwd()
|
||||
input = NormalizePath(input, workdir)
|
||||
log.Printf("Reading input from files recursively starting in %s%s%s", PathColor, input, DefaultColor)
|
||||
|
||||
files := make(chan string, 128)
|
||||
@@ -135,7 +137,7 @@ func ReadFromFilesRecursively(input string, output chan *LinkInstruction, status
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
log.Println(file)
|
||||
file = NormalizePath(file)
|
||||
file = NormalizePath(file, workdir)
|
||||
log.Printf("Processing file: %s%s%s", PathColor, file, DefaultColor)
|
||||
|
||||
// This "has" to be done because instructions are resolved in relation to cwd
|
||||
@@ -162,7 +164,7 @@ func ReadFromFile(input string, output chan *LinkInstruction, status chan error,
|
||||
defer close(status)
|
||||
}
|
||||
|
||||
input = NormalizePath(input)
|
||||
input = NormalizePath(input, filepath.Dir(input))
|
||||
log.Printf("Reading input from file: %s%s%s", PathColor, input, DefaultColor)
|
||||
file, err := os.Open(input)
|
||||
if err != nil {
|
||||
@@ -174,7 +176,7 @@ func ReadFromFile(input string, output chan *LinkInstruction, status chan error,
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
instruction, err := ParseInstruction(line)
|
||||
instruction, err := ParseInstruction(line, filepath.Dir(input))
|
||||
if err != nil {
|
||||
log.Printf("Error parsing line: %s'%s'%s, error: %s%+v%s", SourceColor, line, DefaultColor, ErrorColor, err, DefaultColor)
|
||||
continue
|
||||
@@ -187,9 +189,10 @@ func ReadFromArgs(output chan *LinkInstruction, status chan error) {
|
||||
defer close(output)
|
||||
defer close(status)
|
||||
|
||||
workdir, _ := os.Getwd()
|
||||
log.Printf("Reading input from args")
|
||||
for _, arg := range flag.Args() {
|
||||
instruction, err := ParseInstruction(arg)
|
||||
instruction, err := ParseInstruction(arg, workdir)
|
||||
if err != nil {
|
||||
log.Printf("Error parsing arg: %s'%s'%s, error: %s%+v%s", SourceColor, arg, DefaultColor, ErrorColor, err, DefaultColor)
|
||||
continue
|
||||
@@ -201,6 +204,7 @@ func ReadFromStdin(output chan *LinkInstruction, status chan error) {
|
||||
defer close(output)
|
||||
defer close(status)
|
||||
|
||||
workdir, _ := os.Getwd()
|
||||
log.Printf("Reading input from stdin")
|
||||
|
||||
info, err := os.Stdin.Stat()
|
||||
@@ -213,7 +217,7 @@ func ReadFromStdin(output chan *LinkInstruction, status chan error) {
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
instruction, err := ParseInstruction(line)
|
||||
instruction, err := ParseInstruction(line, workdir)
|
||||
if err != nil {
|
||||
log.Printf("Error parsing line: %s'%s'%s, error: %s%+v%s", SourceColor, line, DefaultColor, ErrorColor, err, DefaultColor)
|
||||
continue
|
||||
|
6
util.go
6
util.go
@@ -27,13 +27,13 @@ func FileExists(path string) bool {
|
||||
return err == nil
|
||||
}
|
||||
|
||||
func NormalizePath(input string) string {
|
||||
workingdirectory, _ := os.Getwd()
|
||||
func NormalizePath(input, workdir string) string {
|
||||
input = filepath.ToSlash(input)
|
||||
input = strings.ReplaceAll(input, "\"", "")
|
||||
|
||||
if !filepath.IsAbs(input) {
|
||||
input = workingdirectory + "/" + input
|
||||
log.Printf("Input '%s' is not absolute, prepending work dir '%s'", input, workdir)
|
||||
input = workdir + "/" + input
|
||||
}
|
||||
|
||||
return filepath.Clean(input)
|
||||
|
Reference in New Issue
Block a user