Simplified hostPortPath parsing (colons mandatory if specifying more than just host)

This commit is contained in:
Russ Magee 2018-07-29 13:22:35 -07:00
parent 55cf5a9277
commit c6bfa2771b
1 changed files with 26 additions and 18 deletions

View File

@ -74,30 +74,27 @@ func parseNonSwitchArgs(a []string, dp string) (user, host, port, path string, i
fancyHostPortPath = strings.Split(fancyArg[1], ":") fancyHostPortPath = strings.Split(fancyArg[1], ":")
} }
// [...@]host[:port[:path]]
if len(fancyHostPortPath) > 2 { if len(fancyHostPortPath) > 2 {
// [user]@host[:port]:path
fancyPath = fancyHostPortPath[2] fancyPath = fancyHostPortPath[2]
} } else if len(fancyHostPortPath) > 1 {
if len(fancyHostPortPath) > 1 {
// [user]@host:port[:...] or [user]@host:path (default port)
fancyPort = fancyHostPortPath[1] fancyPort = fancyHostPortPath[1]
} }
// [user]@host[:...[:...]]
fancyHost = fancyHostPortPath[0] fancyHost = fancyHostPortPath[0]
if fancyPort == "" { if fancyPort == "" {
fancyPort = dp fancyPort = dp
} }
//if fancyPath == "" { if fancyPath == "" {
// fancyPath = "." fancyPath = "."
//} }
if i == len(a)-1 { if i == len(a)-1 {
isDest = true isDest = true
fmt.Println("isDest") fmt.Println("isDest")
} }
//fmt.Println("fancyArgs: user:", fancyUser, "host:", fancyHost, "port:", fancyPort, "path:", fancyPath) fmt.Println("fancyArgs: user:", fancyUser, "host:", fancyHost, "port:", fancyPort, "path:", fancyPath)
} else { } else {
otherArgs = append(otherArgs, a[i]) otherArgs = append(otherArgs, a[i])
} }
@ -126,7 +123,7 @@ func main() {
var server string var server string
var cmdStr string var cmdStr string
var copySrc string var copySrc []byte
var copyDst string var copyDst string
var altUser string var altUser string
@ -157,18 +154,19 @@ func main() {
// hkexsh accepts a command (-x) but not // hkexsh accepts a command (-x) but not
// a srcpath (-r) or dstpath (-t) // a srcpath (-r) or dstpath (-t)
flag.StringVar(&cmdStr, "x", "", "command to run (default empty - interactive shell)") flag.StringVar(&cmdStr, "x", "", "command to run (default empty - interactive shell)")
} else { } // else {
// hkexcp accepts srcpath (-r) and dstpath (-t), but not //// hkexcp accepts srcpath (-r) and dstpath (-t), but not
// a command (-x) //// a command (-x)
flag.StringVar(&copySrc, "r", "", "copy srcpath") //flag.StringVar(&copySrc, "r", "", "copy srcpath")
flag.StringVar(&copyDst, "t", "", "copy dstpath") //flag.StringVar(&copyDst, "t", "", "copy dstpath")
} //}
flag.Parse() flag.Parse()
fancyUser, fancyHost, fancyPort, fancyPath, pathIsDest, otherArgs := fancyUser, fancyHost, fancyPort, fancyPath, pathIsDest, otherArgs :=
parseNonSwitchArgs(flag.Args(), defPort /* defPort */) parseNonSwitchArgs(flag.Args(), defPort /* defPort */)
fmt.Println("otherArgs:", otherArgs) fmt.Println("otherArgs:", otherArgs)
//fmt.Println("fancyHost:", fancyHost) //fmt.Println("fancyHost:", fancyHost)
fmt.Println("fancyPath:", fancyPath)
if fancyUser != "" { if fancyUser != "" {
altUser = fancyUser altUser = fancyUser
} }
@ -177,10 +175,20 @@ func main() {
//fmt.Println("fancyHost sets server to", server) //fmt.Println("fancyHost sets server to", server)
} }
if fancyPath != "" { if fancyPath != "" {
// -if pathIsSrc && len(otherArgs) > 1 ERROR
// -else flatten otherArgs into space-delim list => copySrc
if pathIsDest { if pathIsDest {
for _, v := range otherArgs {
copySrc = append(copySrc, ' ')
copySrc = append(copySrc, v...)
}
fmt.Println(">> copySrc:", string(copySrc))
copyDst = fancyPath copyDst = fancyPath
} else { } else {
copySrc = fancyPath if len(otherArgs) > 1 {
log.Fatal("ERROR: cannot specify more than one dest path for copy")
}
copySrc = []byte(fancyPath)
} }
} }
@ -197,7 +205,7 @@ func main() {
} }
if len(cmdStr) != 0 && (len(copySrc) != 0 || len(copyDst) != 0) { if len(cmdStr) != 0 && (len(copySrc) != 0 || len(copyDst) != 0) {
log.Fatal("incompatible options -- either cmd (-x) or copy ops (-r,-t), but not both") log.Fatal("incompatible options -- either cmd (-x) or copy ops but not both")
} }
if dbg { if dbg {