Improve filepath resolution for relative paths

This commit is contained in:
2024-09-11 19:43:43 +02:00
parent 290e6fdaba
commit 205f8314d6
3 changed files with 16 additions and 12 deletions

16
main.go
View File

@@ -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