Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
dc5eb9cb80 | |||
25a8e2b65a | |||
59faaa181d | |||
7bff91679d | |||
cfa7fc73c9 | |||
a568a736aa | |||
db72688aa2 | |||
05082d8ff3 | |||
a4f90c2bc8 | |||
bec5b3cb9c | |||
018c0797f5 | |||
a7d5317114 | |||
89e29eacee |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -5,3 +5,4 @@ cln.log
|
||||
.qodo
|
||||
*.log
|
||||
*.out
|
||||
test_temp
|
||||
|
4
build.sh
4
build.sh
@@ -1,2 +1,2 @@
|
||||
GOOS=windows GOARCH=amd64 go build -o cln.exe .
|
||||
GOOS=linux GOARCH=amd64 go build -o cln .
|
||||
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o cln.exe .
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o cln .
|
||||
|
4
go.mod
4
go.mod
@@ -1,10 +1,11 @@
|
||||
module cln
|
||||
|
||||
go 1.21.7
|
||||
go 1.23.6
|
||||
|
||||
require gopkg.in/yaml.v3 v3.0.1
|
||||
|
||||
require (
|
||||
git.site.quack-lab.dev/dave/cyutils v1.4.0
|
||||
github.com/bmatcuk/doublestar/v4 v4.8.1
|
||||
github.com/stretchr/testify v1.11.1
|
||||
)
|
||||
@@ -12,4 +13,5 @@ require (
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
golang.org/x/time v0.12.0 // indirect
|
||||
)
|
||||
|
4
go.sum
4
go.sum
@@ -1,3 +1,5 @@
|
||||
git.site.quack-lab.dev/dave/cyutils v1.4.0 h1:/Xo3QfLIFNab+axHneWmUK4MyfuObl+qq8whF9vTQpk=
|
||||
git.site.quack-lab.dev/dave/cyutils v1.4.0/go.mod h1:fBjALu2Cp2u2bDr+E4zbGVMBeIgFzROg+4TCcTNAiQU=
|
||||
github.com/bmatcuk/doublestar/v4 v4.8.1 h1:54Bopc5c2cAvhLRAzqOGCYHYyhcDHsFF4wWIR5wKP38=
|
||||
github.com/bmatcuk/doublestar/v4 v4.8.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
@@ -6,6 +8,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE=
|
||||
golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
|
178
instruction.go
178
instruction.go
@@ -270,48 +270,24 @@ func ParseYAMLFile(filename, workdir string) ([]LinkInstruction, error) {
|
||||
return nil, fmt.Errorf("error reading YAML file: %w", err)
|
||||
}
|
||||
|
||||
// First try to parse as a YAMLConfig with links and from fields
|
||||
var config YAMLConfig
|
||||
err = yaml.Unmarshal(data, &config)
|
||||
LogInfo("First parsing attempt: err=%v, links=%d, from=%d", err, len(config.Links), len(config.From))
|
||||
// Parse as a direct list of instructions
|
||||
var instructions []LinkInstruction
|
||||
err = yaml.Unmarshal(data, &instructions)
|
||||
if err != nil {
|
||||
// If that fails, try parsing as a direct list of instructions
|
||||
var instructions []LinkInstruction
|
||||
err = yaml.Unmarshal(data, &instructions)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing YAML: %w", err)
|
||||
}
|
||||
// Filter out invalid instructions (empty source)
|
||||
validInstructions := []LinkInstruction{}
|
||||
for _, instr := range instructions {
|
||||
if instr.Source != "" {
|
||||
validInstructions = append(validInstructions, instr)
|
||||
}
|
||||
}
|
||||
config.Links = validInstructions
|
||||
return nil, fmt.Errorf("error parsing YAML: %w", err)
|
||||
}
|
||||
|
||||
expanded := []LinkInstruction{}
|
||||
for _, link := range config.Links {
|
||||
LogSource("Expanding pattern source %s in YAML file %s", link.Source, filename)
|
||||
newlinks, err := ExpandPattern(link.Source, workdir, link.Target)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error expanding pattern: %w", err)
|
||||
}
|
||||
// "Clone" the original link instruction for each expanded link
|
||||
for i := range newlinks {
|
||||
newlinks[i].Delete = link.Delete
|
||||
newlinks[i].Hard = link.Hard
|
||||
newlinks[i].Force = link.Force
|
||||
}
|
||||
LogInfo("Expanded pattern %s in YAML file %s to %d links",
|
||||
FormatSourcePath(link.Source), FormatSourcePath(filename), len(newlinks))
|
||||
expanded = append(expanded, newlinks...)
|
||||
// Preprocess instructions: expand globs and from references
|
||||
// Create a new visited map for this file
|
||||
visited := make(map[string]bool)
|
||||
processedInstructions, err := preprocessInstructions(instructions, filename, workdir, visited)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := range expanded {
|
||||
link := &expanded[i]
|
||||
|
||||
// Final processing: normalize paths and set defaults
|
||||
for i := range processedInstructions {
|
||||
link := &processedInstructions[i]
|
||||
link.Tidy()
|
||||
link.Source, _ = ConvertHome(link.Source)
|
||||
link.Target, _ = ConvertHome(link.Target)
|
||||
@@ -324,7 +300,74 @@ func ParseYAMLFile(filename, workdir string) ([]LinkInstruction, error) {
|
||||
}
|
||||
}
|
||||
|
||||
return expanded, nil
|
||||
return processedInstructions, nil
|
||||
}
|
||||
|
||||
// preprocessInstructions handles glob expansion and from references
|
||||
func preprocessInstructions(instructions []LinkInstruction, filename, workdir string, visited map[string]bool) ([]LinkInstruction, error) {
|
||||
var result []LinkInstruction
|
||||
|
||||
for _, instr := range instructions {
|
||||
if instr.Source == "" {
|
||||
continue // Skip invalid instructions
|
||||
}
|
||||
|
||||
if instr.Target == "" {
|
||||
// This is a from reference - load the referenced file
|
||||
fromInstructions, err := loadFromReference(instr.Source, filename, workdir, visited)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error loading from reference %s: %w", instr.Source, err)
|
||||
}
|
||||
result = append(result, fromInstructions...)
|
||||
} else {
|
||||
// This is a regular instruction - expand globs if needed
|
||||
expandedInstructions, err := expandGlobs(instr, filename, workdir)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error expanding globs for %s: %w", instr.Source, err)
|
||||
}
|
||||
result = append(result, expandedInstructions...)
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// loadFromReference loads instructions from a referenced file
|
||||
func loadFromReference(fromFile, currentFile, workdir string, visited map[string]bool) ([]LinkInstruction, error) {
|
||||
// Convert relative paths to absolute paths based on the current file's directory
|
||||
fromPath := fromFile
|
||||
if !filepath.IsAbs(fromPath) {
|
||||
currentDir := filepath.Dir(currentFile)
|
||||
fromPath = filepath.Join(currentDir, fromPath)
|
||||
}
|
||||
|
||||
// Normalize the path
|
||||
fromPath = filepath.Clean(fromPath)
|
||||
|
||||
// Recursively parse the referenced file with cycle detection
|
||||
fromWorkdir := filepath.Dir(fromPath)
|
||||
return parseYAMLFileRecursive(fromPath, fromWorkdir, visited)
|
||||
}
|
||||
|
||||
// expandGlobs expands glob patterns in a single instruction
|
||||
func expandGlobs(instr LinkInstruction, filename, workdir string) ([]LinkInstruction, error) {
|
||||
LogSource("Expanding pattern source %s in YAML file %s", instr.Source, filename)
|
||||
newlinks, err := ExpandPattern(instr.Source, workdir, instr.Target)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Clone the original instruction properties for each expanded link
|
||||
for i := range newlinks {
|
||||
newlinks[i].Delete = instr.Delete
|
||||
newlinks[i].Hard = instr.Hard
|
||||
newlinks[i].Force = instr.Force
|
||||
}
|
||||
|
||||
LogInfo("Expanded pattern %s in YAML file %s to %d links",
|
||||
FormatSourcePath(instr.Source), FormatSourcePath(filename), len(newlinks))
|
||||
|
||||
return newlinks, nil
|
||||
}
|
||||
|
||||
// ParseYAMLFileRecursive parses a YAML file and recursively processes any "From" references
|
||||
@@ -348,50 +391,41 @@ func parseYAMLFileRecursive(filename, workdir string, visited map[string]bool) (
|
||||
visited[normalizedFilename] = true
|
||||
defer delete(visited, normalizedFilename)
|
||||
|
||||
// Parse the current file
|
||||
instructions, err := ParseYAMLFile(filename, workdir)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Read the file to check for "From" references
|
||||
// Parse the current file and preprocess it with cycle detection
|
||||
data, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading YAML file: %w", err)
|
||||
}
|
||||
|
||||
var config YAMLConfig
|
||||
err = yaml.Unmarshal(data, &config)
|
||||
// Parse as a direct list of instructions
|
||||
var instructions []LinkInstruction
|
||||
err = yaml.Unmarshal(data, &instructions)
|
||||
if err != nil {
|
||||
// If parsing as YAMLConfig fails, there are no "From" references to process
|
||||
return instructions, nil
|
||||
return nil, fmt.Errorf("error parsing YAML: %w", err)
|
||||
}
|
||||
|
||||
// Process "From" references
|
||||
for _, fromFile := range config.From {
|
||||
// Convert relative paths to absolute paths based on the current file's directory
|
||||
fromPath := fromFile
|
||||
if !filepath.IsAbs(fromPath) {
|
||||
currentDir := filepath.Dir(filename)
|
||||
fromPath = filepath.Join(currentDir, fromPath)
|
||||
}
|
||||
|
||||
// Normalize the path
|
||||
fromPath = filepath.Clean(fromPath)
|
||||
|
||||
// Recursively parse the referenced file
|
||||
// Use the directory of the referenced file as the workdir for pattern expansion
|
||||
fromWorkdir := filepath.Dir(fromPath)
|
||||
fromInstructions, err := parseYAMLFileRecursive(fromPath, fromWorkdir, visited)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error parsing referenced file %s: %w", fromFile, err)
|
||||
}
|
||||
|
||||
// Append the instructions from the referenced file
|
||||
instructions = append(instructions, fromInstructions...)
|
||||
// Preprocess instructions: expand globs and from references
|
||||
processedInstructions, err := preprocessInstructions(instructions, filename, workdir, visited)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return instructions, nil
|
||||
// Final processing: normalize paths and set defaults
|
||||
for i := range processedInstructions {
|
||||
link := &processedInstructions[i]
|
||||
link.Tidy()
|
||||
link.Source, _ = ConvertHome(link.Source)
|
||||
link.Target, _ = ConvertHome(link.Target)
|
||||
link.Source = NormalizePath(link.Source, workdir)
|
||||
link.Target = NormalizePath(link.Target, workdir)
|
||||
|
||||
// If Delete is true, Force must also be true
|
||||
if link.Delete {
|
||||
link.Force = true
|
||||
}
|
||||
}
|
||||
|
||||
return processedInstructions, nil
|
||||
}
|
||||
|
||||
func ExpandPattern(source, workdir, target string) (links []LinkInstruction, err error) {
|
||||
|
2430
instruction_test.go
2430
instruction_test.go
File diff suppressed because it is too large
Load Diff
23
main.go
23
main.go
@@ -7,8 +7,8 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
utils "git.site.quack-lab.dev/dave/cyutils"
|
||||
)
|
||||
|
||||
const deliminer = ","
|
||||
@@ -130,28 +130,35 @@ func handleStatusErrors(status chan error) {
|
||||
}
|
||||
}
|
||||
|
||||
// processInstructions processes all instructions from the channel
|
||||
// processInstructions processes all instructions from the channel using parallel workers
|
||||
func processInstructions(instructions chan *LinkInstruction) int32 {
|
||||
var instructionsDone int32 = 0
|
||||
var wg sync.WaitGroup
|
||||
|
||||
// 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)
|
||||
wg.Add(1)
|
||||
err := <-status
|
||||
if err != nil {
|
||||
LogError("Failed processing instruction: %v", err)
|
||||
} else {
|
||||
atomic.AddInt32(&instructionsDone, 1)
|
||||
}
|
||||
atomic.AddInt32(&instructionsDone, 1)
|
||||
wg.Done()
|
||||
}
|
||||
wg.Wait()
|
||||
})
|
||||
|
||||
return instructionsDone
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user