Files
synclib/main.go

455 lines
11 KiB
Go

package main
import (
"bufio"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"regexp"
"runtime/debug"
"strings"
"sync/atomic"
utils "git.site.quack-lab.dev/dave/cyutils"
)
const deliminer = ","
const SourceColor = Purple
const TargetColor = Yellow
const ErrorColor = Red
const ImportantColor = BRed
const DefaultColor = Reset
const PathColor = Green
var (
programName = os.Args[0]
undo = false
version = "dev"
)
func main() {
recurse := flag.String("r", "", "recurse into directories")
file := flag.String("f", "", "file to read instructions from")
debug := flag.Bool("d", false, "debug")
undoF := flag.Bool("u", false, "undo")
versionFlag := flag.Bool("v", false, "print version and exit")
dryRun := flag.Bool("n", false, "dry run (no filesystem changes)")
flag.Parse()
undo = *undoF
if *versionFlag {
fmt.Println(getVersionString())
return
}
setupLogging(*debug)
if *dryRun {
filesystem = NewDryRunFileSystem()
LogInfo("Dry run mode enabled - no filesystem changes will be made")
} else {
filesystem = NewRealFileSystem()
}
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")
printSummary(filesystem)
os.Exit(1)
}
LogInfo("All done")
printSummary(filesystem)
}
// setupLogging configures logging based on debug flag
func setupLogging(debug bool) {
if debug {
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
logFile, err := os.Create(programName + ".log")
if err != nil {
LogError("Error creating log file: %v", err)
os.Exit(1)
}
logger := io.MultiWriter(os.Stdout, logFile)
log.SetOutput(logger)
} else {
log.SetFlags(log.Lmicroseconds)
}
}
// startInputSource determines and starts the appropriate input source
func startInputSource(recurse, file string, instructions chan *LinkInstruction, status chan error) {
// Check input sources in priority order
switch {
case recurse != "":
LogInfo("Recurse: %s", recurse)
go ReadFromFilesRecursively(recurse, instructions, status)
case file != "":
LogInfo("File: %s", file)
go ReadFromFile(file, instructions, status, true)
case len(flag.Args()) > 0:
LogInfo("Reading from command line arguments")
go ReadFromArgs(instructions, status)
// case IsPipeInput():
// LogInfo("Reading from stdin pipe")
// go ReadFromStdin(instructions, status)
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 {
LogInfo("Using default sync file")
go ReadFromFile("sync", instructions, status, true)
} else if _, err := os.Stat("sync.yaml"); err == nil {
LogInfo("Using default sync.yaml file")
go ReadFromFile("sync.yaml", instructions, status, true)
} else if _, err := os.Stat("sync.yml"); err == nil {
LogInfo("Using default sync.yml file")
go ReadFromFile("sync.yml", instructions, status, true)
} else {
showUsageAndExit()
}
}
// showUsageAndExit displays usage information and exits
func showUsageAndExit() {
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)
}
// handleStatusErrors processes status channel errors
func handleStatusErrors(status chan error) {
for {
err, ok := <-status
if !ok {
break
}
if err != nil {
LogError("%v", err)
}
}
}
// processInstructions processes all instructions from the channel using parallel workers
func processInstructions(instructions chan *LinkInstruction) int32 {
var instructionsDone int32 = 0
// Collect all instructions first
var allInstructions []*LinkInstruction
for {
instruction, ok := <-instructions
if !ok {
LogInfo("No more instructions to process")
break
}
allInstructions = append(allInstructions, instruction)
}
// Process instructions in parallel using cyutils.WithWorkers
// Let the library handle worker count - use 4 workers as a reasonable default
utils.WithWorkers(4, allInstructions, func(workerID int, _ int, instruction *LinkInstruction) {
LogInfo("Processing: %s", instruction.String())
status := make(chan error)
go instruction.RunAsync(status)
err := <-status
if err != nil {
LogError("Failed processing instruction: %v", err)
} else {
atomic.AddInt32(&instructionsDone, 1)
}
})
return instructionsDone
}
func printSummary(fs FileSystem) {
records := fs.SummaryRecords()
if len(records) == 0 {
return
}
header := []string{"RESULT", "OPERATION", "SOURCE", "TARGET", "DETAIL"}
widths := make([]int, len(header))
for i, h := range header {
widths[i] = len(h)
}
type row struct {
values []string
err error
}
rows := make([]row, len(records))
for i, record := range records {
values := []string{
record.resultLabel(),
record.kindLabel(),
FormatSourcePath(record.source),
FormatTargetPath(record.target),
record.detail(),
}
rows[i] = row{values: values, err: record.err}
for j, val := range values {
if l := visibleLength(val); l > widths[j] {
widths[j] = l
}
}
}
LogInfo("Summary:")
var lineBuilder strings.Builder
writeSummaryRow(&lineBuilder, header, widths)
for _, line := range strings.Split(strings.TrimRight(lineBuilder.String(), "\n"), "\n") {
LogInfo("%s", line)
}
for _, r := range rows {
lineBuilder.Reset()
writeSummaryRow(&lineBuilder, r.values, widths)
line := strings.TrimRight(lineBuilder.String(), "\n")
LogInfo("%s", line)
}
}
func writeSummaryRow(b *strings.Builder, cols []string, widths []int) {
for i, val := range cols {
if val == "" {
val = "-"
}
b.WriteString(val)
pad := widths[i] - visibleLength(val)
if pad < 0 {
pad = 0
}
if i < len(cols)-1 {
b.WriteString(strings.Repeat(" ", pad+2))
}
}
b.WriteByte('\n')
}
var ansiRegexp = regexp.MustCompile(`\x1b\[[0-9;]*m`)
func visibleLength(s string) int {
return len(ansiRegexp.ReplaceAllString(s, ""))
}
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)
workdir, _ := os.Getwd()
input = NormalizePath(input, workdir)
LogInfo("Reading input from files recursively starting in %s", FormatPathValue(input))
files := make(chan string, 128)
fileStatus := make(chan error)
go GetSyncFilesRecursively(input, files, fileStatus)
// Collect all files first
var syncFiles []string
for {
file, ok := <-files
if !ok {
break
}
syncFiles = append(syncFiles, file)
}
// Check for errors from file search
for {
err, ok := <-fileStatus
if !ok {
break
}
if err != nil {
LogError("Failed to get sync files recursively: %v", err)
status <- err
}
}
// Process each file
for _, file := range syncFiles {
file = NormalizePath(file, workdir)
LogInfo("Processing file: %s", FormatPathValue(file))
// Change to the directory containing the sync file
fileDir := filepath.Dir(file)
originalDir, _ := os.Getwd()
err := os.Chdir(fileDir)
if err != nil {
LogError("Failed to change directory to %s: %v", FormatSourcePath(fileDir), err)
continue
}
// Read and process the file
ReadFromFile(file, output, status, false)
// Return to original directory
os.Chdir(originalDir)
}
}
func getVersionString() string {
if version != "" && version != "dev" {
return version
}
if info, ok := debug.ReadBuildInfo(); ok {
if info.Main.Version != "" && info.Main.Version != "(devel)" {
return info.Main.Version
}
var revision, modified, vcsTime string
for _, setting := range info.Settings {
switch setting.Key {
case "vcs.revision":
revision = setting.Value
case "vcs.modified":
modified = setting.Value
case "vcs.time":
vcsTime = setting.Value
}
}
if revision != "" {
if len(revision) > 7 {
revision = revision[:7]
}
if modified == "true" {
revision += "-dirty"
}
if vcsTime != "" {
revision += " (" + vcsTime + ")"
}
return revision
}
}
return "dev"
}
func ReadFromFile(input string, output chan *LinkInstruction, status chan error, doclose bool) {
if doclose {
defer close(output)
defer close(status)
}
input = NormalizePath(input, filepath.Dir(input))
LogInfo("Reading input from file: %s", FormatPathValue(input))
// Check if this is a YAML file
if IsYAMLFile(input) {
LogInfo("Parsing as YAML file")
instructions, err := ParseYAMLFileRecursive(input, filepath.Dir(input))
if err != nil {
LogError("Failed to parse YAML file %s: %v",
FormatSourcePath(input), err)
status <- err
return
}
for _, instruction := range instructions {
instr := instruction // Create a copy to avoid reference issues
LogInfo("Read YAML instruction: %s", instr.String())
output <- &instr
}
return
}
// Handle CSV format (legacy)
file, err := os.Open(input)
if err != nil {
log.Fatalf("Failed to open file %s%s%s: %s%+v%s",
SourceColor, input, DefaultColor, ErrorColor, err, DefaultColor)
return
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
instruction, err := ParseInstruction(line, filepath.Dir(input))
if err != nil {
log.Printf("Error parsing line: %s'%s'%s, error: %s%+v%s",
SourceColor, line, DefaultColor, ErrorColor, err, DefaultColor)
continue
}
log.Printf("Read instruction: %s", instruction.String())
output <- &instruction
}
}
func ReadFromArgs(output chan *LinkInstruction, status chan error) {
defer close(output)
defer close(status)
workdir, _ := os.Getwd()
LogInfo("Reading input from args")
for _, arg := range flag.Args() {
instruction, err := ParseInstruction(arg, workdir)
if err != nil {
LogError("Error parsing arg '%s': %v", arg, err)
continue
}
output <- &instruction
}
}
func ReadFromStdin(output chan *LinkInstruction, status chan error) {
defer close(output)
defer close(status)
workdir, _ := os.Getwd()
LogInfo("Reading input from stdin")
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
instruction, err := ParseInstruction(line, workdir)
if err != nil {
LogError("Error parsing line '%s': %v", line, err)
continue
}
output <- &instruction
}
if err := scanner.Err(); err != nil {
LogError("Error reading from stdin: %v", err)
status <- err
return
}
}