diff --git a/main.go b/main.go index 89fdf83..821dcdc 100644 --- a/main.go +++ b/main.go @@ -30,7 +30,27 @@ func main() { flag.Parse() undo = *undoF - if *debug { + setupLogging(*debug) + + instructions := make(chan *LinkInstruction, 1000) + status := make(chan error) + + startInputSource(*recurse, *file, instructions, status) + + go handleStatusErrors(status) + + instructionsDone := processInstructions(instructions) + + if instructionsDone == 0 { + LogInfo("No instructions were processed") + os.Exit(1) + } + LogInfo("All done") +} + +// setupLogging configures logging based on debug flag +func setupLogging(debug bool) { + if debug { log.SetFlags(log.Lmicroseconds | log.Lshortfile) logFile, err := os.Create(programName + ".log") if err != nil { @@ -42,19 +62,19 @@ func main() { } else { log.SetFlags(log.Lmicroseconds) } +} - instructions := make(chan *LinkInstruction, 1000) - status := make(chan error) - +// startInputSource determines and starts the appropriate input source +func startInputSource(recurse, file string, instructions chan *LinkInstruction, status chan error) { // Check input sources in priority order switch { - case *recurse != "": - LogInfo("Recurse: %s", *recurse) - go ReadFromFilesRecursively(*recurse, instructions, status) + case recurse != "": + LogInfo("Recurse: %s", recurse) + go ReadFromFilesRecursively(recurse, instructions, status) - case *file != "": - LogInfo("File: %s", *file) - go ReadFromFile(*file, instructions, status, true) + case file != "": + LogInfo("File: %s", file) + go ReadFromFile(file, instructions, status, true) case len(flag.Args()) > 0: LogInfo("Reading from command line arguments") @@ -65,39 +85,53 @@ func main() { // go ReadFromStdin(instructions, status) default: - if _, err := os.Stat("sync"); err == nil { - LogInfo("Using default sync file") - go ReadFromFile("sync", instructions, status, true) - } else if _, err := os.Stat("sync.yaml"); err == nil { - LogInfo("Using default sync.yaml file") - go ReadFromFile("sync.yaml", instructions, status, true) - } else if _, err := os.Stat("sync.yml"); err == nil { - LogInfo("Using default sync.yml file") - go ReadFromFile("sync.yml", instructions, status, true) - } else { - LogInfo("No input provided") - LogInfo("Provide input as: ") - LogInfo("Arguments - %s ,,", programName) - LogInfo("File - %s -f ", programName) - LogInfo("YAML File - %s -f ", programName) - LogInfo("Folder (finding sync files in folder recursively) - %s -r ", programName) - LogInfo("stdin - (cat | %s)", programName) - os.Exit(1) + startDefaultInputSource(instructions, status) + } +} + +// startDefaultInputSource tries to find default sync files +func startDefaultInputSource(instructions chan *LinkInstruction, status chan error) { + if _, err := os.Stat("sync"); err == nil { + LogInfo("Using default sync file") + go ReadFromFile("sync", instructions, status, true) + } else if _, err := os.Stat("sync.yaml"); err == nil { + LogInfo("Using default sync.yaml file") + go ReadFromFile("sync.yaml", instructions, status, true) + } else if _, err := os.Stat("sync.yml"); err == nil { + LogInfo("Using default sync.yml file") + go ReadFromFile("sync.yml", instructions, status, true) + } else { + showUsageAndExit() + } +} + +// showUsageAndExit displays usage information and exits +func showUsageAndExit() { + LogInfo("No input provided") + LogInfo("Provide input as: ") + LogInfo("Arguments - %s ,,", programName) + LogInfo("File - %s -f ", programName) + LogInfo("YAML File - %s -f ", programName) + LogInfo("Folder (finding sync files in folder recursively) - %s -r ", programName) + LogInfo("stdin - (cat | %s)", programName) + os.Exit(1) +} + +// handleStatusErrors processes status channel errors +func handleStatusErrors(status chan error) { + for { + err, ok := <-status + if !ok { + break + } + if err != nil { + LogError("%v", err) } } +} - go func() { - for { - err, ok := <-status - if !ok { - break - } - if err != nil { - LogError("%v", err) - } - } - }() - +// processInstructions processes all instructions from the channel +func processInstructions(instructions chan *LinkInstruction) int32 { var instructionsDone int32 = 0 var wg sync.WaitGroup for { @@ -118,11 +152,7 @@ func main() { wg.Done() } wg.Wait() - if instructionsDone == 0 { - LogInfo("No instructions were processed") - os.Exit(1) - } - LogInfo("All done") + return instructionsDone } func IsPipeInput() bool {