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

66
main.go
View File

@@ -30,7 +30,27 @@ func main() {
flag.Parse() flag.Parse()
undo = *undoF 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) log.SetFlags(log.Lmicroseconds | log.Lshortfile)
logFile, err := os.Create(programName + ".log") logFile, err := os.Create(programName + ".log")
if err != nil { if err != nil {
@@ -42,19 +62,19 @@ func main() {
} else { } else {
log.SetFlags(log.Lmicroseconds) log.SetFlags(log.Lmicroseconds)
} }
}
instructions := make(chan *LinkInstruction, 1000) // startInputSource determines and starts the appropriate input source
status := make(chan error) func startInputSource(recurse, file string, instructions chan *LinkInstruction, status chan error) {
// Check input sources in priority order // Check input sources in priority order
switch { switch {
case *recurse != "": case recurse != "":
LogInfo("Recurse: %s", *recurse) LogInfo("Recurse: %s", recurse)
go ReadFromFilesRecursively(*recurse, instructions, status) go ReadFromFilesRecursively(recurse, instructions, status)
case *file != "": case file != "":
LogInfo("File: %s", *file) LogInfo("File: %s", file)
go ReadFromFile(*file, instructions, status, true) go ReadFromFile(file, instructions, status, true)
case len(flag.Args()) > 0: case len(flag.Args()) > 0:
LogInfo("Reading from command line arguments") LogInfo("Reading from command line arguments")
@@ -65,6 +85,12 @@ func main() {
// go ReadFromStdin(instructions, status) // go ReadFromStdin(instructions, status)
default: 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 { if _, err := os.Stat("sync"); err == nil {
LogInfo("Using default sync file") LogInfo("Using default sync file")
go ReadFromFile("sync", instructions, status, true) go ReadFromFile("sync", instructions, status, true)
@@ -75,6 +101,12 @@ func main() {
LogInfo("Using default sync.yml file") LogInfo("Using default sync.yml file")
go ReadFromFile("sync.yml", instructions, status, true) go ReadFromFile("sync.yml", instructions, status, true)
} else { } else {
showUsageAndExit()
}
}
// showUsageAndExit displays usage information and exits
func showUsageAndExit() {
LogInfo("No input provided") LogInfo("No input provided")
LogInfo("Provide input as: ") LogInfo("Provide input as: ")
LogInfo("Arguments - %s <source>,<target>,<force?>", programName) LogInfo("Arguments - %s <source>,<target>,<force?>", programName)
@@ -84,9 +116,9 @@ func main() {
LogInfo("stdin - (cat <file> | %s)", programName) LogInfo("stdin - (cat <file> | %s)", programName)
os.Exit(1) os.Exit(1)
} }
}
go func() { // handleStatusErrors processes status channel errors
func handleStatusErrors(status chan error) {
for { for {
err, ok := <-status err, ok := <-status
if !ok { if !ok {
@@ -96,8 +128,10 @@ func main() {
LogError("%v", err) LogError("%v", err)
} }
} }
}() }
// processInstructions processes all instructions from the channel
func processInstructions(instructions chan *LinkInstruction) int32 {
var instructionsDone int32 = 0 var instructionsDone int32 = 0
var wg sync.WaitGroup var wg sync.WaitGroup
for { for {
@@ -118,11 +152,7 @@ func main() {
wg.Done() wg.Done()
} }
wg.Wait() wg.Wait()
if instructionsDone == 0 { return instructionsDone
LogInfo("No instructions were processed")
os.Exit(1)
}
LogInfo("All done")
} }
func IsPipeInput() bool { func IsPipeInput() bool {