46 lines
1.4 KiB
Go
46 lines
1.4 KiB
Go
|
|
// Package platform 封装操作系统差异,包括隐藏子进程和 WSL 路径转换。
|
|||
|
|
package platform
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"errors"
|
|||
|
|
"path/filepath"
|
|||
|
|
"strings"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// PathDescriptor 是项目路径的标准描述。WindowsPath 用于 Go 文件扫描,LinuxPath 用于 WSL 内 Git。
|
|||
|
|
type PathDescriptor struct {
|
|||
|
|
Original string
|
|||
|
|
WindowsPath string
|
|||
|
|
Distro string
|
|||
|
|
LinuxPath string
|
|||
|
|
WSL bool
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ResolvePath 识别两种 WSL UNC 前缀,并转换为 WSL 内部可识别的 Linux 绝对路径。
|
|||
|
|
func ResolvePath(raw string) (PathDescriptor, error) {
|
|||
|
|
p := filepath.Clean(strings.TrimSpace(raw))
|
|||
|
|
if p == "" {
|
|||
|
|
return PathDescriptor{}, errors.New("PATH_REQUIRED")
|
|||
|
|
}
|
|||
|
|
slash := strings.ReplaceAll(p, "/", "\\")
|
|||
|
|
lower := strings.ToLower(slash)
|
|||
|
|
prefix := ""
|
|||
|
|
switch {
|
|||
|
|
case strings.HasPrefix(lower, "\\\\wsl.localhost\\"):
|
|||
|
|
prefix = "\\\\wsl.localhost\\"
|
|||
|
|
case strings.HasPrefix(lower, "\\\\wsl$\\"):
|
|||
|
|
prefix = "\\\\wsl$\\"
|
|||
|
|
default:
|
|||
|
|
return PathDescriptor{Original: raw, WindowsPath: p}, nil
|
|||
|
|
}
|
|||
|
|
rest := strings.TrimPrefix(slash, prefix)
|
|||
|
|
parts := strings.Split(rest, "\\")
|
|||
|
|
if len(parts) < 2 || parts[0] == "" {
|
|||
|
|
return PathDescriptor{}, errors.New("WSL_PATH_INVALID")
|
|||
|
|
}
|
|||
|
|
distro := parts[0]
|
|||
|
|
linux := "/" + strings.Join(parts[1:], "/")
|
|||
|
|
canonical := "\\\\wsl.localhost\\" + distro + "\\" + strings.Join(parts[1:], "\\")
|
|||
|
|
return PathDescriptor{Original: raw, WindowsPath: canonical, Distro: distro, LinuxPath: linux, WSL: true}, nil
|
|||
|
|
}
|