From 3522976ef789c864b76e389d442ad28494df60f6 Mon Sep 17 00:00:00 2001 From: Russ Magee Date: Thu, 4 Jun 2020 00:53:20 -0700 Subject: [PATCH] Correct implicit username for MSYS2 eg. xs @server.com Signed-off-by: Russ Magee --- xs/xs.go | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/xs/xs.go b/xs/xs.go index f86cb89..69afaa8 100755 --- a/xs/xs.go +++ b/xs/xs.go @@ -256,7 +256,7 @@ func buildCmdRemoteToLocal(copyQuiet bool, copyLimitBPS uint, destPath, files st cmd = xs.GetTool("tar") args = []string{"-xz", "-C", destPath} -} else { + } else { // TODO: Query remote side for total file/dir size bandwidthInBytesPerSec := " -L " + fmt.Sprintf("%d ", copyLimitBPS) displayOpts := " -pre " @@ -309,7 +309,7 @@ func buildCmdLocalToRemote(copyQuiet bool, copyLimitBPS uint, files string) (cap bandwidthInBytesPerSec := " -L " + fmt.Sprintf("%d", copyLimitBPS) displayOpts := " -pre " cmd = xs.GetTool("bash") - args = []string{"-c", xs.GetTool("tar")+" -cz -f /dev/stdout "} + args = []string{"-c", xs.GetTool("tar") + " -cz -f /dev/stdout "} files = strings.TrimSpace(files) // Awesome fact: tar actually can take multiple -C args, and // changes to the dest dir *as it sees each one*. This enables @@ -723,9 +723,9 @@ func main() { // Find out what program we are (shell or copier) myPath := strings.Split(os.Args[0], string(os.PathSeparator)) if myPath[len(myPath)-1] != "xc" && - myPath[len(myPath)-1] != "_xc" && - myPath[len(myPath)-1] != "xc.exe" && - myPath[len(myPath)-1] != "_xc.exe" { + myPath[len(myPath)-1] != "_xc" && + myPath[len(myPath)-1] != "xc.exe" && + myPath[len(myPath)-1] != "_xc.exe" { // xs accepts a command (-x) but not // a srcpath (-r) or dstpath (-t) flag.StringVar(&cmdStr, "x", "", "run <`command`> (if not specified, run interactive shell)") @@ -769,7 +769,7 @@ func main() { var uname string if remoteUser == "" { u, _ := user.Current() // nolint: gosec - uname = u.Username + uname = localUserName(u) } else { uname = remoteUser } @@ -1037,6 +1037,19 @@ func main() { exitWithStatus(int(rec.Status())) } +// currentUser returns the current username minus any OS-specific prefixes +// such as MS Windows workgroup prefixes (eg. workgroup\user). +func localUserName(u *user.User) string { + if u == nil { + log.Fatal("null User?!") + } + + // WinAPI: username may have CIFS prefix %USERDOMAIN%\ + userspec := strings.Split(u.Username, `\`) + username := userspec[len(userspec)-1] + return username +} + func restoreTermState(oldState *xs.State) { _ = xs.Restore(os.Stdin.Fd(), oldState) // nolint: errcheck,gosec }