diff --git a/.vscode/launch.json b/.vscode/launch.json index 490939f..415d164 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,12 +8,12 @@ "mode": "auto", "program": "${workspaceFolder}/main.go", "args": [ - "/home/velvettear/downloads/music", - "192.168.1.11:/tmp", + "/home/velvettear/downloads/music/*", + "192.168.1.11:/share/music", "--password", "$Velvet90", "--concurrency", - "4", + "12", "--verbose", ], "console": "integratedTerminal" diff --git a/log/log.go b/log/log.go index 91e0cbb..e4ff346 100644 --- a/log/log.go +++ b/log/log.go @@ -14,7 +14,7 @@ const LEVEL_WARNING = 2 const LEVEL_ERROR = 3 const LEVEL_FATAL = 4 -var logLevel = 0 +var logLevel = 1 // exported functions func SetLogLevel(level int) { diff --git a/main.go b/main.go index 4d7b5f3..33828ae 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,8 @@ import ( "velvettear/gosync/tools" ) +// version := "0.1" + func main() { timestamp := time.Now() settings.Initialize() diff --git a/settings/arguments.go b/settings/arguments.go index eefb662..5fc5380 100644 --- a/settings/arguments.go +++ b/settings/arguments.go @@ -13,18 +13,20 @@ import ( func Initialize() { os.Args = os.Args[1:] 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, arg := range os.Args { switch strings.ToLower(os.Args[index]) { case "-h": fallthrough case "--help": help.Print() os.Exit(0) - case "-v": - fallthrough - case "--verbose": - setVerbose(true) case "-c": fallthrough case "--concurrency": @@ -65,11 +67,11 @@ func Initialize() { } setSource(arguments[0]) setTarget(arguments[1]) - _, error := os.Stat(Source) - if os.IsNotExist(error) { - log.Fatal("given source does not exist", Source) - } - if !Verbose { - setVerbose(false) + if !SourceIsRemote() { + source, _ := strings.CutSuffix(Source, "/*") + _, error := os.Stat(source) + if os.IsNotExist(error) { + log.Fatal("given source does not exist", source) + } } } diff --git a/settings/variables.go b/settings/variables.go index e8d8645..045859d 100644 --- a/settings/variables.go +++ b/settings/variables.go @@ -16,10 +16,22 @@ var User string // exported function(s) 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) +func isRemote(target string) bool { + return strings.Contains(target, ":") +} + func setVerbose(verbose bool) { Verbose = verbose if Verbose { diff --git a/tools/rsync.go b/tools/rsync.go index daa2bc7..527f0b6 100644 --- a/tools/rsync.go +++ b/tools/rsync.go @@ -164,11 +164,12 @@ func transferFile(bar *mpb.Bar, file string) bool { return true } -func getTargetLocation(source string) string { - if source == settings.Source { - return filepath.Join(settings.Target, filepath.Base(source)) +func getTargetLocation(sourceFile string) string { + if sourceFile == settings.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 { diff --git a/tools/scanner.go b/tools/scanner.go index 2118813..f77e1f4 100644 --- a/tools/scanner.go +++ b/tools/scanner.go @@ -5,6 +5,7 @@ import ( "os" "path/filepath" "strconv" + "strings" "time" "velvettear/gosync/log" "velvettear/gosync/settings" @@ -15,17 +16,22 @@ var sourceFiles []string // unexported function(s) func getSourceFiles() []string { timestamp := time.Now() - stats, error := os.Stat(settings.Source) - if error != nil { - log.Error("encountered an error getting the stats for the source", error.Error()) - } - if stats.IsDir() { - log.Info("scanning source...", settings.Source) - filepath.WalkDir(settings.Source, fillSourceFiles) - log.InfoTimed("found "+strconv.Itoa(len(sourceFiles))+" source files", timestamp.UnixMilli()) + if settings.SourceIsRemote() { + } else { - sourceFiles = append(sourceFiles, settings.Source) + source, _ := strings.CutSuffix(settings.Source, "/*") + stats, error := os.Stat(source) + if error != nil { + log.Error("encountered an error getting the stats for the source", error.Error()) + } + if stats.IsDir() { + log.Info("scanning source...", source) + filepath.WalkDir(source, fillSourceFiles) + } else { + sourceFiles = append(sourceFiles, settings.Source) + } } + log.InfoTimed("found "+strconv.Itoa(len(sourceFiles))+" source files", timestamp.UnixMilli()) return sourceFiles } diff --git a/tools/ssh.go b/tools/ssh.go index 84aabae..fd8a997 100644 --- a/tools/ssh.go +++ b/tools/ssh.go @@ -11,7 +11,12 @@ import ( // exported function(s) 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 } var arguments []string @@ -19,11 +24,11 @@ func TestConnection() error { arguments = append(arguments, "-p", settings.Password) } arguments = append(arguments, "ssh") - target, _, _ := strings.Cut(settings.Target, ":") + 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...) stdout, stdoutError := cmd.StdoutPipe() stderr, stderrError := cmd.StderrPipe()