Fix issue with defaulting to reading from file while providing other args
This commit is contained in:
87
main.go
87
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 <source>,<target>,<force?>", programName)
|
||||
log.Printf("File - %s -f <file>", 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() {
|
||||
@@ -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 <source>,<target>,<force?>", programName)
|
||||
log.Printf("File - %s -f <file>", programName)
|
||||
log.Printf("Folder (finding sync files in folder recursively) - %s -r <folder>", programName)
|
||||
log.Printf("stdin - (cat <file> | %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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user