Compare commits
12 Commits
3a5a333c62
...
master
Author | SHA1 | Date | |
---|---|---|---|
eb81ec4162 | |||
21d7f56ccf | |||
8653191df2 | |||
9da47ce0cf | |||
29bfa2d776 | |||
c94a7ae8ab | |||
ca57ee728e | |||
b53628e698 | |||
3f7fd36f84 | |||
8da1f023a7 | |||
33b3a3d2b6 | |||
78536c3e19 |
3
.gitignore
vendored
3
.gitignore
vendored
@@ -2,3 +2,6 @@
|
|||||||
*.exe
|
*.exe
|
||||||
cln
|
cln
|
||||||
cln.log
|
cln.log
|
||||||
|
.qodo
|
||||||
|
*.log
|
||||||
|
*.out
|
||||||
|
16
.vscode/launch.json
vendored
Normal file
16
.vscode/launch.json
vendored
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
{
|
||||||
|
// Use IntelliSense to learn about possible attributes.
|
||||||
|
// Hover to view descriptions of existing attributes.
|
||||||
|
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "Ereshor Workspace",
|
||||||
|
"type": "go",
|
||||||
|
"request": "launch",
|
||||||
|
"mode": "auto",
|
||||||
|
"program": "${workspaceFolder}",
|
||||||
|
"cwd": "C:\\Users\\Administrator\\Seafile\\Games-Ereshor"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
2
build.sh
Normal file
2
build.sh
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
GOOS=windows GOARCH=amd64 go build -o cln.exe .
|
||||||
|
GOOS=linux GOARCH=amd64 go build -o cln .
|
10
go.mod
10
go.mod
@@ -3,3 +3,13 @@ module cln
|
|||||||
go 1.21.7
|
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
|
||||||
|
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
|
||||||
|
)
|
||||||
|
8
go.sum
8
go.sum
@@ -1,3 +1,11 @@
|
|||||||
|
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=
|
||||||
|
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=
|
||||||
|
138
instruction.go
138
instruction.go
@@ -6,6 +6,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/bmatcuk/doublestar/v4"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -19,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() {
|
||||||
@@ -283,11 +285,10 @@ func ParseYAMLFile(filename, workdir string) ([]LinkInstruction, error) {
|
|||||||
|
|
||||||
expanded := []LinkInstruction{}
|
expanded := []LinkInstruction{}
|
||||||
for _, link := range config.Links {
|
for _, link := range config.Links {
|
||||||
if strings.Contains(link.Source, "*") {
|
LogSource("Expanding pattern source %s in YAML file %s", link.Source, filename)
|
||||||
LogSource("Expanding wildcard source %s in YAML file %s", link.Source, filename)
|
newlinks, err := ExpandPattern(link.Source, workdir, link.Target)
|
||||||
newlinks, err := ExpandWildcard(link.Source, workdir, link.Target)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error expanding wildcard: %w", err)
|
return nil, fmt.Errorf("error expanding pattern: %w", err)
|
||||||
}
|
}
|
||||||
// "Clone" the original link instruction for each expanded link
|
// "Clone" the original link instruction for each expanded link
|
||||||
for i := range newlinks {
|
for i := range newlinks {
|
||||||
@@ -295,12 +296,9 @@ func ParseYAMLFile(filename, workdir string) ([]LinkInstruction, error) {
|
|||||||
newlinks[i].Hard = link.Hard
|
newlinks[i].Hard = link.Hard
|
||||||
newlinks[i].Force = link.Force
|
newlinks[i].Force = link.Force
|
||||||
}
|
}
|
||||||
LogInfo("Expanded wildcard source %s in YAML file %s to %d links",
|
LogInfo("Expanded pattern %s in YAML file %s to %d links",
|
||||||
FormatSourcePath(link.Source), FormatSourcePath(filename), len(newlinks))
|
FormatSourcePath(link.Source), FormatSourcePath(filename), len(newlinks))
|
||||||
expanded = append(expanded, newlinks...)
|
expanded = append(expanded, newlinks...)
|
||||||
} else {
|
|
||||||
expanded = append(expanded, link)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range expanded {
|
for i := range expanded {
|
||||||
@@ -321,24 +319,128 @@ func ParseYAMLFile(filename, workdir string) ([]LinkInstruction, error) {
|
|||||||
return expanded, nil
|
return expanded, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExpandWildcard(source, workdir, target string) (links []LinkInstruction, err error) {
|
// ParseYAMLFileRecursive parses a YAML file and recursively processes any "From" references
|
||||||
dir := filepath.Dir(source)
|
func ParseYAMLFileRecursive(filename, workdir string) ([]LinkInstruction, error) {
|
||||||
pattern := filepath.Base(source)
|
visited := make(map[string]bool)
|
||||||
|
return parseYAMLFileRecursive(filename, workdir, visited)
|
||||||
|
}
|
||||||
|
|
||||||
files, err := filepath.Glob(filepath.Join(workdir, dir, pattern))
|
// 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 {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error expanding wildcard: %w", err)
|
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) {
|
||||||
|
static, pattern := doublestar.SplitPattern(source)
|
||||||
|
if static == "" || static == "." {
|
||||||
|
static = workdir
|
||||||
|
}
|
||||||
|
LogInfo("Static part: %s", static)
|
||||||
|
LogInfo("Pattern part: %s", pattern)
|
||||||
|
|
||||||
|
files, err := doublestar.Glob(os.DirFS(static), pattern)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error expanding pattern: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
targetIsFile := false
|
||||||
|
if info, err := os.Stat(target); err == nil && !info.IsDir() {
|
||||||
|
targetIsFile = true
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
link := LinkInstruction{
|
if len(files) == 1 {
|
||||||
Source: file,
|
// Special case: if there is only one file
|
||||||
Target: filepath.Join(target, filepath.Base(file)),
|
// This should only ever happen if our source is a path (and not a glob!)
|
||||||
|
// And our target is a path
|
||||||
|
// ...but it will also happen if the source IS a glob and it happens to match ONE file
|
||||||
|
// I think that should happen rarely enough to not be an issue...
|
||||||
|
links = append(links, LinkInstruction{
|
||||||
|
Source: filepath.Join(static, file),
|
||||||
|
Target: target,
|
||||||
|
})
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
links = append(links, link)
|
if info, err := os.Stat(file); err == nil && info.IsDir() {
|
||||||
|
// We don't care about matched directories
|
||||||
|
// We want files within them
|
||||||
|
LogInfo("Skipping directory %s", file)
|
||||||
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
LogInfo("Expanded wildcard source %s to %d links", FormatSourcePath(source), len(links))
|
var targetPath string
|
||||||
|
if targetIsFile && len(files) == 1 {
|
||||||
|
// Special case: target is a file, and glob matches exactly one file.
|
||||||
|
// Use target directly (don't append filename).
|
||||||
|
targetPath = target
|
||||||
|
} else {
|
||||||
|
// Default: append filename to target dir.
|
||||||
|
targetPath = filepath.Join(target, file)
|
||||||
|
}
|
||||||
|
|
||||||
|
links = append(links, LinkInstruction{
|
||||||
|
Source: filepath.Join(static, file),
|
||||||
|
Target: targetPath,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
LogInfo("Expanded pattern %s to %d links", FormatSourcePath(source), len(links))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
2343
instruction_test.go
Normal file
2343
instruction_test.go
Normal file
File diff suppressed because it is too large
Load Diff
135
main.go
135
main.go
@@ -7,7 +7,6 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"regexp"
|
|
||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
)
|
)
|
||||||
@@ -20,7 +19,6 @@ const ImportantColor = BRed
|
|||||||
const DefaultColor = Reset
|
const DefaultColor = Reset
|
||||||
const PathColor = Green
|
const PathColor = Green
|
||||||
|
|
||||||
var FileRegex, _ = regexp.Compile(`sync\.ya?ml$`)
|
|
||||||
var programName = os.Args[0]
|
var programName = os.Args[0]
|
||||||
var undo = false
|
var undo = false
|
||||||
|
|
||||||
@@ -32,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 {
|
||||||
@@ -44,29 +62,35 @@ 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")
|
||||||
go ReadFromArgs(instructions, status)
|
go ReadFromArgs(instructions, status)
|
||||||
|
|
||||||
case IsPipeInput():
|
// case IsPipeInput():
|
||||||
LogInfo("Reading from stdin pipe")
|
// LogInfo("Reading from stdin pipe")
|
||||||
go ReadFromStdin(instructions, status)
|
// go ReadFromStdin(instructions, status)
|
||||||
|
|
||||||
default:
|
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 {
|
if _, err := os.Stat("sync"); err == nil {
|
||||||
LogInfo("Using default sync file")
|
LogInfo("Using default sync file")
|
||||||
go ReadFromFile("sync", instructions, status, true)
|
go ReadFromFile("sync", instructions, status, true)
|
||||||
@@ -77,6 +101,12 @@ func main() {
|
|||||||
LogInfo("Using default sync.yml file")
|
LogInfo("Using default sync.yml file")
|
||||||
go ReadFromFile("sync.yml", instructions, status, true)
|
go ReadFromFile("sync.yml", instructions, status, true)
|
||||||
} else {
|
} else {
|
||||||
|
showUsageAndExit()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// showUsageAndExit displays usage information and exits
|
||||||
|
func showUsageAndExit() {
|
||||||
LogInfo("No input provided")
|
LogInfo("No input provided")
|
||||||
LogInfo("Provide input as: ")
|
LogInfo("Provide input as: ")
|
||||||
LogInfo("Arguments - %s <source>,<target>,<force?>", programName)
|
LogInfo("Arguments - %s <source>,<target>,<force?>", programName)
|
||||||
@@ -85,10 +115,10 @@ func main() {
|
|||||||
LogInfo("Folder (finding sync files in folder recursively) - %s -r <folder>", programName)
|
LogInfo("Folder (finding sync files in folder recursively) - %s -r <folder>", programName)
|
||||||
LogInfo("stdin - (cat <file> | %s)", programName)
|
LogInfo("stdin - (cat <file> | %s)", programName)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
go func() {
|
// handleStatusErrors processes status channel errors
|
||||||
|
func handleStatusErrors(status chan error) {
|
||||||
for {
|
for {
|
||||||
err, ok := <-status
|
err, ok := <-status
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -98,8 +128,10 @@ func main() {
|
|||||||
LogError("%v", err)
|
LogError("%v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}
|
||||||
|
|
||||||
|
// processInstructions processes all instructions from the channel
|
||||||
|
func processInstructions(instructions chan *LinkInstruction) int32 {
|
||||||
var instructionsDone int32 = 0
|
var instructionsDone int32 = 0
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
for {
|
for {
|
||||||
@@ -120,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 {
|
||||||
@@ -145,14 +173,19 @@ func ReadFromFilesRecursively(input string, output chan *LinkInstruction, status
|
|||||||
|
|
||||||
files := make(chan string, 128)
|
files := make(chan string, 128)
|
||||||
fileStatus := make(chan error)
|
fileStatus := make(chan error)
|
||||||
var wg sync.WaitGroup
|
|
||||||
go GetSyncFilesRecursively(input, files, fileStatus)
|
go GetSyncFilesRecursively(input, files, fileStatus)
|
||||||
go func() {
|
|
||||||
wg.Wait()
|
|
||||||
close(files)
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
// 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 {
|
for {
|
||||||
err, ok := <-fileStatus
|
err, ok := <-fileStatus
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -163,40 +196,26 @@ func ReadFromFilesRecursively(input string, output chan *LinkInstruction, status
|
|||||||
status <- err
|
status <- err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
|
||||||
|
|
||||||
for {
|
// Process each file
|
||||||
file, ok := <-files
|
for _, file := range syncFiles {
|
||||||
if !ok {
|
|
||||||
LogInfo("No more files to process")
|
|
||||||
break
|
|
||||||
}
|
|
||||||
wg.Add(1)
|
|
||||||
go func() {
|
|
||||||
defer wg.Done()
|
|
||||||
LogInfo(file)
|
|
||||||
file = NormalizePath(file, workdir)
|
file = NormalizePath(file, workdir)
|
||||||
LogInfo("Processing file: %s", FormatPathValue(file))
|
LogInfo("Processing file: %s", FormatPathValue(file))
|
||||||
|
|
||||||
// This "has" to be done because instructions are resolved in relation to cwd
|
// Change to the directory containing the sync file
|
||||||
fileDir := FileRegex.FindStringSubmatch(file)
|
fileDir := filepath.Dir(file)
|
||||||
if fileDir == nil {
|
originalDir, _ := os.Getwd()
|
||||||
LogError("Failed to extract directory from %s", FormatSourcePath(file))
|
err := os.Chdir(fileDir)
|
||||||
return
|
|
||||||
}
|
|
||||||
LogInfo("Changing directory to %s (for %s)",
|
|
||||||
FormatPathValue(fileDir[1]),
|
|
||||||
FormatPathValue(file))
|
|
||||||
err := os.Chdir(fileDir[1])
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogError("Failed to change directory to %s: %v",
|
LogError("Failed to change directory to %s: %v", FormatSourcePath(fileDir), err)
|
||||||
FormatSourcePath(fileDir[1]), err)
|
continue
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Read and process the file
|
||||||
ReadFromFile(file, output, status, false)
|
ReadFromFile(file, output, status, false)
|
||||||
// Don't return directory, stay where we are
|
|
||||||
os.Chdir(workdir)
|
// Return to original directory
|
||||||
}()
|
os.Chdir(originalDir)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,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)
|
||||||
|
52
release.sh
Normal file
52
release.sh
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
echo "Figuring out the tag..."
|
||||||
|
TAG=$(git describe --tags --exact-match 2>/dev/null || echo "")
|
||||||
|
if [ -z "$TAG" ]; then
|
||||||
|
# Get the latest tag
|
||||||
|
LATEST_TAG=$(git describe --tags $(git rev-list --tags --max-count=1))
|
||||||
|
# Increment the patch version
|
||||||
|
IFS='.' read -r -a VERSION_PARTS <<< "$LATEST_TAG"
|
||||||
|
VERSION_PARTS[2]=$((VERSION_PARTS[2]+1))
|
||||||
|
TAG="${VERSION_PARTS[0]}.${VERSION_PARTS[1]}.${VERSION_PARTS[2]}"
|
||||||
|
# Create a new tag
|
||||||
|
git tag $TAG
|
||||||
|
git push origin $TAG
|
||||||
|
fi
|
||||||
|
echo "Tag: $TAG"
|
||||||
|
|
||||||
|
echo "Building the thing..."
|
||||||
|
sh build.sh
|
||||||
|
sh install.sh
|
||||||
|
|
||||||
|
echo "Creating a release..."
|
||||||
|
TOKEN="$GITEA_API_KEY"
|
||||||
|
GITEA="https://git.site.quack-lab.dev"
|
||||||
|
REPO="dave/synclib"
|
||||||
|
# Create a release
|
||||||
|
RELEASE_RESPONSE=$(curl -s -X POST \
|
||||||
|
-H "Authorization: token $TOKEN" \
|
||||||
|
-H "Accept: application/json" \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{
|
||||||
|
"tag_name": "'"$TAG"'",
|
||||||
|
"name": "'"$TAG"'",
|
||||||
|
"draft": false,
|
||||||
|
"prerelease": false
|
||||||
|
}' \
|
||||||
|
$GITEA/api/v1/repos/$REPO/releases)
|
||||||
|
|
||||||
|
# Extract the release ID
|
||||||
|
echo $RELEASE_RESPONSE
|
||||||
|
RELEASE_ID=$(echo $RELEASE_RESPONSE | awk -F'"id":' '{print $2+0; exit}')
|
||||||
|
echo "Release ID: $RELEASE_ID"
|
||||||
|
|
||||||
|
echo "Uploading the things..."
|
||||||
|
curl -X POST \
|
||||||
|
-H "Authorization: token $TOKEN" \
|
||||||
|
-F "attachment=@cln.exe" \
|
||||||
|
"$GITEA/api/v1/repos/$REPO/releases/${RELEASE_ID}/assets?name=cln.exe"
|
||||||
|
curl -X POST \
|
||||||
|
-H "Authorization: token $TOKEN" \
|
||||||
|
-F "attachment=@cln" \
|
||||||
|
"$GITEA/api/v1/repos/$REPO/releases/${RELEASE_ID}/assets?name=cln"
|
@@ -1,2 +1,6 @@
|
|||||||
- source: \*
|
- source: A/**/*
|
||||||
target: test/
|
target: B
|
||||||
|
- source: A/go.mod
|
||||||
|
target: B/go.mod
|
||||||
|
- source: A
|
||||||
|
target: B/foo
|
||||||
|
26
synclib.log
26
synclib.log
@@ -1,26 +0,0 @@
|
|||||||
18:24:37.967093 Using default sync.yaml file
|
|
||||||
18:24:37.967093 Input 'sync.yaml' is not absolute, prepending work dir '.'
|
|
||||||
18:24:37.967593 Reading input from file: [0;32mC:/Users/Administrator/Seafile/Projects-Go/GoProjects/synclib/sync.yaml[0m
|
|
||||||
18:24:37.967593 Parsing as YAML file
|
|
||||||
18:24:37.967593 [4;31mFailed to parse YAML file [0;35mC:/Users/Administrator/Seafile/Projects-Go/GoProjects/synclib/sync.yaml[0m: [4;31merror unmarshaling YAML: yaml: unmarshal errors:
|
|
||||||
line 1: cannot unmarshal !!seq into main.YAMLConfig[0m[0m
|
|
||||||
18:24:37.967593 No more instructions to process
|
|
||||||
18:24:37.968092 No instructions were processed
|
|
||||||
18:27:59.691333 Using default sync.yaml file
|
|
||||||
18:27:59.691333 Input 'sync.yaml' is not absolute, prepending work dir '.'
|
|
||||||
18:27:59.691834 Reading input from file: [0;32mC:/Users/Administrator/Seafile/Projects-Go/GoProjects/synclib/sync.yaml[0m
|
|
||||||
18:27:59.691834 Parsing as YAML file
|
|
||||||
18:27:59.692335 [0;35mExpanding wildcard source \* in YAML file C:/Users/Administrator/Seafile/Projects-Go/GoProjects/synclib/sync.yaml[0m
|
|
||||||
18:27:59.692335 Expanded wildcard source [0;35m\*[0m to 0 links
|
|
||||||
18:27:59.692836 Expanded wildcard source [0;35m\*[0m in YAML file [0;35mC:/Users/Administrator/Seafile/Projects-Go/GoProjects/synclib/sync.yaml[0m to 0 links
|
|
||||||
18:27:59.692836 No more instructions to process
|
|
||||||
18:27:59.692836 No instructions were processed
|
|
||||||
18:28:04.075821 Using default sync.yaml file
|
|
||||||
18:28:04.076320 Input 'sync.yaml' is not absolute, prepending work dir '.'
|
|
||||||
18:28:04.076320 Reading input from file: [0;32mC:/Users/Administrator/Seafile/Projects-Go/GoProjects/synclib/sync.yaml[0m
|
|
||||||
18:28:04.076320 Parsing as YAML file
|
|
||||||
18:28:04.076320 [0;35mExpanding wildcard source \* in YAML file C:/Users/Administrator/Seafile/Projects-Go/GoProjects/synclib/sync.yaml[0m
|
|
||||||
18:28:04.076821 Expanded wildcard source [0;35m\*[0m to 0 links
|
|
||||||
18:28:04.076821 Expanded wildcard source [0;35m\*[0m in YAML file [0;35mC:/Users/Administrator/Seafile/Projects-Go/GoProjects/synclib/sync.yaml[0m to 0 links
|
|
||||||
18:28:04.076821 No more instructions to process
|
|
||||||
18:28:04.076821 No instructions were processed
|
|
117
util.go
117
util.go
@@ -5,9 +5,8 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
|
||||||
"sync/atomic"
|
"github.com/bmatcuk/doublestar/v4"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func IsSymlink(path string) (bool, error) {
|
func IsSymlink(path string) (bool, error) {
|
||||||
@@ -76,112 +75,24 @@ func GetSyncFilesRecursively(input string, output chan string, status chan error
|
|||||||
defer close(output)
|
defer close(output)
|
||||||
defer close(status)
|
defer close(status)
|
||||||
|
|
||||||
var filesProcessed int32
|
workdir, _ := os.Getwd()
|
||||||
var foldersProcessed int32
|
input = NormalizePath(input, workdir)
|
||||||
var activeWorkers int32
|
LogInfo("Searching for sync files recursively starting in %s", FormatPathValue(input))
|
||||||
|
|
||||||
progressTicker := time.NewTicker(200 * time.Millisecond)
|
// Use doublestar to find all sync.yml and sync.yaml files recursively
|
||||||
defer progressTicker.Stop()
|
pattern := "**/sync.y*ml"
|
||||||
|
files, err := doublestar.Glob(os.DirFS(input), pattern)
|
||||||
done := make(chan struct{})
|
|
||||||
defer close(done)
|
|
||||||
|
|
||||||
directories := make(chan string, 100000)
|
|
||||||
workerPool := make(chan struct{}, 4000)
|
|
||||||
directories <- input
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case <-progressTicker.C:
|
|
||||||
dirCount := len(directories)
|
|
||||||
workers := atomic.LoadInt32(&activeWorkers)
|
|
||||||
fmt.Printf("\rFiles processed: %d; Folders processed: %d; Active workers: %d; Directory queue: %d",
|
|
||||||
atomic.LoadInt32(&filesProcessed),
|
|
||||||
atomic.LoadInt32(&foldersProcessed),
|
|
||||||
workers,
|
|
||||||
dirCount)
|
|
||||||
case <-done:
|
|
||||||
// Final progress update
|
|
||||||
fmt.Printf("\nFiles processed: %d; Folders processed: %d; Completed successfully\n",
|
|
||||||
atomic.LoadInt32(&filesProcessed),
|
|
||||||
atomic.LoadInt32(&foldersProcessed))
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
allDone := make(chan struct{})
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
// WTF is this waitgroup?
|
|
||||||
// Nowhere is it added...
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
if atomic.LoadInt32(&activeWorkers) == 0 && len(directories) == 0 {
|
|
||||||
time.Sleep(10 * time.Millisecond)
|
|
||||||
if atomic.LoadInt32(&activeWorkers) == 0 && len(directories) == 0 {
|
|
||||||
close(allDone)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
time.Sleep(50 * time.Millisecond)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
for {
|
|
||||||
select {
|
|
||||||
case directory, ok := <-directories:
|
|
||||||
if !ok {
|
|
||||||
wg.Wait()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
atomic.AddInt32(&activeWorkers, 1)
|
|
||||||
|
|
||||||
go func(dir string) {
|
|
||||||
workerPool <- struct{}{}
|
|
||||||
|
|
||||||
atomic.AddInt32(&foldersProcessed, 1)
|
|
||||||
processDirectory(dir, directories, output, &filesProcessed)
|
|
||||||
|
|
||||||
<-workerPool
|
|
||||||
atomic.AddInt32(&activeWorkers, -1)
|
|
||||||
}(directory)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
<-allDone
|
|
||||||
|
|
||||||
if atomic.LoadInt32(&filesProcessed) > 0 {
|
|
||||||
LogInfo("Files processed: %d; Folders processed: %d",
|
|
||||||
atomic.LoadInt32(&filesProcessed),
|
|
||||||
atomic.LoadInt32(&foldersProcessed))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func processDirectory(directory string, directories chan<- string, output chan<- string, filesProcessed *int32) {
|
|
||||||
files, err := os.ReadDir(directory)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LogError("Error reading directory %s: %v", directory, err)
|
LogError("Failed to search for pattern %s: %v", pattern, err)
|
||||||
|
status <- err
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
if file.IsDir() {
|
fullPath := filepath.Join(input, file)
|
||||||
directories <- filepath.Join(directory, file.Name())
|
LogInfo("Found sync file: %s", FormatPathValue(fullPath))
|
||||||
} else {
|
output <- fullPath
|
||||||
if IsYAMLSyncFile(file.Name()) {
|
|
||||||
output <- filepath.Join(directory, file.Name())
|
|
||||||
}
|
}
|
||||||
atomic.AddInt32(filesProcessed, 1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func IsYAMLSyncFile(filename string) bool {
|
LogInfo("Completed recursive search for sync files")
|
||||||
return filename == "sync.yaml" || filename == "sync.yml"
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user