gosync/settings/arguments.go

98 lines
1.9 KiB
Go
Raw Permalink Normal View History

2023-09-07 15:20:19 +02:00
package settings
import (
"os"
"runtime"
"strconv"
"strings"
"time"
2023-09-07 15:33:35 +02:00
"velvettear/gosync/help"
2023-09-07 15:20:19 +02:00
"velvettear/gosync/log"
)
// exported function(s)
func Initialize() {
var arguments []string
2023-09-22 16:28:09 +02:00
for index := 1; index < len(os.Args); index++ {
arg := strings.ToLower(os.Args[index])
if arg != "-v" && arg != "--verbose" {
2023-09-22 16:42:59 +02:00
arguments = append(arguments, os.Args[index])
continue
}
setVerbose(true)
}
2023-09-22 16:28:09 +02:00
var filteredArguments []string
for index := 0; index < len(arguments); index++ {
2023-09-22 16:42:59 +02:00
switch strings.ToLower(arguments[index]) {
2023-09-07 15:33:35 +02:00
case "-h":
fallthrough
case "--help":
help.Print()
os.Exit(0)
2023-09-07 15:20:19 +02:00
case "-c":
fallthrough
case "--concurrency":
2023-09-08 13:29:45 +02:00
index++
2023-09-22 16:28:09 +02:00
if index < len(arguments) {
concurrency, error := strconv.Atoi(arguments[index])
if error != nil {
break
2023-09-07 15:20:19 +02:00
}
setConcurrency(concurrency)
2023-09-07 15:20:19 +02:00
}
case "-d":
fallthrough
case "--delay":
index++
2023-09-22 16:28:09 +02:00
if index < len(arguments) {
delay, error := strconv.Atoi(arguments[index])
if error != nil {
break
}
setDelay(time.Duration(delay) * time.Millisecond)
2023-09-07 15:20:19 +02:00
}
case "-p":
fallthrough
case "--password":
2023-09-08 13:29:45 +02:00
index++
2023-09-22 16:28:09 +02:00
if index > len(arguments) {
2023-09-07 15:20:19 +02:00
break
}
2023-09-22 16:28:09 +02:00
setPassword(arguments[index])
2023-09-07 15:20:19 +02:00
case "-u":
fallthrough
case "--user":
2023-09-08 13:29:45 +02:00
index++
2023-09-22 16:28:09 +02:00
if index > len(arguments) {
2023-09-07 15:20:19 +02:00
break
}
2023-09-22 16:28:09 +02:00
setUser(arguments[index])
2023-09-07 15:20:19 +02:00
default:
2023-09-22 16:28:09 +02:00
filteredArguments = append(filteredArguments, arguments[index])
2023-09-07 15:20:19 +02:00
}
}
2023-09-22 16:28:09 +02:00
if len(arguments) < 2 {
2023-09-07 15:33:35 +02:00
log.Fatal("error: missing arguments")
}
2023-09-22 16:28:09 +02:00
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)
}
2023-09-07 15:20:19 +02:00
}
setDefaults()
}
// unexported function(s)
func setDefaults() {
if Concurrency == 0 {
setConcurrency(runtime.NumCPU())
}
if Delay == 0 {
setDelay(100)
}
2023-09-07 15:20:19 +02:00
}