Skip to content

Commit

Permalink
Merge pull request #582 from milkpirate/win
Browse files Browse the repository at this point in the history
feat(window / git bash): Make assh work on Windows cmd and Git bash
  • Loading branch information
moul committed Oct 15, 2022
2 parents 89fc54a + c2edcaa commit 610085d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 18 deletions.
4 changes: 4 additions & 0 deletions pkg/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"path/filepath"

"moul.io/assh/v2/pkg/utils"

"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -42,6 +44,8 @@ func init() {
if err != nil {
log.Fatal(err)
}
abspath = filepath.ToSlash(abspath)
abspath = utils.EscapeSpaces(abspath)
config.SetASSHBinaryPath(abspath)

RootCmd.Flags().BoolP("help", "h", false, "print usage")
Expand Down
14 changes: 12 additions & 2 deletions pkg/utils/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,15 @@ package utils
import (
"errors"
"os"
"path/filepath"
"strings"
)

// EscapeSpaces escapes all space characters with a backslash (\)
func EscapeSpaces(s string) string {
return strings.ReplaceAll(s, " ", "\\ ")
}

// GetHomeDir returns '~' as a path
func GetHomeDir() string {
if homeDir := os.Getenv("HOME"); homeDir != "" {
Expand Down Expand Up @@ -42,14 +48,18 @@ func ExpandUser(path string) (string, error) {
// Expand variables
path = ExpandEnvSafe(path)

if path[:2] == "~/" {
if strings.HasPrefix(path, "~/") {
homeDir := GetHomeDir()
if homeDir == "" {
return "", errors.New("user home directory not found")
}

return strings.Replace(path, "~", homeDir, 1), nil
path = strings.Replace(path, "~", homeDir, 1)
}

path = filepath.FromSlash(path) // OS-agnostic slashes
path = EscapeSpaces(path)

return path, nil
}

Expand Down
59 changes: 43 additions & 16 deletions pkg/utils/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,30 @@ package utils

import (
"os"
"runtime"
"testing"

. "github.com/smartystreets/goconvey/convey"
)

func TestEscapeSpaces(t *testing.T) {
Convey("Testing EscapeSpaces", t, func() {
So(EscapeSpaces("foo bar"), ShouldEqual, "foo\\ bar")
So(EscapeSpaces("/a/b c/d"), ShouldEqual, "/a/b\\ c/d")
})
}

func TestExpandEnvSafe(t *testing.T) {
Convey("Testing ExpandEnvSafe", t, func() {
So(os.Setenv("FOO", "bar"), ShouldBeNil)
So(ExpandEnvSafe("/a/$FOO/c"), ShouldEqual, "/a/bar/c")
So(ExpandEnvSafe("/a/${FOO}/c"), ShouldEqual, "/a/bar/c")
So(ExpandEnvSafe("/a/${FOO}/c/$FOO"), ShouldEqual, "/a/bar/c/bar")
So(ExpandEnvSafe("/a/$(FOO)/c"), ShouldEqual, "/a/$(FOO)/c")
So(ExpandEnvSafe(""), ShouldEqual, "")
})
}

func TestGetHomeDir(t *testing.T) {
Convey("Testing GetHomeDir", t, func() {
oldHome := os.Getenv("HOME")
Expand Down Expand Up @@ -42,38 +61,46 @@ func TestGetHomeDir(t *testing.T) {
}

func TestExpandUser(t *testing.T) {
expected := "/a/b/c/test"
expectedEscaped := "/a/b/c/test\\ dir"

if runtime.GOOS == "windows" {
expected = "\\a\\b\\c\\test"
expectedEscaped = "\\a\\b\\c\\test\\ dir"
}

Convey("Testing ExpandUser", t, func() {
oldHome := os.Getenv("HOME")
oldUserProfile := os.Getenv("USERPROFILE")

So(os.Setenv("HOME", "/a/b/c"), ShouldBeNil)
So(os.Setenv("USERPROFILE", ""), ShouldBeNil)
dir, err := ExpandUser("~/test")
So(dir, ShouldEqual, "/a/b/c/test")
dir, err := ExpandUser("~/test dir")
So(dir, ShouldEqual, expectedEscaped)
So(err, ShouldBeNil)

So(os.Setenv("HOME", "/a/b/d"), ShouldBeNil)
So(os.Setenv("HOME", "/a/b/c"), ShouldBeNil)
So(os.Setenv("USERPROFILE", ""), ShouldBeNil)
dir, err = ExpandUser("~/test")
So(dir, ShouldEqual, "/a/b/d/test")
So(dir, ShouldEqual, expected)
So(err, ShouldBeNil)

So(os.Setenv("HOME", "/a/b/d"), ShouldBeNil)
So(os.Setenv("USERPROFILE", "/a/b/e"), ShouldBeNil)
So(os.Setenv("HOME", "/a/b/c"), ShouldBeNil)
So(os.Setenv("USERPROFILE", ""), ShouldBeNil)
dir, err = ExpandUser("~/test")
So(dir, ShouldEqual, "/a/b/d/test")
So(dir, ShouldEqual, expected)
So(err, ShouldBeNil)

So(os.Setenv("HOME", ""), ShouldBeNil)
So(os.Setenv("USERPROFILE", "/a/b/f"), ShouldBeNil)
So(os.Setenv("HOME", "/a/b/c"), ShouldBeNil)
So(os.Setenv("USERPROFILE", "/a/b/e"), ShouldBeNil)
dir, err = ExpandUser("~/test")
So(dir, ShouldEqual, "/a/b/f/test")
So(dir, ShouldEqual, expected)
So(err, ShouldBeNil)

So(os.Setenv("HOME", ""), ShouldBeNil)
So(os.Setenv("USERPROFILE", "/a/b/g"), ShouldBeNil)
So(os.Setenv("USERPROFILE", "/a/b/c"), ShouldBeNil)
dir, err = ExpandUser("~/test")
So(dir, ShouldEqual, "/a/b/g/test")
So(dir, ShouldEqual, expected)
So(err, ShouldBeNil)

So(os.Setenv("HOME", ""), ShouldBeNil)
Expand All @@ -85,25 +112,25 @@ func TestExpandUser(t *testing.T) {
So(os.Setenv("HOME", ""), ShouldBeNil)
So(os.Setenv("USERPROFILE", ""), ShouldBeNil)
dir, err = ExpandUser("/a/b/c/test")
So(dir, ShouldEqual, "/a/b/c/test")
So(dir, ShouldEqual, expected)
So(err, ShouldBeNil)

So(os.Setenv("HOME", "/e/f"), ShouldBeNil)
So(os.Setenv("USERPROFILE", ""), ShouldBeNil)
dir, err = ExpandUser("/a/b/c/test")
So(dir, ShouldEqual, "/a/b/c/test")
So(dir, ShouldEqual, expected)
So(err, ShouldBeNil)

So(os.Setenv("HOME", ""), ShouldBeNil)
So(os.Setenv("USERPROFILE", "/e/g"), ShouldBeNil)
dir, err = ExpandUser("/a/b/c/test")
So(dir, ShouldEqual, "/a/b/c/test")
So(dir, ShouldEqual, expected)
So(err, ShouldBeNil)

So(os.Setenv("HOME", "/e/h"), ShouldBeNil)
So(os.Setenv("USERPROFILE", "/e/i"), ShouldBeNil)
dir, err = ExpandUser("/a/b/c/test")
So(dir, ShouldEqual, "/a/b/c/test")
So(dir, ShouldEqual, expected)
So(err, ShouldBeNil)

So(os.Setenv("HOME", oldHome), ShouldBeNil)
Expand Down

0 comments on commit 610085d

Please sign in to comment.