diff --git a/instruction.go b/instruction.go index 5e0c1fe..4ff5c02 100644 --- a/instruction.go +++ b/instruction.go @@ -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 } diff --git a/main.go b/main.go index c83f4d4..a2b1627 100644 --- a/main.go +++ b/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 diff --git a/util.go b/util.go index ae5df7c..5230022 100644 --- a/util.go +++ b/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)