Fix up logging a little
This commit is contained in:
@@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"slices"
|
"slices"
|
||||||
@@ -157,7 +156,7 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
if info.Mode().IsRegular() && info.Name() == filepath.Base(instruction.Source) {
|
if info.Mode().IsRegular() && info.Name() == filepath.Base(instruction.Source) {
|
||||||
log.Printf("Overwriting existing file %s%s%s", TargetColor, instruction.Target, DefaultColor)
|
LogTarget("Overwriting existing file %s", instruction.Target)
|
||||||
err := os.Remove(instruction.Target)
|
err := os.Remove(instruction.Target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
status <- fmt.Errorf("could not remove existing file %s%s%s; err: %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor)
|
status <- fmt.Errorf("could not remove existing file %s%s%s; err: %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor)
|
||||||
@@ -167,7 +166,7 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if isSymlink {
|
if isSymlink {
|
||||||
log.Printf("Removing symlink at %s%s%s", TargetColor, instruction.Target, DefaultColor)
|
LogTarget("Removing symlink at %s", instruction.Target)
|
||||||
err = os.Remove(instruction.Target)
|
err = os.Remove(instruction.Target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
status <- fmt.Errorf("failed deleting %s%s%s due to %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor)
|
status <- fmt.Errorf("failed deleting %s%s%s due to %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor)
|
||||||
@@ -178,7 +177,7 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) {
|
|||||||
status <- fmt.Errorf("refusing to delte actual (non symlink) file %s%s%s", TargetColor, instruction.Target, DefaultColor)
|
status <- fmt.Errorf("refusing to delte actual (non symlink) file %s%s%s", TargetColor, instruction.Target, DefaultColor)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("%sDeleting (!!!)%s %s%s%s", ImportantColor, DefaultColor, TargetColor, instruction.Target, DefaultColor)
|
LogImportant("Deleting (!!!) %s", instruction.Target)
|
||||||
err = os.RemoveAll(instruction.Target)
|
err = os.RemoveAll(instruction.Target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
status <- fmt.Errorf("failed deleting %s%s%s due to %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor)
|
status <- fmt.Errorf("failed deleting %s%s%s due to %s%+v%s", TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor)
|
||||||
@@ -210,7 +209,9 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) {
|
|||||||
status <- fmt.Errorf("failed creating symlink between %s%s%s and %s%s%s with error %s%+v%s", SourceColor, instruction.Source, DefaultColor, TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor)
|
status <- fmt.Errorf("failed creating symlink between %s%s%s and %s%s%s with error %s%+v%s", SourceColor, instruction.Source, DefaultColor, TargetColor, instruction.Target, DefaultColor, ErrorColor, err, DefaultColor)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("Created symlink between %s%s%s and %s%s%s", SourceColor, instruction.Source, DefaultColor, TargetColor, instruction.Target, DefaultColor)
|
LogInfo("Created symlink between %s and %s",
|
||||||
|
FormatSourcePath(instruction.Source),
|
||||||
|
FormatTargetPath(instruction.Target))
|
||||||
|
|
||||||
status <- nil
|
status <- nil
|
||||||
}
|
}
|
||||||
@@ -237,12 +238,13 @@ func ParseYAMLFile(filename, workdir string) ([]LinkInstruction, error) {
|
|||||||
toRemove := []int{}
|
toRemove := []int{}
|
||||||
for i, link := range config.Links {
|
for i, link := range config.Links {
|
||||||
if strings.Contains(link.Source, "*") {
|
if strings.Contains(link.Source, "*") {
|
||||||
log.Printf("Expanding wildcard source %s%s%s in YAML file %s%s%s", SourceColor, link.Source, DefaultColor, SourceColor, filename, DefaultColor)
|
LogSource("Expanding wildcard source %s in YAML file %s", link.Source, filename)
|
||||||
newlinks, err := ExpandWildcard(link.Source, workdir, link.Target)
|
newlinks, err := ExpandWildcard(link.Source, workdir, link.Target)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error expanding wildcard: %w", err)
|
return nil, fmt.Errorf("error expanding wildcard: %w", err)
|
||||||
}
|
}
|
||||||
log.Printf("Expanded wildcard source %s%s%s in YAML file %s%s%s to %d links", SourceColor, link.Source, DefaultColor, SourceColor, filename, DefaultColor, len(newlinks))
|
LogInfo("Expanded wildcard source %s in YAML file %s to %d links",
|
||||||
|
FormatSourcePath(link.Source), FormatSourcePath(filename), len(newlinks))
|
||||||
config.Links = append(config.Links, newlinks...)
|
config.Links = append(config.Links, newlinks...)
|
||||||
toRemove = append(toRemove, i)
|
toRemove = append(toRemove, i)
|
||||||
}
|
}
|
||||||
@@ -287,6 +289,7 @@ func ExpandWildcard(source, workdir, target string) (links []LinkInstruction, er
|
|||||||
links = append(links, link)
|
links = append(links, link)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
LogInfo("Expanded wildcard source %s to %d links", FormatSourcePath(source), len(links))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
66
logger.go
Normal file
66
logger.go
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// LogInfo logs an informational message
|
||||||
|
func LogInfo(format string, args ...interface{}) {
|
||||||
|
log.Printf(format, args...)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LogSource logs a message about a source file/path with proper coloring
|
||||||
|
func LogSource(format string, args ...interface{}) {
|
||||||
|
message := fmt.Sprintf(format, args...)
|
||||||
|
log.Printf("%s%s%s", SourceColor, message, DefaultColor)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LogTarget logs a message about a target file/path with proper coloring
|
||||||
|
func LogTarget(format string, args ...interface{}) {
|
||||||
|
message := fmt.Sprintf(format, args...)
|
||||||
|
log.Printf("%s%s%s", TargetColor, message, DefaultColor)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LogPath logs a message about a path with proper coloring
|
||||||
|
func LogPath(format string, args ...interface{}) {
|
||||||
|
message := fmt.Sprintf(format, args...)
|
||||||
|
log.Printf("%s%s%s", PathColor, message, DefaultColor)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LogImportant logs a message that needs attention with proper coloring
|
||||||
|
func LogImportant(format string, args ...interface{}) {
|
||||||
|
message := fmt.Sprintf(format, args...)
|
||||||
|
log.Printf("%s%s%s", ImportantColor, message, DefaultColor)
|
||||||
|
}
|
||||||
|
|
||||||
|
// LogError logs an error message with proper coloring that won't be cut off
|
||||||
|
func LogError(format string, args ...interface{}) {
|
||||||
|
message := fmt.Sprintf(format, args...)
|
||||||
|
log.Printf("%s%s%s", ErrorColor, message, DefaultColor)
|
||||||
|
// Make sure we're properly resetting before applying error color to avoid cutoffs
|
||||||
|
log.Printf("%s%s%s", ErrorColor, fmt.Sprintf(format, args...), DefaultColor)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FormatSourcePath formats a source path with proper coloring
|
||||||
|
func FormatSourcePath(path string) string {
|
||||||
|
return fmt.Sprintf("%s%s%s", SourceColor, path, DefaultColor)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FormatTargetPath formats a target path with proper coloring
|
||||||
|
func FormatTargetPath(path string) string {
|
||||||
|
return fmt.Sprintf("%s%s%s", TargetColor, path, DefaultColor)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FormatPathValue formats a path value with proper coloring
|
||||||
|
func FormatPathValue(path string) string {
|
||||||
|
return fmt.Sprintf("%s%s%s", PathColor, path, DefaultColor)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FormatErrorValue formats an error value with proper coloring
|
||||||
|
func FormatErrorValue(err error) string {
|
||||||
|
if err == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("%s%v%s", ErrorColor, err, DefaultColor)
|
||||||
|
}
|
95
main.go
95
main.go
@@ -33,7 +33,7 @@ func main() {
|
|||||||
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
|
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
|
||||||
logFile, err := os.Create("cln.log")
|
logFile, err := os.Create("cln.log")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error creating log file: %v", err)
|
LogError("Error creating log file: %v", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
logger := io.MultiWriter(os.Stdout, logFile)
|
logger := io.MultiWriter(os.Stdout, logFile)
|
||||||
@@ -48,36 +48,36 @@ func main() {
|
|||||||
// Check input sources in priority order
|
// Check input sources in priority order
|
||||||
switch {
|
switch {
|
||||||
case *recurse != "":
|
case *recurse != "":
|
||||||
log.Printf("Recurse: %s", *recurse)
|
LogInfo("Recurse: %s", *recurse)
|
||||||
go ReadFromFilesRecursively(*recurse, instructions, status)
|
go ReadFromFilesRecursively(*recurse, instructions, status)
|
||||||
|
|
||||||
case *file != "":
|
case *file != "":
|
||||||
log.Printf("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:
|
||||||
log.Printf("Reading from command line arguments")
|
LogInfo("Reading from command line arguments")
|
||||||
go ReadFromArgs(instructions, status)
|
go ReadFromArgs(instructions, status)
|
||||||
|
|
||||||
case IsPipeInput():
|
case IsPipeInput():
|
||||||
log.Printf("Reading from stdin pipe")
|
LogInfo("Reading from stdin pipe")
|
||||||
go ReadFromStdin(instructions, status)
|
go ReadFromStdin(instructions, status)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if _, err := os.Stat("sync.yaml"); err == nil {
|
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)
|
go ReadFromFile("sync.yaml", instructions, status, true)
|
||||||
} else if _, err := os.Stat("sync.yml"); err == nil {
|
} 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)
|
go ReadFromFile("sync.yml", instructions, status, true)
|
||||||
} else {
|
} else {
|
||||||
log.Printf("No input provided")
|
LogInfo("No input provided")
|
||||||
log.Printf("Provide input as: ")
|
LogInfo("Provide input as: ")
|
||||||
log.Printf("Arguments - %s <source>,<target>,<force?>", programName)
|
LogInfo("Arguments - %s <source>,<target>,<force?>", programName)
|
||||||
log.Printf("File - %s -f <file>", programName)
|
LogInfo("File - %s -f <file>", programName)
|
||||||
log.Printf("YAML File - %s -f <file.yaml>", programName)
|
LogInfo("YAML File - %s -f <file.yaml>", programName)
|
||||||
log.Printf("Folder (finding sync files in folder recursively) - %s -r <folder>", programName)
|
LogInfo("Folder (finding sync files in folder recursively) - %s -r <folder>", programName)
|
||||||
log.Printf("stdin - (cat <file> | %s)", programName)
|
LogInfo("stdin - (cat <file> | %s)", programName)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -89,7 +89,7 @@ func main() {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
LogError("%v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
@@ -99,26 +99,27 @@ func main() {
|
|||||||
for {
|
for {
|
||||||
instruction, ok := <-instructions
|
instruction, ok := <-instructions
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Printf("No more instructions to process")
|
LogInfo("No more instructions to process")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
log.Printf("Processing: %s", instruction.String())
|
LogInfo("Processing: %s", instruction.String())
|
||||||
status := make(chan error)
|
status := make(chan error)
|
||||||
go instruction.RunAsync(status)
|
go instruction.RunAsync(status)
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
err := <-status
|
err := <-status
|
||||||
if err != nil {
|
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)
|
atomic.AddInt32(&instructionsDone, 1)
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
log.Println("All done")
|
|
||||||
if instructionsDone == 0 {
|
if instructionsDone == 0 {
|
||||||
log.Printf("No instructions were processed")
|
LogInfo("No instructions were processed")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
LogInfo("All done")
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsPipeInput() bool {
|
func IsPipeInput() bool {
|
||||||
@@ -135,55 +136,61 @@ func ReadFromFilesRecursively(input string, output chan *LinkInstruction, status
|
|||||||
|
|
||||||
workdir, _ := os.Getwd()
|
workdir, _ := os.Getwd()
|
||||||
input = NormalizePath(input, workdir)
|
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)
|
files := make(chan string, 128)
|
||||||
recurseStatus := make(chan error)
|
fileStatus := make(chan error)
|
||||||
go GetSyncFilesRecursively(input, files, recurseStatus)
|
var wg sync.WaitGroup
|
||||||
|
go GetSyncFilesRecursively(input, files, fileStatus)
|
||||||
|
go func() {
|
||||||
|
wg.Wait()
|
||||||
|
close(files)
|
||||||
|
}()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
err, ok := <-recurseStatus
|
err, ok := <-fileStatus
|
||||||
if !ok {
|
if !ok {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if err != nil {
|
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
|
status <- err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
for {
|
for {
|
||||||
file, ok := <-files
|
file, ok := <-files
|
||||||
if !ok {
|
if !ok {
|
||||||
log.Printf("No more files to process")
|
LogInfo("No more files to process")
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
log.Println(file)
|
LogInfo(file)
|
||||||
file = NormalizePath(file, workdir)
|
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
|
// This "has" to be done because instructions are resolved in relation to cwd
|
||||||
fileDir := FileRegex.FindStringSubmatch(file)
|
fileDir := FileRegex.FindStringSubmatch(file)
|
||||||
if fileDir == nil {
|
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
|
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])
|
err := os.Chdir(fileDir[1])
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ReadFromFile(file, output, status, false)
|
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) {
|
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))
|
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
|
// Check if this is a YAML file
|
||||||
if IsYAMLFile(input) {
|
if IsYAMLFile(input) {
|
||||||
log.Printf("Parsing as YAML file")
|
LogInfo("Parsing as YAML file")
|
||||||
instructions, err := ParseYAMLFile(input, filepath.Dir(input))
|
instructions, err := ParseYAMLFile(input, filepath.Dir(input))
|
||||||
if err != nil {
|
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)
|
SourceColor, input, DefaultColor, ErrorColor, err, DefaultColor)
|
||||||
status <- err
|
status <- err
|
||||||
return
|
return
|
||||||
@@ -208,7 +215,7 @@ func ReadFromFile(input string, output chan *LinkInstruction, status chan error,
|
|||||||
|
|
||||||
for _, instruction := range instructions {
|
for _, instruction := range instructions {
|
||||||
instr := instruction // Create a copy to avoid reference issues
|
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
|
output <- &instr
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
@@ -220,11 +227,12 @@ func ReadFromArgs(output chan *LinkInstruction, status chan error) {
|
|||||||
defer close(status)
|
defer close(status)
|
||||||
|
|
||||||
workdir, _ := os.Getwd()
|
workdir, _ := os.Getwd()
|
||||||
log.Printf("Reading input from args")
|
LogInfo("Reading input from args")
|
||||||
for _, arg := range flag.Args() {
|
for _, arg := range flag.Args() {
|
||||||
instruction, err := ParseInstruction(arg, workdir)
|
instruction, err := ParseInstruction(arg, workdir)
|
||||||
if err != nil {
|
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
|
continue
|
||||||
}
|
}
|
||||||
output <- &instruction
|
output <- &instruction
|
||||||
@@ -236,20 +244,21 @@ func ReadFromStdin(output chan *LinkInstruction, status chan error) {
|
|||||||
defer close(status)
|
defer close(status)
|
||||||
|
|
||||||
workdir, _ := os.Getwd()
|
workdir, _ := os.Getwd()
|
||||||
log.Printf("Reading input from stdin")
|
LogInfo("Reading input from stdin")
|
||||||
|
|
||||||
scanner := bufio.NewScanner(os.Stdin)
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
instruction, err := ParseInstruction(line, workdir)
|
instruction, err := ParseInstruction(line, workdir)
|
||||||
if err != nil {
|
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
|
continue
|
||||||
}
|
}
|
||||||
output <- &instruction
|
output <- &instruction
|
||||||
}
|
}
|
||||||
if err := scanner.Err(); err != nil {
|
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
|
status <- err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
26
synclib.log
Normal file
26
synclib.log
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
18:24:37.967093 Using default sync.yaml file
|
||||||
|
18:24:37.967093 Input 'sync.yaml' is not absolute, prepending work dir '.'
|
||||||
|
18:24:37.967593 Reading input from file: [0;32mC:/Users/Administrator/Seafile/Projects-Go/GoProjects/synclib/sync.yaml[0m
|
||||||
|
18:24:37.967593 Parsing as YAML file
|
||||||
|
18:24:37.967593 [4;31mFailed to parse YAML file [0;35mC:/Users/Administrator/Seafile/Projects-Go/GoProjects/synclib/sync.yaml[0m: [4;31merror unmarshaling YAML: yaml: unmarshal errors:
|
||||||
|
line 1: cannot unmarshal !!seq into main.YAMLConfig[0m[0m
|
||||||
|
18:24:37.967593 No more instructions to process
|
||||||
|
18:24:37.968092 No instructions were processed
|
||||||
|
18:27:59.691333 Using default sync.yaml file
|
||||||
|
18:27:59.691333 Input 'sync.yaml' is not absolute, prepending work dir '.'
|
||||||
|
18:27:59.691834 Reading input from file: [0;32mC:/Users/Administrator/Seafile/Projects-Go/GoProjects/synclib/sync.yaml[0m
|
||||||
|
18:27:59.691834 Parsing as YAML file
|
||||||
|
18:27:59.692335 [0;35mExpanding wildcard source \* in YAML file C:/Users/Administrator/Seafile/Projects-Go/GoProjects/synclib/sync.yaml[0m
|
||||||
|
18:27:59.692335 Expanded wildcard source [0;35m\*[0m to 0 links
|
||||||
|
18:27:59.692836 Expanded wildcard source [0;35m\*[0m in YAML file [0;35mC:/Users/Administrator/Seafile/Projects-Go/GoProjects/synclib/sync.yaml[0m to 0 links
|
||||||
|
18:27:59.692836 No more instructions to process
|
||||||
|
18:27:59.692836 No instructions were processed
|
||||||
|
18:28:04.075821 Using default sync.yaml file
|
||||||
|
18:28:04.076320 Input 'sync.yaml' is not absolute, prepending work dir '.'
|
||||||
|
18:28:04.076320 Reading input from file: [0;32mC:/Users/Administrator/Seafile/Projects-Go/GoProjects/synclib/sync.yaml[0m
|
||||||
|
18:28:04.076320 Parsing as YAML file
|
||||||
|
18:28:04.076320 [0;35mExpanding wildcard source \* in YAML file C:/Users/Administrator/Seafile/Projects-Go/GoProjects/synclib/sync.yaml[0m
|
||||||
|
18:28:04.076821 Expanded wildcard source [0;35m\*[0m to 0 links
|
||||||
|
18:28:04.076821 Expanded wildcard source [0;35m\*[0m in YAML file [0;35mC:/Users/Administrator/Seafile/Projects-Go/GoProjects/synclib/sync.yaml[0m to 0 links
|
||||||
|
18:28:04.076821 No more instructions to process
|
||||||
|
18:28:04.076821 No instructions were processed
|
11
util.go
11
util.go
@@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -33,12 +32,12 @@ func NormalizePath(input, workdir string) string {
|
|||||||
input = strings.ReplaceAll(input, "\"", "")
|
input = strings.ReplaceAll(input, "\"", "")
|
||||||
|
|
||||||
if !filepath.IsAbs(input) {
|
if !filepath.IsAbs(input) {
|
||||||
log.Printf("Input '%s' is not absolute, prepending work dir '%s'", input, workdir)
|
LogInfo("Input '%s' is not absolute, prepending work dir '%s'", input, workdir)
|
||||||
var err error
|
var err error
|
||||||
input = filepath.Join(workdir, input)
|
input = filepath.Join(workdir, input)
|
||||||
input, err = filepath.Abs(input)
|
input, err = filepath.Abs(input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Failed to get absolute path for %s%s%s: %s%+v%s", SourceColor, input, DefaultColor, ErrorColor, err, DefaultColor)
|
LogError("Failed to get absolute path for %s: %v", FormatSourcePath(input), err)
|
||||||
return input
|
return input
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -157,15 +156,17 @@ func GetSyncFilesRecursively(input string, output chan string, status chan error
|
|||||||
|
|
||||||
<-allDone
|
<-allDone
|
||||||
|
|
||||||
log.Printf("Files processed: %d; Folders processed: %d",
|
if atomic.LoadInt32(&filesProcessed) > 0 {
|
||||||
|
LogInfo("Files processed: %d; Folders processed: %d",
|
||||||
atomic.LoadInt32(&filesProcessed),
|
atomic.LoadInt32(&filesProcessed),
|
||||||
atomic.LoadInt32(&foldersProcessed))
|
atomic.LoadInt32(&foldersProcessed))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func processDirectory(directory string, directories chan<- string, output chan<- string, filesProcessed *int32) {
|
func processDirectory(directory string, directories chan<- string, output chan<- string, filesProcessed *int32) {
|
||||||
files, err := os.ReadDir(directory)
|
files, err := os.ReadDir(directory)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Error reading directory %s: %+v", directory, err)
|
LogError("Error reading directory %s: %v", directory, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user