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/main.go b/main.go index 2a63211..41df371 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "fmt" "math/rand" "os" + "path/filepath" + "runtime" "strings" "time" @@ -77,6 +79,11 @@ func (c *rootCmd) Run(fl *flag.FlagSet) { dir = "~" } + // Get linux relative path if on windows + if runtime.GOOS == "windows" { + dir = gitbashWindowsDir(dir) + } + err := sshCode(host, dir, options{ skipSync: c.skipSync, sshFlags: c.sshFlags, @@ -112,3 +119,14 @@ Arguments: helpTab, ) } + +//This section translates a windows path such as "C:\Users\user" to "\Users\user" +//AND removes the default paths for mingw and git4windows to fix specifying a file path breaking +func gitbashWindowsDir(dir string) (res string) { + res = filepath.ToSlash(dir) + res = strings.Replace(res, "C:", "", -1) + + // If you dont use "C:\mingw64" as the location where you installed mingw, cop this and replace /mingw64 with your install path + res = strings.Replace(res, "/mingw64", "", -1) + return res +} diff --git a/settings.go b/settings.go index ad962a3..bf00d89 100644 --- a/settings.go +++ b/settings.go @@ -24,6 +24,8 @@ func configDir() (string, error) { path = os.ExpandEnv("$HOME/.config/Code/User/") case "darwin": path = os.ExpandEnv("$HOME/Library/Application Support/Code/User/") + case "windows": + return gitbashWindowsDir(os.ExpandEnv("/c/Users/$USERNAME/AppData/Roaming/Code/User")), nil default: return "", xerrors.Errorf("unsupported platform: %s", runtime.GOOS) } @@ -39,6 +41,8 @@ func extensionsDir() (string, error) { switch runtime.GOOS { case "linux", "darwin": path = os.ExpandEnv("$HOME/.vscode/extensions/") + case "windows": + return gitbashWindowsDir(os.ExpandEnv("/c/Users/$USERNAME/.vscode/extensions/")), nil default: return "", xerrors.Errorf("unsupported platform: %s", runtime.GOOS) } diff --git a/sshcode.go b/sshcode.go index ad2235e..1960dcc 100644 --- a/sshcode.go +++ b/sshcode.go @@ -10,6 +10,7 @@ import ( "os/exec" "os/signal" "path/filepath" + "runtime" "strconv" "strings" "syscall" @@ -237,11 +238,14 @@ func openBrowser(url string) { var openCmd *exec.Cmd const ( - macPath = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" - wslPath = "/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe" + macPath = "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" + wslPath = "/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe" + msysPath = "/Program Files (x86)/Google/Chrome/Application/chrome.exe" ) switch { + case commandExists("chrome"): + openCmd = exec.Command("chrome", chromeOptions(url)...) case commandExists("google-chrome"): openCmd = exec.Command("google-chrome", chromeOptions(url)...) case commandExists("google-chrome-stable"): @@ -254,6 +258,8 @@ func openBrowser(url string) { openCmd = exec.Command(macPath, chromeOptions(url)...) case pathExists(wslPath): openCmd = exec.Command(wslPath, chromeOptions(url)...) + case pathExists(msysPath): + openCmd = exec.Command(msysPath, chromeOptions(url)...) default: err := browser.OpenURL(url) if err != nil { @@ -308,6 +314,13 @@ func randomPort() (string, error) { // checkSSHDirectory performs sanity and safety checks on sshDirectory, and // returns a new value for o.reuseConnection depending on the checks. func checkSSHDirectory(sshDirectory string, reuseConnection bool) bool { + + if runtime.GOOS == "windows" { + flog.Info("OS is windows, disabling connection reuse feature") + //reuseConnection = false + return false + } + sshDirectoryMode, err := os.Lstat(expandPath(sshDirectory)) if err != nil { if reuseConnection { @@ -410,7 +423,7 @@ func syncUserSettings(sshFlags string, host string, back bool) error { return err } - const remoteSettingsDir = "~/.local/share/code-server/User/" + const remoteSettingsDir = ".local/share/code-server/User/" var ( src = localConfDir + "/" @@ -436,7 +449,7 @@ func syncExtensions(sshFlags string, host string, back bool) error { return err } - const remoteExtensionsDir = "~/.local/share/code-server/extensions/" + const remoteExtensionsDir = ".local/share/code-server/extensions/" var ( src = localExtensionsDir + "/" @@ -483,7 +496,7 @@ func downloadScript(codeServerPath string) string { [ "$(uname -m)" != "x86_64" ] && echo "Unsupported server architecture $(uname -m). code-server only has releases for x86_64 systems." && exit 1 pkill -f %v || true -mkdir -p ~/.local/share/code-server %v +mkdir -p $HOME/.local/share/code-server %v cd %v curlflags="-o latest-linux" if [ -f latest-linux ]; then @@ -494,8 +507,8 @@ curl $curlflags 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)), codeServerPath, codeServerPath, codeServerPath,