gosync/settings/arguments.go

75 lines
1.4 KiB
Go

package settings
import (
"os"
"runtime"
"strconv"
"strings"
"velvettear/gosync/help"
"velvettear/gosync/log"
)
// exported function(s)
func Initialize() {
os.Args = os.Args[1:]
var arguments []string
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":
index++
var concurrency int
if index < len(os.Args) {
tmp, error := strconv.Atoi(os.Args[index])
if error == nil {
concurrency = tmp
}
}
if concurrency == 0 {
concurrency = runtime.NumCPU()
}
setConcurrency(concurrency)
case "-p":
fallthrough
case "--password":
index++
if index > len(os.Args) {
break
}
setPassword(os.Args[index])
case "-u":
fallthrough
case "--user":
index++
if index > len(os.Args) {
break
}
setUser(os.Args[index])
default:
arguments = append(arguments, os.Args[index])
}
}
if len(os.Args) < 2 {
log.Fatal("error: missing arguments")
}
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)
}
}