diff --git a/instruction.go b/instruction.go index 1ec1ac7..c15430a 100644 --- a/instruction.go +++ b/instruction.go @@ -85,12 +85,15 @@ func (instruction *LinkInstruction) RunSync() error { } func (instruction *LinkInstruction) RunAsync(status chan (error)) { + defer close(status) if !FileExists(instruction.Source) { status <- fmt.Errorf("instruction source %s%s%s does not exist", SourceColor, instruction.Source, DefaultColor) + return } if AreSame(instruction.Source, instruction.Target) { status <- fmt.Errorf("source %s%s%s and target %s%s%s are the same, %snothing to do...%s", SourceColor, instruction.Source, DefaultColor, TargetColor, instruction.Target, DefaultColor, PathColor, DefaultColor) + return } if FileExists(instruction.Target) { @@ -98,6 +101,7 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) { isSymlink, err := IsSymlink(instruction.Target) if err != nil { status <- fmt.Errorf("could not determine whether %s%s%s is a sym link or not, stopping; err: %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor) + return } if isSymlink { @@ -105,20 +109,25 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) { err = os.Remove(instruction.Target) if err != nil { status <- fmt.Errorf("failed deleting %s%s%s due to %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor) + return } } else { status <- fmt.Errorf("refusing to delte actual (non symlink) file %s%s%s", TargetColor, instruction.Target, DefaultColor) + return } } else { status <- fmt.Errorf("target %s%s%s exists - handle manually or set the 'forced' flag (3rd field)", TargetColor, instruction.Target, DefaultColor) + return } } err := os.Symlink(instruction.Source, instruction.Target) if err != nil { status <- fmt.Errorf("failed creating symlink between %s%s%s and %s%s%s with error %s%+v%s", SourceColor, instruction.Source, DefaultColor, TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor) + return } log.Printf("Created symlink between %s%s%s and %s%s%s", SourceColor, instruction.Source, DefaultColor, TargetColor, instruction.Target, DefaultColor) status <- nil + return } \ No newline at end of file diff --git a/main.go b/main.go index 417b5c0..5266057 100644 --- a/main.go +++ b/main.go @@ -3,11 +3,10 @@ package main import ( "bufio" "flag" - "fmt" "log" "os" "regexp" - "strings" + "sync" ) const deliminer = "," @@ -62,14 +61,19 @@ func main() { log.Printf("stdin - (cat | %s)", programName) os.Exit(1) } + var wg sync.WaitGroup for _, instruction := range instructions { log.Printf("Processing: %s", instruction.String()) - err := instruction.RunSync() + status := make(chan error) + go instruction.RunAsync(status) + wg.Add(1) + err := <-status if err != nil { log.Printf("Failed parsing instruction %s%s%s due to %s%+v%s", SourceColor, instruction.String(), DefaultColor, ErrorColor, err, DefaultColor) - continue } + wg.Done() } + wg.Wait() } func ReadFromFilesRecursively(input string) ([]LinkInstruction, error) {