Fix up logging a little
This commit is contained in:
95
main.go
95
main.go
@@ -33,7 +33,7 @@ func main() {
|
||||
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
|
||||
logFile, err := os.Create("cln.log")
|
||||
if err != nil {
|
||||
log.Printf("Error creating log file: %v", err)
|
||||
LogError("Error creating log file: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
logger := io.MultiWriter(os.Stdout, logFile)
|
||||
@@ -48,36 +48,36 @@ func main() {
|
||||
// Check input sources in priority order
|
||||
switch {
|
||||
case *recurse != "":
|
||||
log.Printf("Recurse: %s", *recurse)
|
||||
LogInfo("Recurse: %s", *recurse)
|
||||
go ReadFromFilesRecursively(*recurse, instructions, status)
|
||||
|
||||
case *file != "":
|
||||
log.Printf("File: %s", *file)
|
||||
LogInfo("File: %s", *file)
|
||||
go ReadFromFile(*file, instructions, status, true)
|
||||
|
||||
case len(flag.Args()) > 0:
|
||||
log.Printf("Reading from command line arguments")
|
||||
LogInfo("Reading from command line arguments")
|
||||
go ReadFromArgs(instructions, status)
|
||||
|
||||
case IsPipeInput():
|
||||
log.Printf("Reading from stdin pipe")
|
||||
LogInfo("Reading from stdin pipe")
|
||||
go ReadFromStdin(instructions, status)
|
||||
|
||||
default:
|
||||
if _, err := os.Stat("sync.yaml"); err == nil {
|
||||
log.Printf("Using default sync.yaml file")
|
||||
LogInfo("Using default sync.yaml file")
|
||||
go ReadFromFile("sync.yaml", instructions, status, true)
|
||||
} else if _, err := os.Stat("sync.yml"); err == nil {
|
||||
log.Printf("Using default sync.yml file")
|
||||
LogInfo("Using default sync.yml file")
|
||||
go ReadFromFile("sync.yml", 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("YAML File - %s -f <file.yaml>", programName)
|
||||
log.Printf("Folder (finding sync files in folder recursively) - %s -r <folder>", programName)
|
||||
log.Printf("stdin - (cat <file> | %s)", programName)
|
||||
LogInfo("No input provided")
|
||||
LogInfo("Provide input as: ")
|
||||
LogInfo("Arguments - %s <source>,<target>,<force?>", programName)
|
||||
LogInfo("File - %s -f <file>", programName)
|
||||
LogInfo("YAML File - %s -f <file.yaml>", programName)
|
||||
LogInfo("Folder (finding sync files in folder recursively) - %s -r <folder>", programName)
|
||||
LogInfo("stdin - (cat <file> | %s)", programName)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,7 @@ func main() {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
LogError("%v", err)
|
||||
}
|
||||
}
|
||||
}()
|
||||
@@ -99,26 +99,27 @@ func main() {
|
||||
for {
|
||||
instruction, ok := <-instructions
|
||||
if !ok {
|
||||
log.Printf("No more instructions to process")
|
||||
LogInfo("No more instructions to process")
|
||||
break
|
||||
}
|
||||
log.Printf("Processing: %s", instruction.String())
|
||||
LogInfo("Processing: %s", instruction.String())
|
||||
status := make(chan error)
|
||||
go instruction.RunAsync(status)
|
||||
wg.Add(1)
|
||||
err := <-status
|
||||
if err != nil {
|
||||
log.Printf("Failed parsing instruction %s%s%s due to %s%+v%s", SourceColor, instruction.String(), DefaultColor, ErrorColor, err, DefaultColor)
|
||||
LogError("Failed parsing instruction %s due to %v",
|
||||
FormatSourcePath(instruction.String()), err)
|
||||
}
|
||||
atomic.AddInt32(&instructionsDone, 1)
|
||||
wg.Done()
|
||||
}
|
||||
wg.Wait()
|
||||
log.Println("All done")
|
||||
if instructionsDone == 0 {
|
||||
log.Printf("No instructions were processed")
|
||||
LogInfo("No instructions were processed")
|
||||
os.Exit(1)
|
||||
}
|
||||
LogInfo("All done")
|
||||
}
|
||||
|
||||
func IsPipeInput() bool {
|
||||
@@ -135,55 +136,61 @@ func ReadFromFilesRecursively(input string, output chan *LinkInstruction, status
|
||||
|
||||
workdir, _ := os.Getwd()
|
||||
input = NormalizePath(input, workdir)
|
||||
log.Printf("Reading input from files recursively starting in %s%s%s", PathColor, input, DefaultColor)
|
||||
LogInfo("Reading input from files recursively starting in %s%s%s", PathColor, input, DefaultColor)
|
||||
|
||||
files := make(chan string, 128)
|
||||
recurseStatus := make(chan error)
|
||||
go GetSyncFilesRecursively(input, files, recurseStatus)
|
||||
fileStatus := make(chan error)
|
||||
var wg sync.WaitGroup
|
||||
go GetSyncFilesRecursively(input, files, fileStatus)
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(files)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
for {
|
||||
err, ok := <-recurseStatus
|
||||
err, ok := <-fileStatus
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
log.Printf("Failed to get sync files recursively: %s%+v%s", ErrorColor, err, DefaultColor)
|
||||
LogError("Failed to get sync files recursively: %v", err)
|
||||
status <- err
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for {
|
||||
file, ok := <-files
|
||||
if !ok {
|
||||
log.Printf("No more files to process")
|
||||
LogInfo("No more files to process")
|
||||
break
|
||||
}
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
log.Println(file)
|
||||
LogInfo(file)
|
||||
file = NormalizePath(file, workdir)
|
||||
log.Printf("Processing file: %s%s%s", PathColor, file, DefaultColor)
|
||||
LogInfo("Processing file: %s%s%s", PathColor, file, DefaultColor)
|
||||
|
||||
// This "has" to be done because instructions are resolved in relation to cwd
|
||||
fileDir := FileRegex.FindStringSubmatch(file)
|
||||
if fileDir == nil {
|
||||
log.Printf("Failed to extract directory from %s%s%s", SourceColor, file, DefaultColor)
|
||||
LogError("Failed to extract directory from %s%s%s", SourceColor, file, DefaultColor)
|
||||
return
|
||||
}
|
||||
log.Printf("Changing directory to %s%s%s (for %s%s%s)", PathColor, fileDir[1], DefaultColor, PathColor, file, DefaultColor)
|
||||
LogInfo("Changing directory to %s%s%s (for %s%s%s)", PathColor, fileDir[1], DefaultColor, PathColor, file, DefaultColor)
|
||||
err := os.Chdir(fileDir[1])
|
||||
if err != nil {
|
||||
log.Printf("Failed to change directory to %s%s%s: %s%+v%s", SourceColor, fileDir[1], DefaultColor, ErrorColor, err, DefaultColor)
|
||||
LogError("Failed to change directory to %s%s%s: %v",
|
||||
SourceColor, fileDir[1], DefaultColor, err)
|
||||
return
|
||||
}
|
||||
|
||||
ReadFromFile(file, output, status, false)
|
||||
// Don't return directory, stay where we are
|
||||
os.Chdir(workdir)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func ReadFromFile(input string, output chan *LinkInstruction, status chan error, doclose bool) {
|
||||
@@ -193,14 +200,14 @@ func ReadFromFile(input string, output chan *LinkInstruction, status chan error,
|
||||
}
|
||||
|
||||
input = NormalizePath(input, filepath.Dir(input))
|
||||
log.Printf("Reading input from file: %s%s%s", PathColor, input, DefaultColor)
|
||||
LogInfo("Reading input from file: %s%s%s", PathColor, input, DefaultColor)
|
||||
|
||||
// Check if this is a YAML file
|
||||
if IsYAMLFile(input) {
|
||||
log.Printf("Parsing as YAML file")
|
||||
LogInfo("Parsing as YAML file")
|
||||
instructions, err := ParseYAMLFile(input, filepath.Dir(input))
|
||||
if err != nil {
|
||||
log.Printf("Failed to parse YAML file %s%s%s: %s%+v%s",
|
||||
LogError("Failed to parse YAML file %s%s%s: %s%+v%s",
|
||||
SourceColor, input, DefaultColor, ErrorColor, err, DefaultColor)
|
||||
status <- err
|
||||
return
|
||||
@@ -208,7 +215,7 @@ func ReadFromFile(input string, output chan *LinkInstruction, status chan error,
|
||||
|
||||
for _, instruction := range instructions {
|
||||
instr := instruction // Create a copy to avoid reference issues
|
||||
log.Printf("Read YAML instruction: %s", instr.String())
|
||||
LogInfo("Read YAML instruction: %s", instr.String())
|
||||
output <- &instr
|
||||
}
|
||||
return
|
||||
@@ -220,11 +227,12 @@ func ReadFromArgs(output chan *LinkInstruction, status chan error) {
|
||||
defer close(status)
|
||||
|
||||
workdir, _ := os.Getwd()
|
||||
log.Printf("Reading input from args")
|
||||
LogInfo("Reading input from args")
|
||||
for _, arg := range flag.Args() {
|
||||
instruction, err := ParseInstruction(arg, workdir)
|
||||
if err != nil {
|
||||
log.Printf("Error parsing arg: %s'%s'%s, error: %s%+v%s", SourceColor, arg, DefaultColor, ErrorColor, err, DefaultColor)
|
||||
LogError("Error parsing arg: %s'%s'%s, error: %v",
|
||||
SourceColor, arg, DefaultColor, err)
|
||||
continue
|
||||
}
|
||||
output <- &instruction
|
||||
@@ -236,20 +244,21 @@ func ReadFromStdin(output chan *LinkInstruction, status chan error) {
|
||||
defer close(status)
|
||||
|
||||
workdir, _ := os.Getwd()
|
||||
log.Printf("Reading input from stdin")
|
||||
LogInfo("Reading input from stdin")
|
||||
|
||||
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)
|
||||
LogError("Error parsing line: %s'%s'%s, error: %v",
|
||||
SourceColor, line, DefaultColor, err)
|
||||
continue
|
||||
}
|
||||
output <- &instruction
|
||||
}
|
||||
if err := scanner.Err(); err != nil {
|
||||
log.Fatalf("Error reading from stdin: %s%+v%s", ErrorColor, err, DefaultColor)
|
||||
LogError("Error reading from stdin: %v", err)
|
||||
status <- err
|
||||
return
|
||||
}
|
||||
|
Reference in New Issue
Block a user