Compare commits
15 Commits
Author | SHA1 | Date | |
---|---|---|---|
ff76a5399c | |||
3f0791466b | |||
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
|
.qodo
|
||||||
*.log
|
*.log
|
||||||
*.out
|
*.out
|
||||||
|
test_temp
|
||||||
|
4
build.sh
4
build.sh
@@ -1,2 +1,2 @@
|
|||||||
GOOS=windows GOARCH=amd64 go build -o cln.exe .
|
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o cln.exe .
|
||||||
GOOS=linux GOARCH=amd64 go build -o cln .
|
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o cln .
|
||||||
|
4
go.mod
4
go.mod
@@ -1,10 +1,11 @@
|
|||||||
module cln
|
module cln
|
||||||
|
|
||||||
go 1.21.7
|
go 1.23.6
|
||||||
|
|
||||||
require gopkg.in/yaml.v3 v3.0.1
|
require gopkg.in/yaml.v3 v3.0.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
git.site.quack-lab.dev/dave/cyutils v1.4.0
|
||||||
github.com/bmatcuk/doublestar/v4 v4.8.1
|
github.com/bmatcuk/doublestar/v4 v4.8.1
|
||||||
github.com/stretchr/testify v1.11.1
|
github.com/stretchr/testify v1.11.1
|
||||||
)
|
)
|
||||||
@@ -12,4 +13,5 @@ require (
|
|||||||
require (
|
require (
|
||||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
github.com/pmezard/go-difflib v1.0.0 // 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 h1:54Bopc5c2cAvhLRAzqOGCYHYyhcDHsFF4wWIR5wKP38=
|
||||||
github.com/bmatcuk/doublestar/v4 v4.8.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
|
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=
|
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/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 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
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 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
183
instruction.go
183
instruction.go
@@ -270,48 +270,24 @@ func ParseYAMLFile(filename, workdir string) ([]LinkInstruction, error) {
|
|||||||
return nil, fmt.Errorf("error reading YAML file: %w", err)
|
return nil, fmt.Errorf("error reading YAML file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// First try to parse as a YAMLConfig with links and from fields
|
// Parse as a direct list of instructions
|
||||||
var config YAMLConfig
|
var instructions []LinkInstruction
|
||||||
err = yaml.Unmarshal(data, &config)
|
err = yaml.Unmarshal(data, &instructions)
|
||||||
LogInfo("First parsing attempt: err=%v, links=%d, from=%d", err, len(config.Links), len(config.From))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If that fails, try parsing as a direct list of instructions
|
return nil, fmt.Errorf("error parsing YAML: %w", err)
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
expanded := []LinkInstruction{}
|
// Preprocess instructions: expand globs and from references
|
||||||
for _, link := range config.Links {
|
// Create a new visited map for this file
|
||||||
LogSource("Expanding pattern source %s in YAML file %s", link.Source, filename)
|
visited := make(map[string]bool)
|
||||||
newlinks, err := ExpandPattern(link.Source, workdir, link.Target)
|
processedInstructions, err := preprocessInstructions(instructions, filename, workdir, visited)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error expanding pattern: %w", err)
|
return nil, 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...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range expanded {
|
// Final processing: normalize paths and set defaults
|
||||||
link := &expanded[i]
|
for i := range processedInstructions {
|
||||||
|
link := &processedInstructions[i]
|
||||||
link.Tidy()
|
link.Tidy()
|
||||||
link.Source, _ = ConvertHome(link.Source)
|
link.Source, _ = ConvertHome(link.Source)
|
||||||
link.Target, _ = ConvertHome(link.Target)
|
link.Target, _ = ConvertHome(link.Target)
|
||||||
@@ -324,7 +300,79 @@ 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) {
|
||||||
|
// First convert home directory if it starts with ~
|
||||||
|
fromPath, err := ConvertHome(fromFile)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error converting home directory: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Convert relative paths to absolute paths based on the current file's directory
|
||||||
|
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
|
// ParseYAMLFileRecursive parses a YAML file and recursively processes any "From" references
|
||||||
@@ -348,50 +396,41 @@ func parseYAMLFileRecursive(filename, workdir string, visited map[string]bool) (
|
|||||||
visited[normalizedFilename] = true
|
visited[normalizedFilename] = true
|
||||||
defer delete(visited, normalizedFilename)
|
defer delete(visited, normalizedFilename)
|
||||||
|
|
||||||
// Parse the current file
|
// Parse the current file and preprocess it with cycle detection
|
||||||
instructions, err := ParseYAMLFile(filename, workdir)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// Read the file to check for "From" references
|
|
||||||
data, err := os.ReadFile(filename)
|
data, err := os.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error reading YAML file: %w", err)
|
return nil, fmt.Errorf("error reading YAML file: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var config YAMLConfig
|
// Parse as a direct list of instructions
|
||||||
err = yaml.Unmarshal(data, &config)
|
var instructions []LinkInstruction
|
||||||
|
err = yaml.Unmarshal(data, &instructions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// If parsing as YAMLConfig fails, there are no "From" references to process
|
return nil, fmt.Errorf("error parsing YAML: %w", err)
|
||||||
return instructions, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process "From" references
|
// Preprocess instructions: expand globs and from references
|
||||||
for _, fromFile := range config.From {
|
processedInstructions, err := preprocessInstructions(instructions, filename, workdir, visited)
|
||||||
// Convert relative paths to absolute paths based on the current file's directory
|
if err != nil {
|
||||||
fromPath := fromFile
|
return nil, err
|
||||||
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...)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
func ExpandPattern(source, workdir, target string) (links []LinkInstruction, err error) {
|
||||||
|
2546
instruction_test.go
2546
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"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
utils "git.site.quack-lab.dev/dave/cyutils"
|
||||||
)
|
)
|
||||||
|
|
||||||
const deliminer = ","
|
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 {
|
func processInstructions(instructions chan *LinkInstruction) int32 {
|
||||||
var instructionsDone int32 = 0
|
var instructionsDone int32 = 0
|
||||||
var wg sync.WaitGroup
|
|
||||||
|
// Collect all instructions first
|
||||||
|
var allInstructions []*LinkInstruction
|
||||||
for {
|
for {
|
||||||
instruction, ok := <-instructions
|
instruction, ok := <-instructions
|
||||||
if !ok {
|
if !ok {
|
||||||
LogInfo("No more instructions to process")
|
LogInfo("No more instructions to process")
|
||||||
break
|
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())
|
LogInfo("Processing: %s", instruction.String())
|
||||||
status := make(chan error)
|
status := make(chan error)
|
||||||
go instruction.RunAsync(status)
|
go instruction.RunAsync(status)
|
||||||
wg.Add(1)
|
|
||||||
err := <-status
|
err := <-status
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogError("Failed processing instruction: %v", err)
|
LogError("Failed processing instruction: %v", err)
|
||||||
|
} else {
|
||||||
|
atomic.AddInt32(&instructionsDone, 1)
|
||||||
}
|
}
|
||||||
atomic.AddInt32(&instructionsDone, 1)
|
})
|
||||||
wg.Done()
|
|
||||||
}
|
|
||||||
wg.Wait()
|
|
||||||
return instructionsDone
|
return instructionsDone
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user