Make instructions run async
This commit is contained in:
@@ -85,12 +85,15 @@ func (instruction *LinkInstruction) RunSync() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (instruction *LinkInstruction) RunAsync(status chan (error)) {
|
func (instruction *LinkInstruction) RunAsync(status chan (error)) {
|
||||||
|
defer close(status)
|
||||||
if !FileExists(instruction.Source) {
|
if !FileExists(instruction.Source) {
|
||||||
status <- fmt.Errorf("instruction source %s%s%s does not exist", SourceColor, instruction.Source, DefaultColor)
|
status <- fmt.Errorf("instruction source %s%s%s does not exist", SourceColor, instruction.Source, DefaultColor)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if AreSame(instruction.Source, instruction.Target) {
|
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)
|
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) {
|
if FileExists(instruction.Target) {
|
||||||
@@ -98,6 +101,7 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) {
|
|||||||
isSymlink, err := IsSymlink(instruction.Target)
|
isSymlink, err := IsSymlink(instruction.Target)
|
||||||
if err != nil {
|
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)
|
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 {
|
if isSymlink {
|
||||||
@@ -105,20 +109,25 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) {
|
|||||||
err = os.Remove(instruction.Target)
|
err = os.Remove(instruction.Target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
status <- fmt.Errorf("failed deleting %s%s%s due to %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor)
|
status <- fmt.Errorf("failed deleting %s%s%s due to %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
status <- fmt.Errorf("refusing to delte actual (non symlink) file %s%s%s", TargetColor, instruction.Target, DefaultColor)
|
status <- fmt.Errorf("refusing to delte actual (non symlink) file %s%s%s", TargetColor, instruction.Target, DefaultColor)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
status <- fmt.Errorf("target %s%s%s exists - handle manually or set the 'forced' flag (3rd field)", TargetColor, instruction.Target, DefaultColor)
|
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)
|
err := os.Symlink(instruction.Source, instruction.Target)
|
||||||
if err != nil {
|
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)
|
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)
|
log.Printf("Created symlink between %s%s%s and %s%s%s", SourceColor, instruction.Source, DefaultColor, TargetColor, instruction.Target, DefaultColor)
|
||||||
|
|
||||||
status <- nil
|
status <- nil
|
||||||
|
return
|
||||||
}
|
}
|
12
main.go
12
main.go
@@ -3,11 +3,10 @@ package main
|
|||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
const deliminer = ","
|
const deliminer = ","
|
||||||
@@ -62,14 +61,19 @@ func main() {
|
|||||||
log.Printf("stdin - (cat <file> | %s)", programName)
|
log.Printf("stdin - (cat <file> | %s)", programName)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
var wg sync.WaitGroup
|
||||||
for _, instruction := range instructions {
|
for _, instruction := range instructions {
|
||||||
log.Printf("Processing: %s", instruction.String())
|
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 {
|
if err != nil {
|
||||||
log.Printf("Failed parsing instruction %s%s%s due to %s%+v%s", SourceColor, instruction.String(), DefaultColor, ErrorColor, err, DefaultColor)
|
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) {
|
func ReadFromFilesRecursively(input string) ([]LinkInstruction, error) {
|
||||||
|
Reference in New Issue
Block a user