Refactor main to decouple logic from actual main()

This commit is contained in:
2025-10-06 20:11:01 +02:00
parent 9da47ce0cf
commit 8653191df2

68
main.go
View File

@@ -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,6 +85,12 @@ func main() {
// go ReadFromStdin(instructions, status)
default:
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)
@@ -75,6 +101,12 @@ func main() {
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 <source>,<target>,<force?>", programName)
@@ -83,10 +115,10 @@ func main() {
LogInfo("Folder (finding sync files in folder recursively) - %s -r <folder>", programName)
LogInfo("stdin - (cat <file> | %s)", programName)
os.Exit(1)
}
}
}
go func() {
// handleStatusErrors processes status channel errors
func handleStatusErrors(status chan error) {
for {
err, ok := <-status
if !ok {
@@ -96,8 +128,10 @@ func main() {
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 {