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",
"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"

View file

@ -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) {

View file

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

View file

@ -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 !SourceIsRemote() {
source, _ := strings.CutSuffix(Source, "/*")
_, error := os.Stat(source)
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)
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 {

View file

@ -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 {

View file

@ -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 settings.SourceIsRemote() {
} else {
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...", settings.Source)
filepath.WalkDir(settings.Source, fillSourceFiles)
log.InfoTimed("found "+strconv.Itoa(len(sourceFiles))+" source files", timestamp.UnixMilli())
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
}

View file

@ -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()