Compare commits
5 Commits
v1.0.0
...
29bfa2d776
Author | SHA1 | Date | |
---|---|---|---|
29bfa2d776 | |||
c94a7ae8ab | |||
ca57ee728e | |||
b53628e698 | |||
3f7fd36f84 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -3,3 +3,4 @@
|
|||||||
cln
|
cln
|
||||||
cln.log
|
cln.log
|
||||||
.qodo
|
.qodo
|
||||||
|
*.log
|
||||||
|
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 .
|
@@ -337,21 +337,21 @@ func ExpandPattern(source, workdir, target string) (links []LinkInstruction, err
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, file := range files {
|
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
|
||||||
|
}
|
||||||
if info, err := os.Stat(file); err == nil && info.IsDir() {
|
if info, err := os.Stat(file); err == nil && info.IsDir() {
|
||||||
// We don't care about matched directories
|
// We don't care about matched directories
|
||||||
// We want files within them
|
// We want files within them
|
||||||
if len(files) == 1 {
|
|
||||||
// Special case: if there is only one file, and it's a directory
|
|
||||||
// This should only ever happen if our source is a path (and not a glob!)
|
|
||||||
// And our target is a path, a directory
|
|
||||||
// ...but it will also happen if the source IS a glob and it happens to match ONE directory
|
|
||||||
// I think that should happen rarely enough to not be an issue...
|
|
||||||
links = append(links, LinkInstruction{
|
|
||||||
Source: filepath.Join(static, file),
|
|
||||||
Target: target,
|
|
||||||
})
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
LogInfo("Skipping directory %s", file)
|
LogInfo("Skipping directory %s", file)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
81
main.go
81
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
|
||||||
|
|
||||||
@@ -145,58 +143,49 @@ 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() {
|
|
||||||
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 {
|
for {
|
||||||
file, ok := <-files
|
file, ok := <-files
|
||||||
if !ok {
|
if !ok {
|
||||||
LogInfo("No more files to process")
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
wg.Add(1)
|
syncFiles = append(syncFiles, file)
|
||||||
go func() {
|
}
|
||||||
defer wg.Done()
|
|
||||||
LogInfo(file)
|
|
||||||
file = NormalizePath(file, workdir)
|
|
||||||
LogInfo("Processing file: %s", FormatPathValue(file))
|
|
||||||
|
|
||||||
// This "has" to be done because instructions are resolved in relation to cwd
|
// Check for errors from file search
|
||||||
fileDir := FileRegex.FindStringSubmatch(file)
|
for {
|
||||||
if fileDir == nil {
|
err, ok := <-fileStatus
|
||||||
LogError("Failed to extract directory from %s", FormatSourcePath(file))
|
if !ok {
|
||||||
return
|
break
|
||||||
}
|
}
|
||||||
LogInfo("Changing directory to %s (for %s)",
|
if err != nil {
|
||||||
FormatPathValue(fileDir[1]),
|
LogError("Failed to get sync files recursively: %v", err)
|
||||||
FormatPathValue(file))
|
status <- err
|
||||||
err := os.Chdir(fileDir[1])
|
}
|
||||||
if err != nil {
|
}
|
||||||
LogError("Failed to change directory to %s: %v",
|
|
||||||
FormatSourcePath(fileDir[1]), err)
|
// Process each file
|
||||||
return
|
for _, file := range syncFiles {
|
||||||
}
|
file = NormalizePath(file, workdir)
|
||||||
ReadFromFile(file, output, status, false)
|
LogInfo("Processing file: %s", FormatPathValue(file))
|
||||||
// Don't return directory, stay where we are
|
|
||||||
os.Chdir(workdir)
|
// 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
10
release.sh
10
release.sh
@@ -16,8 +16,8 @@ fi
|
|||||||
echo "Tag: $TAG"
|
echo "Tag: $TAG"
|
||||||
|
|
||||||
echo "Building the thing..."
|
echo "Building the thing..."
|
||||||
go build .
|
sh build.sh
|
||||||
go install .
|
sh install.sh
|
||||||
|
|
||||||
echo "Creating a release..."
|
echo "Creating a release..."
|
||||||
TOKEN="$GITEA_API_KEY"
|
TOKEN="$GITEA_API_KEY"
|
||||||
@@ -45,4 +45,8 @@ echo "Uploading the things..."
|
|||||||
curl -X POST \
|
curl -X POST \
|
||||||
-H "Authorization: token $TOKEN" \
|
-H "Authorization: token $TOKEN" \
|
||||||
-F "attachment=@cln.exe" \
|
-F "attachment=@cln.exe" \
|
||||||
"$GITEA/api/v1/repos/$REPO/releases/${RELEASE_ID}/assets?name=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"
|
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