Compare commits
1 Commits
v1.2.0
...
beca23447e
Author | SHA1 | Date | |
---|---|---|---|
beca23447e |
2
.gitignore
vendored
2
.gitignore
vendored
@@ -2,5 +2,3 @@
|
||||
*.exe
|
||||
cln
|
||||
cln.log
|
||||
.qodo
|
||||
*.log
|
||||
|
16
.vscode/launch.json
vendored
16
.vscode/launch.json
vendored
@@ -1,16 +0,0 @@
|
||||
{
|
||||
// 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
2
build.sh
@@ -1,2 +0,0 @@
|
||||
GOOS=windows GOARCH=amd64 go build -o cln.exe .
|
||||
GOOS=linux GOARCH=amd64 go build -o cln .
|
2
go.mod
2
go.mod
@@ -3,5 +3,3 @@ module cln
|
||||
go 1.21.7
|
||||
|
||||
require gopkg.in/yaml.v3 v3.0.1
|
||||
|
||||
require github.com/bmatcuk/doublestar/v4 v4.8.1
|
||||
|
2
go.sum
2
go.sum
@@ -1,5 +1,3 @@
|
||||
github.com/bmatcuk/doublestar/v4 v4.8.1 h1:54Bopc5c2cAvhLRAzqOGCYHYyhcDHsFF4wWIR5wKP38=
|
||||
github.com/bmatcuk/doublestar/v4 v4.8.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
|
||||
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=
|
||||
|
144
instruction.go
144
instruction.go
@@ -4,9 +4,9 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"slices"
|
||||
"strings"
|
||||
|
||||
"github.com/bmatcuk/doublestar/v4"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
@@ -49,38 +49,12 @@ func (instruction *LinkInstruction) String() string {
|
||||
flagsStr = " [" + strings.Join(flags, ", ") + "]"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s → %s%s",
|
||||
FormatSourcePath(instruction.Source),
|
||||
FormatTargetPath(instruction.Target),
|
||||
return fmt.Sprintf("%s%s%s → %s%s%s%s",
|
||||
SourceColor, instruction.Source, DefaultColor,
|
||||
TargetColor, instruction.Target, DefaultColor,
|
||||
flagsStr)
|
||||
}
|
||||
|
||||
func (instruction *LinkInstruction) Undo() {
|
||||
if !FileExists(instruction.Target) {
|
||||
LogInfo("%s does not exist, skipping", FormatTargetPath(instruction.Target))
|
||||
return
|
||||
}
|
||||
|
||||
isSymlink, err := IsSymlink(instruction.Target)
|
||||
if err != nil {
|
||||
LogError("could not determine whether %s is a sym link or not, stopping; err: %v",
|
||||
FormatTargetPath(instruction.Target), err)
|
||||
return
|
||||
}
|
||||
|
||||
if isSymlink {
|
||||
LogInfo("Removing symlink at %s", FormatTargetPath(instruction.Target))
|
||||
err = os.Remove(instruction.Target)
|
||||
if err != nil {
|
||||
LogError("could not remove symlink at %s; err: %v",
|
||||
FormatTargetPath(instruction.Target), err)
|
||||
}
|
||||
LogSuccess("Removed symlink at %s", FormatTargetPath(instruction.Target))
|
||||
} else {
|
||||
LogInfo("%s is not a symlink, skipping", FormatTargetPath(instruction.Target))
|
||||
}
|
||||
}
|
||||
|
||||
func ParseInstruction(line, workdir string) (LinkInstruction, error) {
|
||||
line = strings.TrimSpace(line)
|
||||
if strings.HasPrefix(line, "#") {
|
||||
@@ -157,21 +131,13 @@ func isTrue(value string) bool {
|
||||
|
||||
func (instruction *LinkInstruction) RunAsync(status chan (error)) {
|
||||
defer close(status)
|
||||
if undo {
|
||||
instruction.Undo()
|
||||
return
|
||||
}
|
||||
|
||||
if !FileExists(instruction.Source) {
|
||||
status <- fmt.Errorf("instruction source %s does not exist", FormatSourcePath(instruction.Source))
|
||||
return
|
||||
}
|
||||
|
||||
if !instruction.Force && AreSame(instruction.Source, instruction.Target) {
|
||||
//status <- fmt.Errorf("source %s and target %s are the same, nothing to do...",
|
||||
// FormatSourcePath(instruction.Source),
|
||||
// FormatTargetPath(instruction.Target))
|
||||
LogInfo("Source %s and target %s are the same, nothing to do...",
|
||||
status <- fmt.Errorf("source %s and target %s are the same, nothing to do...",
|
||||
FormatSourcePath(instruction.Source),
|
||||
FormatTargetPath(instruction.Target))
|
||||
return
|
||||
@@ -256,7 +222,7 @@ func (instruction *LinkInstruction) RunAsync(status chan (error)) {
|
||||
err)
|
||||
return
|
||||
}
|
||||
LogSuccess("Created symlink between %s and %s",
|
||||
LogInfo("Created symlink between %s and %s",
|
||||
FormatSourcePath(instruction.Source),
|
||||
FormatTargetPath(instruction.Target))
|
||||
|
||||
@@ -282,26 +248,27 @@ func ParseYAMLFile(filename, workdir string) ([]LinkInstruction, error) {
|
||||
config.Links = instructions
|
||||
}
|
||||
|
||||
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)
|
||||
toRemove := []int{}
|
||||
for i, link := range config.Links {
|
||||
if strings.Contains(link.Source, "*") {
|
||||
LogSource("Expanding wildcard source %s in YAML file %s", link.Source, filename)
|
||||
newlinks, err := ExpandWildcard(link.Source, workdir, link.Target)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error expanding wildcard: %w", err)
|
||||
}
|
||||
LogInfo("Expanded wildcard source %s in YAML file %s to %d links",
|
||||
FormatSourcePath(link.Source), FormatSourcePath(filename), len(newlinks))
|
||||
config.Links = append(config.Links, newlinks...)
|
||||
toRemove = append(toRemove, i)
|
||||
}
|
||||
// "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 {
|
||||
link := &expanded[i]
|
||||
for i := len(toRemove) - 1; i >= 0; i-- {
|
||||
config.Links = slices.Delete(config.Links, toRemove[i], 1)
|
||||
}
|
||||
|
||||
for i := range config.Links {
|
||||
link := &config.Links[i]
|
||||
|
||||
link.Tidy()
|
||||
link.Source, _ = ConvertHome(link.Source)
|
||||
@@ -310,69 +277,32 @@ func ParseYAMLFile(filename, workdir string) ([]LinkInstruction, error) {
|
||||
link.Target = NormalizePath(link.Target, workdir)
|
||||
|
||||
// If Delete is true, Force must also be true
|
||||
if link.Delete {
|
||||
link.Force = true
|
||||
if config.Links[i].Delete {
|
||||
config.Links[i].Force = true
|
||||
}
|
||||
}
|
||||
|
||||
return expanded, nil
|
||||
return config.Links, nil
|
||||
}
|
||||
|
||||
func ExpandPattern(source, workdir, target string) (links []LinkInstruction, err error) {
|
||||
static, pattern := doublestar.SplitPattern(source)
|
||||
if static == "" {
|
||||
static = workdir
|
||||
}
|
||||
LogInfo("Static part: %s", static)
|
||||
LogInfo("Pattern part: %s", pattern)
|
||||
func ExpandWildcard(source, workdir, target string) (links []LinkInstruction, err error) {
|
||||
dir := filepath.Dir(source)
|
||||
pattern := filepath.Base(source)
|
||||
|
||||
files, err := doublestar.Glob(os.DirFS(static), pattern)
|
||||
files, err := filepath.Glob(filepath.Join(workdir, dir, 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
|
||||
return nil, fmt.Errorf("error expanding wildcard: %w", err)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
if len(files) == 1 {
|
||||
// Special case: if there is only one 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
|
||||
link := LinkInstruction{
|
||||
Source: file,
|
||||
Target: filepath.Join(target, filepath.Base(file)),
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
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,
|
||||
})
|
||||
links = append(links, link)
|
||||
}
|
||||
|
||||
LogInfo("Expanded pattern %s to %d links", FormatSourcePath(source), len(links))
|
||||
LogInfo("Expanded wildcard source %s to %d links", FormatSourcePath(source), len(links))
|
||||
return
|
||||
}
|
||||
|
||||
|
@@ -14,7 +14,6 @@ const (
|
||||
TargetPrefix = "TARGET"
|
||||
PathPrefix = "PATH"
|
||||
ImportantPrefix = "IMPORTANT"
|
||||
SuccessPrefix = "DONE"
|
||||
)
|
||||
|
||||
// LogInfo logs an informational message
|
||||
@@ -23,12 +22,6 @@ func LogInfo(format string, args ...interface{}) {
|
||||
log.Printf("%s[%s]%s %s", BGreen, InfoPrefix, Reset, message)
|
||||
}
|
||||
|
||||
// LogSuccess logs a success message
|
||||
func LogSuccess(format string, args ...interface{}) {
|
||||
message := fmt.Sprintf(format, args...)
|
||||
log.Printf("%s%s[%s]%s %s", BBlue, On_Blue, SuccessPrefix, Reset, message)
|
||||
}
|
||||
|
||||
// LogSource logs a message about a source file/path with proper coloring
|
||||
func LogSource(format string, args ...interface{}) {
|
||||
message := fmt.Sprintf(format, args...)
|
||||
|
119
main.go
119
main.go
@@ -7,6 +7,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
@@ -19,20 +20,18 @@ const ImportantColor = BRed
|
||||
const DefaultColor = Reset
|
||||
const PathColor = Green
|
||||
|
||||
var FileRegex, _ = regexp.Compile(`sync\.ya?ml$`)
|
||||
var programName = os.Args[0]
|
||||
var undo = false
|
||||
|
||||
func main() {
|
||||
recurse := flag.String("r", "", "recurse into directories")
|
||||
file := flag.String("f", "", "file to read instructions from")
|
||||
debug := flag.Bool("d", false, "debug")
|
||||
undoF := flag.Bool("u", false, "undo")
|
||||
flag.Parse()
|
||||
undo = *undoF
|
||||
|
||||
if *debug {
|
||||
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
|
||||
logFile, err := os.Create(programName + ".log")
|
||||
logFile, err := os.Create("cln.log")
|
||||
if err != nil {
|
||||
LogError("Error creating log file: %v", err)
|
||||
os.Exit(1)
|
||||
@@ -60,15 +59,12 @@ func main() {
|
||||
LogInfo("Reading from command line arguments")
|
||||
go ReadFromArgs(instructions, status)
|
||||
|
||||
// case IsPipeInput():
|
||||
// LogInfo("Reading from stdin pipe")
|
||||
// go ReadFromStdin(instructions, status)
|
||||
case IsPipeInput():
|
||||
LogInfo("Reading from stdin pipe")
|
||||
go ReadFromStdin(instructions, status)
|
||||
|
||||
default:
|
||||
if _, err := os.Stat("sync"); err == nil {
|
||||
LogInfo("Using default sync file")
|
||||
go ReadFromFile("sync", instructions, status, true)
|
||||
} else if _, err := os.Stat("sync.yaml"); err == nil {
|
||||
if _, err := os.Stat("sync.yaml"); err == nil {
|
||||
LogInfo("Using default sync.yaml file")
|
||||
go ReadFromFile("sync.yaml", instructions, status, true)
|
||||
} else if _, err := os.Stat("sync.yml"); err == nil {
|
||||
@@ -143,49 +139,58 @@ func ReadFromFilesRecursively(input string, output chan *LinkInstruction, status
|
||||
|
||||
files := make(chan string, 128)
|
||||
fileStatus := make(chan error)
|
||||
var wg sync.WaitGroup
|
||||
go GetSyncFilesRecursively(input, files, fileStatus)
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(files)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
for {
|
||||
err, ok := <-fileStatus
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
LogError("Failed to get sync files recursively: %v", err)
|
||||
status <- err
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Collect all files first
|
||||
var syncFiles []string
|
||||
for {
|
||||
file, ok := <-files
|
||||
if !ok {
|
||||
LogInfo("No more files to process")
|
||||
break
|
||||
}
|
||||
syncFiles = append(syncFiles, file)
|
||||
}
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
LogInfo(file)
|
||||
file = NormalizePath(file, workdir)
|
||||
LogInfo("Processing file: %s", FormatPathValue(file))
|
||||
|
||||
// Check for errors from file search
|
||||
for {
|
||||
err, ok := <-fileStatus
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
LogError("Failed to get sync files recursively: %v", err)
|
||||
status <- err
|
||||
}
|
||||
}
|
||||
|
||||
// Process each file
|
||||
for _, file := range syncFiles {
|
||||
file = NormalizePath(file, workdir)
|
||||
LogInfo("Processing file: %s", FormatPathValue(file))
|
||||
|
||||
// Change to the directory containing the sync file
|
||||
fileDir := filepath.Dir(file)
|
||||
originalDir, _ := os.Getwd()
|
||||
err := os.Chdir(fileDir)
|
||||
if err != nil {
|
||||
LogError("Failed to change directory to %s: %v", FormatSourcePath(fileDir), err)
|
||||
continue
|
||||
}
|
||||
|
||||
// Read and process the file
|
||||
ReadFromFile(file, output, status, false)
|
||||
|
||||
// Return to original directory
|
||||
os.Chdir(originalDir)
|
||||
// This "has" to be done because instructions are resolved in relation to cwd
|
||||
fileDir := FileRegex.FindStringSubmatch(file)
|
||||
if fileDir == nil {
|
||||
LogError("Failed to extract directory from %s", FormatSourcePath(file))
|
||||
return
|
||||
}
|
||||
LogInfo("Changing directory to %s (for %s)",
|
||||
FormatPathValue(fileDir[1]),
|
||||
FormatPathValue(file))
|
||||
err := os.Chdir(fileDir[1])
|
||||
if err != nil {
|
||||
LogError("Failed to change directory to %s: %v",
|
||||
FormatSourcePath(fileDir[1]), err)
|
||||
return
|
||||
}
|
||||
ReadFromFile(file, output, status, false)
|
||||
// Don't return directory, stay where we are
|
||||
os.Chdir(workdir)
|
||||
}()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,28 +221,6 @@ func ReadFromFile(input string, output chan *LinkInstruction, status chan error,
|
||||
}
|
||||
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)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
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)
|
||||
continue
|
||||
}
|
||||
log.Printf("Read instruction: %s", instruction.String())
|
||||
output <- &instruction
|
||||
}
|
||||
}
|
||||
|
||||
func ReadFromArgs(output chan *LinkInstruction, status chan error) {
|
||||
|
48
release.sh
48
release.sh
@@ -1,48 +0,0 @@
|
||||
#!/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"
|
@@ -1,6 +1,2 @@
|
||||
- source: A/**/*
|
||||
target: B
|
||||
- source: A/go.mod
|
||||
target: B/go.mod
|
||||
- source: A
|
||||
target: B/foo
|
||||
- source: \*
|
||||
target: test/
|
26
synclib.log
Normal file
26
synclib.log
Normal file
@@ -0,0 +1,26 @@
|
||||
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
|
119
util.go
119
util.go
@@ -5,8 +5,9 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/bmatcuk/doublestar/v4"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
func IsSymlink(path string) (bool, error) {
|
||||
@@ -75,24 +76,112 @@ func GetSyncFilesRecursively(input string, output chan string, status chan error
|
||||
defer close(output)
|
||||
defer close(status)
|
||||
|
||||
workdir, _ := os.Getwd()
|
||||
input = NormalizePath(input, workdir)
|
||||
LogInfo("Searching for sync files recursively starting in %s", FormatPathValue(input))
|
||||
var filesProcessed int32
|
||||
var foldersProcessed int32
|
||||
var activeWorkers int32
|
||||
|
||||
// Use doublestar to find all sync.yml and sync.yaml files recursively
|
||||
pattern := "**/sync.y*ml"
|
||||
files, err := doublestar.Glob(os.DirFS(input), pattern)
|
||||
progressTicker := time.NewTicker(200 * time.Millisecond)
|
||||
defer progressTicker.Stop()
|
||||
|
||||
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 {
|
||||
LogError("Failed to search for pattern %s: %v", pattern, err)
|
||||
status <- err
|
||||
LogError("Error reading directory %s: %v", directory, err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
fullPath := filepath.Join(input, file)
|
||||
LogInfo("Found sync file: %s", FormatPathValue(fullPath))
|
||||
output <- fullPath
|
||||
if file.IsDir() {
|
||||
directories <- filepath.Join(directory, file.Name())
|
||||
} else {
|
||||
if IsYAMLSyncFile(file.Name()) {
|
||||
output <- filepath.Join(directory, file.Name())
|
||||
}
|
||||
atomic.AddInt32(filesProcessed, 1)
|
||||
}
|
||||
}
|
||||
|
||||
LogInfo("Completed recursive search for sync files")
|
||||
}
|
||||
|
||||
func IsYAMLSyncFile(filename string) bool {
|
||||
return filename == "sync.yaml" || filename == "sync.yml"
|
||||
}
|
||||
|
Reference in New Issue
Block a user