Files
synclib/main.go

257 lines
6.6 KiB
Go

package main
import (
"bufio"
"flag"
"io"
"log"
"os"
"path/filepath"
"regexp"
"sync"
"sync/atomic"
)
const deliminer = ","
const SourceColor = Purple
const TargetColor = Yellow
const ErrorColor = URed
const ImportantColor = BRed
const DefaultColor = White
const PathColor = Green
var FileRegex, _ = regexp.Compile(`sync\.ya?ml$`)
var programName = os.Args[0]
func main() {
recurse := flag.String("r", "", "recurse into directories")
file := flag.String("f", "", "file to read instructions from")
debug := flag.Bool("d", false, "debug")
flag.Parse()
if *debug {
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
logFile, err := os.Create("cln.log")
if err != nil {
log.Printf("Error creating log file: %v", err)
os.Exit(1)
}
logger := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(logger)
} else {
log.SetFlags(log.Lmicroseconds)
}
instructions := make(chan *LinkInstruction, 1000)
status := make(chan error)
// Check input sources in priority order
switch {
case *recurse != "":
log.Printf("Recurse: %s", *recurse)
go ReadFromFilesRecursively(*recurse, instructions, status)
case *file != "":
log.Printf("File: %s", *file)
go ReadFromFile(*file, instructions, status, true)
case len(flag.Args()) > 0:
log.Printf("Reading from command line arguments")
go ReadFromArgs(instructions, status)
case IsPipeInput():
log.Printf("Reading from stdin pipe")
go ReadFromStdin(instructions, status)
default:
if _, err := os.Stat("sync.yaml"); err == nil {
log.Printf("Using default sync.yaml file")
go ReadFromFile("sync.yaml", instructions, status, true)
} else if _, err := os.Stat("sync.yml"); err == nil {
log.Printf("Using default sync.yml file")
go ReadFromFile("sync.yml", instructions, status, true)
} else {
log.Printf("No input provided")
log.Printf("Provide input as: ")
log.Printf("Arguments - %s <source>,<target>,<force?>", programName)
log.Printf("File - %s -f <file>", programName)
log.Printf("YAML File - %s -f <file.yaml>", programName)
log.Printf("Folder (finding sync files in folder recursively) - %s -r <folder>", programName)
log.Printf("stdin - (cat <file> | %s)", programName)
os.Exit(1)
}
}
go func() {
for {
err, ok := <-status
if !ok {
break
}
if err != nil {
log.Println(err)
}
}
}()
var instructionsDone int32
var wg sync.WaitGroup
for {
instruction, ok := <-instructions
if !ok {
log.Printf("No more instructions to process")
break
}
log.Printf("Processing: %s", instruction.String())
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)
}
atomic.AddInt32(&instructionsDone, 1)
wg.Done()
}
wg.Wait()
log.Println("All done")
if instructionsDone == 0 {
log.Printf("No instructions were processed")
os.Exit(1)
}
}
func IsPipeInput() bool {
info, err := os.Stdin.Stat()
if err != nil {
return false
}
return info.Mode()&os.ModeNamedPipe != 0
}
func ReadFromFilesRecursively(input string, output chan *LinkInstruction, status chan error) {
defer close(output)
defer close(status)
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)
recurseStatus := make(chan error)
go GetSyncFilesRecursively(input, files, recurseStatus)
go func() {
for {
err, ok := <-recurseStatus
if !ok {
break
}
if err != nil {
log.Printf("Failed to get sync files recursively: %s%+v%s", ErrorColor, err, DefaultColor)
status <- err
}
}
}()
var wg sync.WaitGroup
for {
file, ok := <-files
if !ok {
log.Printf("No more files to process")
break
}
wg.Add(1)
go func() {
defer wg.Done()
log.Println(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
fileDir := FileRegex.FindStringSubmatch(file)
if fileDir == nil {
log.Printf("Failed to extract directory from %s%s%s", SourceColor, file, DefaultColor)
return
}
log.Printf("Changing directory to %s%s%s (for %s%s%s)", PathColor, fileDir[1], DefaultColor, PathColor, file, DefaultColor)
err := os.Chdir(fileDir[1])
if err != nil {
log.Printf("Failed to change directory to %s%s%s: %s%+v%s", SourceColor, fileDir[1], DefaultColor, ErrorColor, err, DefaultColor)
return
}
ReadFromFile(file, output, status, false)
}()
}
wg.Wait()
}
func ReadFromFile(input string, output chan *LinkInstruction, status chan error, doclose bool) {
if doclose {
defer close(output)
defer close(status)
}
input = NormalizePath(input, filepath.Dir(input))
log.Printf("Reading input from file: %s%s%s", PathColor, input, DefaultColor)
// Check if this is a YAML file
if IsYAMLFile(input) {
log.Printf("Parsing as YAML file")
instructions, err := ParseYAMLFile(input, filepath.Dir(input))
if err != nil {
log.Printf("Failed to parse YAML file %s%s%s: %s%+v%s",
SourceColor, input, DefaultColor, ErrorColor, err, DefaultColor)
status <- err
return
}
for _, instruction := range instructions {
instr := instruction // Create a copy to avoid reference issues
log.Printf("Read YAML instruction: %s", instr.String())
output <- &instr
}
return
}
}
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, workdir)
if err != nil {
log.Printf("Error parsing arg: %s'%s'%s, error: %s%+v%s", SourceColor, arg, DefaultColor, ErrorColor, err, DefaultColor)
continue
}
output <- &instruction
}
}
func ReadFromStdin(output chan *LinkInstruction, status chan error) {
defer close(output)
defer close(status)
workdir, _ := os.Getwd()
log.Printf("Reading input from stdin")
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
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
}
output <- &instruction
}
if err := scanner.Err(); err != nil {
log.Fatalf("Error reading from stdin: %s%+v%s", ErrorColor, err, DefaultColor)
status <- err
return
}
}