Compare commits
4 Commits
29bfa2d776
...
v1.3.0
Author | SHA1 | Date | |
---|---|---|---|
eb81ec4162 | |||
21d7f56ccf | |||
8653191df2 | |||
9da47ce0cf |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,3 +4,4 @@ cln
|
|||||||
cln.log
|
cln.log
|
||||||
.qodo
|
.qodo
|
||||||
*.log
|
*.log
|
||||||
|
*.out
|
||||||
|
10
go.mod
10
go.mod
@@ -4,4 +4,12 @@ go 1.21.7
|
|||||||
|
|
||||||
require gopkg.in/yaml.v3 v3.0.1
|
require gopkg.in/yaml.v3 v3.0.1
|
||||||
|
|
||||||
require github.com/bmatcuk/doublestar/v4 v4.8.1
|
require (
|
||||||
|
github.com/bmatcuk/doublestar/v4 v4.8.1
|
||||||
|
github.com/stretchr/testify v1.11.1
|
||||||
|
)
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||||
|
)
|
||||||
|
6
go.sum
6
go.sum
@@ -1,5 +1,11 @@
|
|||||||
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/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
|
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=
|
||||||
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=
|
||||||
|
@@ -20,6 +20,7 @@ type LinkInstruction struct {
|
|||||||
|
|
||||||
type YAMLConfig struct {
|
type YAMLConfig struct {
|
||||||
Links []LinkInstruction `yaml:"links"`
|
Links []LinkInstruction `yaml:"links"`
|
||||||
|
From []string `yaml:"from,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (instruction *LinkInstruction) Tidy() {
|
func (instruction *LinkInstruction) Tidy() {
|
||||||
@@ -318,9 +319,76 @@ func ParseYAMLFile(filename, workdir string) ([]LinkInstruction, error) {
|
|||||||
return expanded, nil
|
return expanded, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ParseYAMLFileRecursive parses a YAML file and recursively processes any "From" references
|
||||||
|
func ParseYAMLFileRecursive(filename, workdir string) ([]LinkInstruction, error) {
|
||||||
|
visited := make(map[string]bool)
|
||||||
|
return parseYAMLFileRecursive(filename, workdir, visited)
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseYAMLFileRecursive is the internal recursive function that tracks visited files to prevent cycles
|
||||||
|
func parseYAMLFileRecursive(filename, workdir string, visited map[string]bool) ([]LinkInstruction, error) {
|
||||||
|
// Normalize the filename to prevent cycles with different path representations
|
||||||
|
normalizedFilename, err := filepath.Abs(filename)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error normalizing filename: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for cycles
|
||||||
|
if visited[normalizedFilename] {
|
||||||
|
return nil, fmt.Errorf("circular reference detected: %s", filename)
|
||||||
|
}
|
||||||
|
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
|
||||||
|
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)
|
||||||
|
if err != nil {
|
||||||
|
// If parsing as YAMLConfig fails, there are no "From" references to process
|
||||||
|
return instructions, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return instructions, nil
|
||||||
|
}
|
||||||
|
|
||||||
func ExpandPattern(source, workdir, target string) (links []LinkInstruction, err error) {
|
func ExpandPattern(source, workdir, target string) (links []LinkInstruction, err error) {
|
||||||
static, pattern := doublestar.SplitPattern(source)
|
static, pattern := doublestar.SplitPattern(source)
|
||||||
if static == "" {
|
if static == "" || static == "." {
|
||||||
static = workdir
|
static = workdir
|
||||||
}
|
}
|
||||||
LogInfo("Static part: %s", static)
|
LogInfo("Static part: %s", static)
|
||||||
|
2343
instruction_test.go
Normal file
2343
instruction_test.go
Normal file
File diff suppressed because it is too large
Load Diff
122
main.go
122
main.go
@@ -30,7 +30,27 @@ func main() {
|
|||||||
flag.Parse()
|
flag.Parse()
|
||||||
undo = *undoF
|
undo = *undoF
|
||||||
|
|
||||||
if *debug {
|
setupLogging(*debug)
|
||||||
|
|
||||||
|
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")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
LogInfo("All done")
|
||||||
|
}
|
||||||
|
|
||||||
|
// setupLogging configures logging based on debug flag
|
||||||
|
func setupLogging(debug bool) {
|
||||||
|
if debug {
|
||||||
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
|
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
|
||||||
logFile, err := os.Create(programName + ".log")
|
logFile, err := os.Create(programName + ".log")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -42,19 +62,19 @@ func main() {
|
|||||||
} else {
|
} else {
|
||||||
log.SetFlags(log.Lmicroseconds)
|
log.SetFlags(log.Lmicroseconds)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
instructions := make(chan *LinkInstruction, 1000)
|
// startInputSource determines and starts the appropriate input source
|
||||||
status := make(chan error)
|
func startInputSource(recurse, file string, instructions chan *LinkInstruction, status chan error) {
|
||||||
|
|
||||||
// Check input sources in priority order
|
// Check input sources in priority order
|
||||||
switch {
|
switch {
|
||||||
case *recurse != "":
|
case recurse != "":
|
||||||
LogInfo("Recurse: %s", *recurse)
|
LogInfo("Recurse: %s", recurse)
|
||||||
go ReadFromFilesRecursively(*recurse, instructions, status)
|
go ReadFromFilesRecursively(recurse, instructions, status)
|
||||||
|
|
||||||
case *file != "":
|
case file != "":
|
||||||
LogInfo("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:
|
||||||
LogInfo("Reading from command line arguments")
|
LogInfo("Reading from command line arguments")
|
||||||
@@ -65,39 +85,53 @@ func main() {
|
|||||||
// go ReadFromStdin(instructions, status)
|
// go ReadFromStdin(instructions, status)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if _, err := os.Stat("sync"); err == nil {
|
startDefaultInputSource(instructions, status)
|
||||||
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")
|
// startDefaultInputSource tries to find default sync files
|
||||||
go ReadFromFile("sync.yaml", instructions, status, true)
|
func startDefaultInputSource(instructions chan *LinkInstruction, status chan error) {
|
||||||
} else if _, err := os.Stat("sync.yml"); err == nil {
|
if _, err := os.Stat("sync"); err == nil {
|
||||||
LogInfo("Using default sync.yml file")
|
LogInfo("Using default sync file")
|
||||||
go ReadFromFile("sync.yml", instructions, status, true)
|
go ReadFromFile("sync", instructions, status, true)
|
||||||
} else {
|
} else if _, err := os.Stat("sync.yaml"); err == nil {
|
||||||
LogInfo("No input provided")
|
LogInfo("Using default sync.yaml file")
|
||||||
LogInfo("Provide input as: ")
|
go ReadFromFile("sync.yaml", instructions, status, true)
|
||||||
LogInfo("Arguments - %s <source>,<target>,<force?>", programName)
|
} else if _, err := os.Stat("sync.yml"); err == nil {
|
||||||
LogInfo("File - %s -f <file>", programName)
|
LogInfo("Using default sync.yml file")
|
||||||
LogInfo("YAML File - %s -f <file.yaml>", programName)
|
go ReadFromFile("sync.yml", instructions, status, true)
|
||||||
LogInfo("Folder (finding sync files in folder recursively) - %s -r <folder>", programName)
|
} else {
|
||||||
LogInfo("stdin - (cat <file> | %s)", programName)
|
showUsageAndExit()
|
||||||
os.Exit(1)
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
// processInstructions processes all instructions from the channel
|
||||||
for {
|
func processInstructions(instructions chan *LinkInstruction) int32 {
|
||||||
err, ok := <-status
|
|
||||||
if !ok {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
LogError("%v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
var instructionsDone int32 = 0
|
var instructionsDone int32 = 0
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
for {
|
for {
|
||||||
@@ -118,11 +152,7 @@ func main() {
|
|||||||
wg.Done()
|
wg.Done()
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
if instructionsDone == 0 {
|
return instructionsDone
|
||||||
LogInfo("No instructions were processed")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
LogInfo("All done")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsPipeInput() bool {
|
func IsPipeInput() bool {
|
||||||
@@ -201,7 +231,7 @@ func ReadFromFile(input string, output chan *LinkInstruction, status chan error,
|
|||||||
// Check if this is a YAML file
|
// Check if this is a YAML file
|
||||||
if IsYAMLFile(input) {
|
if IsYAMLFile(input) {
|
||||||
LogInfo("Parsing as YAML file")
|
LogInfo("Parsing as YAML file")
|
||||||
instructions, err := ParseYAMLFile(input, filepath.Dir(input))
|
instructions, err := ParseYAMLFileRecursive(input, filepath.Dir(input))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogError("Failed to parse YAML file %s: %v",
|
LogError("Failed to parse YAML file %s: %v",
|
||||||
FormatSourcePath(input), err)
|
FormatSourcePath(input), err)
|
||||||
|
Reference in New Issue
Block a user