From 151c9940861d94386d5d35ea7fe0801a63508242 Mon Sep 17 00:00:00 2001 From: Eduardo Argollo Date: Mon, 8 Jul 2019 18:06:53 -0300 Subject: [PATCH 1/2] Adding git bash support --- .gitignore | 1 + README.md | 1 + main.go | 30 ++++++++++++++++++++++++++++++ settings.go | 6 ++++++ sshcode.go | 6 ++++-- 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index dc0daa9..52d5d04 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ vendor bin .vscode sshcode +sshcode.exe \ No newline at end of file diff --git a/README.md b/README.md index dddd19e..8ce0c4d 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ We currently support: - Linux - MacOS - WSL +- Windows Git Bash (MINGW64) - Requires [rsynch for Windows](http://repo.msys2.org/msys/x86_64/rsync-3.1.3-1-x86_64.pkg.tar.xz) For the remote server, we currently only support Linux `x86_64` (64-bit) servers with `glibc`. `musl` libc (which is most notably used by Alpine Linux) diff --git a/main.go b/main.go index 2a63211..4b82a9a 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,9 @@ import ( "fmt" "math/rand" "os" + "os/user" + "path/filepath" + "runtime" "strings" "time" @@ -77,6 +80,12 @@ func (c *rootCmd) Run(fl *flag.FlagSet) { dir = "~" } + // Get linux relative path if on windows + if runtime.GOOS == "windows" { + dir = relativeWindowsPath(dir) + fmt.Printf("relative path is %s\n", dir) + } + err := sshCode(host, dir, options{ skipSync: c.skipSync, sshFlags: c.sshFlags, @@ -112,3 +121,24 @@ Arguments: helpTab, ) } + +func relativeWindowsPath(dir string) string { + fmt.Printf("Received '%s'\n", dir) + usr, err := user.Current() + if err != nil { + fmt.Printf("Could not get user: %v", err) + return dir + } + rel, err := filepath.Rel(usr.HomeDir, dir) + if err != nil { + return dir + } + rel = "~/" + filepath.ToSlash(rel) + return rel +} + +func gitbashWindowsDir(dir string) (res string) { + res = filepath.ToSlash(dir) + res = "/" + strings.Replace(res, ":", "", -1) + return res +} diff --git a/settings.go b/settings.go index ad962a3..a3bd1cd 100644 --- a/settings.go +++ b/settings.go @@ -24,6 +24,9 @@ func configDir() (string, error) { path = os.ExpandEnv("$HOME/.config/Code/User/") case "darwin": path = os.ExpandEnv("$HOME/Library/Application Support/Code/User/") + case "windows": + // Can't use the filepath.Clean function to keep Linux format path that works well with gitbash + return gitbashWindowsDir(os.ExpandEnv("$HOME/.config/Code/User")), nil default: return "", xerrors.Errorf("unsupported platform: %s", runtime.GOOS) } @@ -39,6 +42,9 @@ func extensionsDir() (string, error) { switch runtime.GOOS { case "linux", "darwin": path = os.ExpandEnv("$HOME/.vscode/extensions/") + case "windows": + // Can't use the filepath.Clean function to keep Linux format path that works well with gitbash + return gitbashWindowsDir(os.ExpandEnv("$HOME/.vscode/extensions")), nil default: return "", xerrors.Errorf("unsupported platform: %s", runtime.GOOS) } diff --git a/sshcode.go b/sshcode.go index f525126..3c172ea 100644 --- a/sshcode.go +++ b/sshcode.go @@ -490,8 +490,10 @@ wget -N https://codesrv-ci.cdr.sh/latest-linux ln latest-linux %v chmod +x %v`, codeServerPath, - filepath.Dir(codeServerPath), - filepath.Dir(codeServerPath), + filepath.ToSlash(filepath.Dir(codeServerPath)), + filepath.ToSlash(filepath.Dir(codeServerPath)), + // filepath.Dir(codeServerPath), + // filepath.Dir(codeServerPath), codeServerPath, codeServerPath, codeServerPath, From 11274d37a9bb357e235e40016b730b8dae1b18cf Mon Sep 17 00:00:00 2001 From: Eduardo Argollo Date: Mon, 29 Jul 2019 21:09:07 -0300 Subject: [PATCH 2/2] Addressing code review --- README.md | 2 +- main.go | 5 ++--- sshcode.go | 2 -- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 8ce0c4d..802b704 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ We currently support: - Linux - MacOS - WSL -- Windows Git Bash (MINGW64) - Requires [rsynch for Windows](http://repo.msys2.org/msys/x86_64/rsync-3.1.3-1-x86_64.pkg.tar.xz) +- Windows Git Bash (MINGW64) - Requires rsync for Windows. To install rsync download it [here](http://repo.msys2.org/msys/x86_64/rsync-3.1.3-1-x86_64.pkg.tar.xz) and run `tar xvf rsync-3.1.3-1-x86_64.pkg.tar.xz` on Git bash. Don't forget to copy the executable to a folder in the PATH ` cp usr/bin/rsync.exe /mingw64/bin`. For the remote server, we currently only support Linux `x86_64` (64-bit) servers with `glibc`. `musl` libc (which is most notably used by Alpine Linux) diff --git a/main.go b/main.go index 4b82a9a..b26a837 100644 --- a/main.go +++ b/main.go @@ -83,7 +83,6 @@ func (c *rootCmd) Run(fl *flag.FlagSet) { // Get linux relative path if on windows if runtime.GOOS == "windows" { dir = relativeWindowsPath(dir) - fmt.Printf("relative path is %s\n", dir) } err := sshCode(host, dir, options{ @@ -123,10 +122,9 @@ Arguments: } func relativeWindowsPath(dir string) string { - fmt.Printf("Received '%s'\n", dir) usr, err := user.Current() if err != nil { - fmt.Printf("Could not get user: %v", err) + flog.Error("Could not get user: %v", err) return dir } rel, err := filepath.Rel(usr.HomeDir, dir) @@ -137,6 +135,7 @@ func relativeWindowsPath(dir string) string { return rel } +// gitbashWindowsDir translates a directory similar to `C:\Users\username\path` to `~/path` for compatibility with Git bash. func gitbashWindowsDir(dir string) (res string) { res = filepath.ToSlash(dir) res = "/" + strings.Replace(res, ":", "", -1) diff --git a/sshcode.go b/sshcode.go index b62cbc4..5235c1d 100644 --- a/sshcode.go +++ b/sshcode.go @@ -496,8 +496,6 @@ chmod +x %v`, codeServerPath, filepath.ToSlash(filepath.Dir(codeServerPath)), filepath.ToSlash(filepath.Dir(codeServerPath)), - // filepath.Dir(codeServerPath), - // filepath.Dir(codeServerPath), codeServerPath, codeServerPath, codeServerPath,