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