From 1c23ad0cfd994540d9cd99274cbbef28e9bd11f9 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 10 Nov 2024 15:58:30 +0100 Subject: [PATCH] Fix issue with defaulting to reading from file while providing other args --- main.go | 87 +++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 53 insertions(+), 34 deletions(-) diff --git a/main.go b/main.go index a2b1627..09bbc2e 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,7 @@ var programName = os.Args[0] func main() { recurse := flag.String("r", "", "recurse into directories") - file := flag.String("f", "sync", "file to read instructions from") + file := flag.String("f", "", "file to read instructions from") debug := flag.Bool("d", false, "debug") flag.Parse() @@ -43,19 +43,40 @@ func main() { log.SetFlags(log.Lmicroseconds) } - log.Printf("Recurse: %s", *recurse) - log.Printf("File: %s", *file) - instructions := make(chan *LinkInstruction, 1000) status := make(chan error) - if *recurse != "" { + + // Check input sources in priority order + switch { + case *recurse != "": + log.Printf("Recurse: %s", *recurse) go ReadFromFilesRecursively(*recurse, instructions, status) - } else if *file != "" { + + case *file != "": + log.Printf("File: %s", *file) go ReadFromFile(*file, instructions, status, true) - } else if len(os.Args) > 1 { + + case len(flag.Args()) > 0: + log.Printf("Reading from command line arguments") go ReadFromArgs(instructions, status) - } else { + + case IsPipeInput(): + log.Printf("Reading from stdin pipe") go ReadFromStdin(instructions, status) + + default: + if _, err := os.Stat("sync"); err == nil { + log.Printf("Using default sync file") + go ReadFromFile("sync", instructions, status, true) + } else { + log.Printf("No input provided") + log.Printf("Provide input as: ") + log.Printf("Arguments - %s ,,", programName) + log.Printf("File - %s -f ", programName) + log.Printf("Folder (finding sync files in folder recursively) - %s -r ", programName) + log.Printf("stdin - (cat | %s)", programName) + os.Exit(1) + } } go func() { @@ -92,16 +113,19 @@ func main() { wg.Wait() log.Println("All done") if instructionsDone == 0 { - log.Printf("No input provided") - log.Printf("Provide input as: ") - log.Printf("Arguments - %s ,,", programName) - log.Printf("File - %s -f ", programName) - log.Printf("Folder (finding sync files in folder recursively) - %s -r ", programName) - log.Printf("stdin - (cat | %s)", programName) + 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) @@ -158,6 +182,7 @@ func ReadFromFilesRecursively(input string, output chan *LinkInstruction, status } wg.Wait() } + func ReadFromFile(input string, output chan *LinkInstruction, status chan error, doclose bool) { if doclose { defer close(output) @@ -185,6 +210,7 @@ func ReadFromFile(input string, output chan *LinkInstruction, status chan error, output <- &instruction } } + func ReadFromArgs(output chan *LinkInstruction, status chan error) { defer close(output) defer close(status) @@ -200,6 +226,7 @@ func ReadFromArgs(output chan *LinkInstruction, status chan error) { output <- &instruction } } + func ReadFromStdin(output chan *LinkInstruction, status chan error) { defer close(output) defer close(status) @@ -207,27 +234,19 @@ func ReadFromStdin(output chan *LinkInstruction, status chan error) { workdir, _ := os.Getwd() log.Printf("Reading input from stdin") - info, err := os.Stdin.Stat() - if err != nil { - log.Fatalf("Failed to stat stdin: %s%+v%s", ErrorColor, err, DefaultColor) + 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 } - if info.Mode()&os.ModeNamedPipe != 0 { - 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 - } - } }