diff --git a/cmd/wsh/cmd/wshcmd-file-util.go b/cmd/wsh/cmd/wshcmd-file-util.go index 432cc1b1f..119294035 100644 --- a/cmd/wsh/cmd/wshcmd-file-util.go +++ b/cmd/wsh/cmd/wshcmd-file-util.go @@ -8,6 +8,7 @@ import ( "fmt" "io" "io/fs" + "log" "strings" "github.com/wavetermdev/waveterm/pkg/remote/connparse" @@ -210,10 +211,12 @@ func streamFileList(zoneId string, path string, recursive bool, filesOnly bool) } func fixRelativePaths(path string) (string, error) { + log.Printf("fixRelativePaths: path=%q", path) conn, err := connparse.ParseURI(path) if err != nil { return "", err } + log.Printf("fixRelativePaths: conn=%v", conn) if conn.Scheme == connparse.ConnectionTypeWsh { if conn.Host == connparse.ConnHostCurrent { conn.Host = RpcContext.Conn diff --git a/pkg/remote/connparse/connparse.go b/pkg/remote/connparse/connparse.go index 1114c3dfd..bb37cdb84 100644 --- a/pkg/remote/connparse/connparse.go +++ b/pkg/remote/connparse/connparse.go @@ -6,6 +6,7 @@ package connparse import ( "context" "fmt" + "log" "regexp" "strings" @@ -87,29 +88,46 @@ func GetConnNameFromContext(ctx context.Context) (string, error) { // ParseURI parses a connection URI and returns the connection type, host/path, and parameters. func ParseURI(uri string) (*Connection, error) { - split := strings.SplitN(uri, "://", 2) + split := strings.SplitN(uri, "//", 2) var scheme string var rest string if len(split) > 1 { - scheme = split[0] + scheme = strings.TrimSuffix(split[0], ":") rest = split[1] } else { rest = split[0] } + log.Printf("parseURI: scheme=%q, rest=%q", scheme, rest) + var host string var remotePath string + + parseGenericPath := func() { + split = strings.SplitN(rest, "/", 2) + host = split[0] + if len(split) > 1 { + remotePath = split[1] + } else { + remotePath = "/" + } + log.Printf("parseGenericPath: host=%q, remotePath=%q", host, remotePath) + } + parseWshPath := func() { + if strings.HasPrefix(rest, "wsl://") { + host = wslConnRegex.FindString(rest) + remotePath = strings.TrimPrefix(rest, host) + log.Printf("parseWshPath: host=%q, remotePath=%q", host, remotePath) + } else { + parseGenericPath() + } + } + if scheme == "" { scheme = ConnectionTypeWsh - if strings.HasPrefix(rest, "//") { - rest = strings.TrimPrefix(rest, "//") - split = strings.SplitN(rest, "/", 2) - host = split[0] - if len(split) > 1 { - remotePath = split[1] - } else { - remotePath = "/" - } + if len(rest) != len(uri) { + // This accounts for when the uri starts with "//", which would get trimmed in the first split. + parseWshPath() } else if strings.HasPrefix(rest, "/~") { host = wshrpc.LocalConnName remotePath = rest @@ -117,19 +135,10 @@ func ParseURI(uri string) (*Connection, error) { host = ConnHostCurrent remotePath = rest } + } else if scheme == ConnectionTypeWsh { + parseWshPath() } else { - if strings.HasPrefix(rest, "wsl://") { - host = wslConnRegex.FindString(rest) - remotePath = strings.TrimPrefix(rest, host) - } else { - split = strings.SplitN(rest, "/", 2) - host = split[0] - if len(split) > 1 { - remotePath = split[1] - } else { - remotePath = "/" - } - } + parseGenericPath() } if scheme == ConnectionTypeWsh { diff --git a/pkg/util/fileutil/fileutil.go b/pkg/util/fileutil/fileutil.go index 84d9e1e88..b5bbfbb1b 100644 --- a/pkg/util/fileutil/fileutil.go +++ b/pkg/util/fileutil/fileutil.go @@ -19,7 +19,7 @@ import ( func FixPath(path string) (string, error) { if strings.HasPrefix(path, "~") { - return filepath.Join(wavebase.GetHomeDir(), path[1:]), nil + path = filepath.Join(wavebase.GetHomeDir(), path[1:]) } else if !filepath.IsAbs(path) { log.Printf("FixPath: path is not absolute: %s", path) path, err := filepath.Abs(path) @@ -27,10 +27,8 @@ func FixPath(path string) (string, error) { return "", err } log.Printf("FixPath: fixed path: %s", path) - return path, nil - } else { - return path, nil } + return path, nil } const (