gosync/settings/arguments.go
2023-09-22 16:42:59 +02:00

97 lines
1.9 KiB
Go

package settings
import (
"os"
"runtime"
"strconv"
"strings"
"time"
"velvettear/gosync/help"
"velvettear/gosync/log"
)
// exported function(s)
func Initialize() {
var arguments []string
for index := 1; index < len(os.Args); index++ {
arg := strings.ToLower(os.Args[index])
if arg != "-v" && arg != "--verbose" {
arguments = append(arguments, os.Args[index])
continue
}
setVerbose(true)
}
var filteredArguments []string
for index := 0; index < len(arguments); index++ {
switch strings.ToLower(arguments[index]) {
case "-h":
fallthrough
case "--help":
help.Print()
os.Exit(0)
case "-c":
fallthrough
case "--concurrency":
index++
if index < len(arguments) {
concurrency, error := strconv.Atoi(arguments[index])
if error != nil {
break
}
setConcurrency(concurrency)
}
case "-d":
fallthrough
case "--delay":
index++
if index < len(arguments) {
delay, error := strconv.Atoi(arguments[index])
if error != nil {
break
}
setDelay(time.Duration(delay) * time.Millisecond)
}
case "-p":
fallthrough
case "--password":
index++
if index > len(arguments) {
break
}
setPassword(arguments[index])
case "-u":
fallthrough
case "--user":
index++
if index > len(arguments) {
break
}
setUser(arguments[index])
default:
filteredArguments = append(filteredArguments, arguments[index])
}
}
if len(arguments) < 2 {
log.Fatal("error: missing arguments")
}
setSource(filteredArguments[0])
setTarget(filteredArguments[1])
if !SourceIsRemote() {
source, _ := strings.CutSuffix(Source, "/*")
_, error := os.Stat(source)
if os.IsNotExist(error) {
log.Fatal("given source does not exist", source)
}
}
setDefaults()
}
// unexported function(s)
func setDefaults() {
if Concurrency == 0 {
setConcurrency(runtime.NumCPU())
}
if Delay == 0 {
setDelay(100)
}
}