Implement yaml format
This commit is contained in:
46
main.go
46
main.go
@@ -20,8 +20,8 @@ const ImportantColor = BRed
|
||||
const DefaultColor = White
|
||||
const PathColor = Green
|
||||
|
||||
var DirRegex, _ = regexp.Compile(`^(.+?)[/\\]sync$`)
|
||||
var FileRegex, _ = regexp.Compile(`^sync$`)
|
||||
var DirRegex, _ = regexp.Compile(`^(.+?)[/\\]sync(?:\.ya?ml)?$`)
|
||||
var FileRegex, _ = regexp.Compile(`^sync(?:\.ya?ml)?$`)
|
||||
var programName = os.Args[0]
|
||||
|
||||
func main() {
|
||||
@@ -51,28 +51,35 @@ func main() {
|
||||
case *recurse != "":
|
||||
log.Printf("Recurse: %s", *recurse)
|
||||
go ReadFromFilesRecursively(*recurse, instructions, status)
|
||||
|
||||
|
||||
case *file != "":
|
||||
log.Printf("File: %s", *file)
|
||||
go ReadFromFile(*file, instructions, status, true)
|
||||
|
||||
|
||||
case len(flag.Args()) > 0:
|
||||
log.Printf("Reading from command line arguments")
|
||||
go ReadFromArgs(instructions, status)
|
||||
|
||||
|
||||
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 if _, err := os.Stat("sync.yaml"); err == nil {
|
||||
log.Printf("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")
|
||||
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)
|
||||
os.Exit(1)
|
||||
@@ -191,9 +198,31 @@ 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)
|
||||
|
||||
// Check if this is a YAML file
|
||||
if IsYAMLFile(input) {
|
||||
log.Printf("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",
|
||||
SourceColor, input, DefaultColor, ErrorColor, err, DefaultColor)
|
||||
status <- err
|
||||
return
|
||||
}
|
||||
|
||||
for _, instruction := range instructions {
|
||||
instr := instruction // Create a copy to avoid reference issues
|
||||
log.Printf("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)
|
||||
log.Fatalf("Failed to open file %s%s%s: %s%+v%s",
|
||||
SourceColor, input, DefaultColor, ErrorColor, err, DefaultColor)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
@@ -203,7 +232,8 @@ func ReadFromFile(input string, output chan *LinkInstruction, status chan error,
|
||||
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)
|
||||
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())
|
||||
|
Reference in New Issue
Block a user