added support for wildcard extension '/*'

This commit is contained in:
Daniel Sommer 2023-09-22 10:54:10 +02:00
parent f67203286c
commit 53f077adcb
8 changed files with 61 additions and 33 deletions

6
.vscode/launch.json vendored
View file

@ -8,12 +8,12 @@
"mode": "auto", "mode": "auto",
"program": "${workspaceFolder}/main.go", "program": "${workspaceFolder}/main.go",
"args": [ "args": [
"/home/velvettear/downloads/music", "/home/velvettear/downloads/music/*",
"192.168.1.11:/tmp", "192.168.1.11:/share/music",
"--password", "--password",
"$Velvet90", "$Velvet90",
"--concurrency", "--concurrency",
"4", "12",
"--verbose", "--verbose",
], ],
"console": "integratedTerminal" "console": "integratedTerminal"

View file

@ -14,7 +14,7 @@ const LEVEL_WARNING = 2
const LEVEL_ERROR = 3 const LEVEL_ERROR = 3
const LEVEL_FATAL = 4 const LEVEL_FATAL = 4
var logLevel = 0 var logLevel = 1
// exported functions // exported functions
func SetLogLevel(level int) { func SetLogLevel(level int) {

View file

@ -8,6 +8,8 @@ import (
"velvettear/gosync/tools" "velvettear/gosync/tools"
) )
// version := "0.1"
func main() { func main() {
timestamp := time.Now() timestamp := time.Now()
settings.Initialize() settings.Initialize()

View file

@ -13,18 +13,20 @@ import (
func Initialize() { func Initialize() {
os.Args = os.Args[1:] os.Args = os.Args[1:]
var arguments []string var arguments []string
for _, arg := range os.Args {
arg = strings.ToLower(arg)
if arg != "-v" && arg != "--verbose" {
continue
}
setVerbose(true)
}
for index := 0; index < len(os.Args); index++ { for index := 0; index < len(os.Args); index++ {
// for index, arg := range os.Args {
switch strings.ToLower(os.Args[index]) { switch strings.ToLower(os.Args[index]) {
case "-h": case "-h":
fallthrough fallthrough
case "--help": case "--help":
help.Print() help.Print()
os.Exit(0) os.Exit(0)
case "-v":
fallthrough
case "--verbose":
setVerbose(true)
case "-c": case "-c":
fallthrough fallthrough
case "--concurrency": case "--concurrency":
@ -65,11 +67,11 @@ func Initialize() {
} }
setSource(arguments[0]) setSource(arguments[0])
setTarget(arguments[1]) setTarget(arguments[1])
_, error := os.Stat(Source) if !SourceIsRemote() {
source, _ := strings.CutSuffix(Source, "/*")
_, error := os.Stat(source)
if os.IsNotExist(error) { if os.IsNotExist(error) {
log.Fatal("given source does not exist", Source) log.Fatal("given source does not exist", source)
} }
if !Verbose {
setVerbose(false)
} }
} }

View file

@ -16,10 +16,22 @@ var User string
// exported function(s) // exported function(s)
func TargetIsRemote() bool { func TargetIsRemote() bool {
return strings.Contains(Target, ":") return isRemote(Target)
}
func SourceIsRemote() bool {
return isRemote(Source)
}
func SourceIsWildcard() bool {
return strings.HasSuffix(Source, "/*")
} }
// unexported function(s) // unexported function(s)
func isRemote(target string) bool {
return strings.Contains(target, ":")
}
func setVerbose(verbose bool) { func setVerbose(verbose bool) {
Verbose = verbose Verbose = verbose
if Verbose { if Verbose {

View file

@ -164,11 +164,12 @@ func transferFile(bar *mpb.Bar, file string) bool {
return true return true
} }
func getTargetLocation(source string) string { func getTargetLocation(sourceFile string) string {
if source == settings.Source { if sourceFile == settings.Source {
return filepath.Join(settings.Target, filepath.Base(source)) return filepath.Join(settings.Target, filepath.Base(sourceFile))
} }
return filepath.Join(settings.Target, strings.Replace(source, filepath.Dir(settings.Source), "", 1)) source, _ := strings.CutSuffix(settings.Source, "/*")
return filepath.Join(settings.Target, strings.Replace(sourceFile, filepath.Dir(source), "", 1))
} }
func createProgressBar(barcontainer *mpb.Progress, name string, size int64, max int64, priority int, total bool) *mpb.Bar { func createProgressBar(barcontainer *mpb.Progress, name string, size int64, max int64, priority int, total bool) *mpb.Bar {

View file

@ -5,6 +5,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strconv" "strconv"
"strings"
"time" "time"
"velvettear/gosync/log" "velvettear/gosync/log"
"velvettear/gosync/settings" "velvettear/gosync/settings"
@ -15,17 +16,22 @@ var sourceFiles []string
// unexported function(s) // unexported function(s)
func getSourceFiles() []string { func getSourceFiles() []string {
timestamp := time.Now() timestamp := time.Now()
stats, error := os.Stat(settings.Source) if settings.SourceIsRemote() {
} else {
source, _ := strings.CutSuffix(settings.Source, "/*")
stats, error := os.Stat(source)
if error != nil { if error != nil {
log.Error("encountered an error getting the stats for the source", error.Error()) log.Error("encountered an error getting the stats for the source", error.Error())
} }
if stats.IsDir() { if stats.IsDir() {
log.Info("scanning source...", settings.Source) log.Info("scanning source...", source)
filepath.WalkDir(settings.Source, fillSourceFiles) filepath.WalkDir(source, fillSourceFiles)
log.InfoTimed("found "+strconv.Itoa(len(sourceFiles))+" source files", timestamp.UnixMilli())
} else { } else {
sourceFiles = append(sourceFiles, settings.Source) sourceFiles = append(sourceFiles, settings.Source)
} }
}
log.InfoTimed("found "+strconv.Itoa(len(sourceFiles))+" source files", timestamp.UnixMilli())
return sourceFiles return sourceFiles
} }

View file

@ -11,7 +11,12 @@ import (
// exported function(s) // exported function(s)
func TestConnection() error { func TestConnection() error {
if !settings.TargetIsRemote() { var remote string
if settings.SourceIsRemote() {
remote, _, _ = strings.Cut(settings.Target, ":")
} else if settings.TargetIsRemote() {
remote, _, _ = strings.Cut(settings.Target, ":")
} else {
return nil return nil
} }
var arguments []string var arguments []string
@ -19,11 +24,11 @@ func TestConnection() error {
arguments = append(arguments, "-p", settings.Password) arguments = append(arguments, "-p", settings.Password)
} }
arguments = append(arguments, "ssh") arguments = append(arguments, "ssh")
target, _, _ := strings.Cut(settings.Target, ":")
if len(settings.User) > 0 { if len(settings.User) > 0 {
target = settings.User + "@" + target remote = settings.User + "@" + remote
} }
arguments = append(arguments, target, "'exit'") arguments = append(arguments, remote, "'exit'")
cmd := exec.Command("sshpass", arguments...) cmd := exec.Command("sshpass", arguments...)
stdout, stdoutError := cmd.StdoutPipe() stdout, stdoutError := cmd.StdoutPipe()
stderr, stderrError := cmd.StderrPipe() stderr, stderrError := cmd.StderrPipe()