我正在研究https://github.com/cdr/sshcode的分支,更具体地说,我正在研究PR以向程序添加git4win / msys2支持
当前的问题源于这两个功能
func gitbashWindowsDir(dir string) string {
if dir == "~" { //Special case
return "~/"
}
mountPoints := gitbashMountPointsAndHome()
// Apply mount points
absDir, _ := filepath.Abs(dir)
absDir = filepath.ToSlash(absDir)
for _, mp := range mountPoints {
if strings.HasPrefix(absDir, mp[0]) {
resolved := strings.Replace(absDir, mp[0], mp[1], 1)
flog.Info("Resolved windows path '%s' to '%s", dir, resolved)
return resolved
}
}
return dir
}
// This function returns an array with MINGW64 mount points including relative home dir
func gitbashMountPointsAndHome() [][]string {
// Initialize mount points with home dir
mountPoints := [][]string{{filepath.ToSlash(os.Getenv("HOME")), "~"}}
// Load mount points
out, err := exec.Command("mount").Output()
if err != nil {
log.Fatal(err)
}
lines := strings.Split(string(out), "\n")
var mountRx = regexp.MustCompile(`^(.*) on (.*) type`)
for _, line := range lines {
extract := mountRx.FindStringSubmatch(line)
if len(extract) > 0 {
mountPoints = append(mountPoints, []string{extract[1], extract[2]})
}
res = strings.TrimPrefix(dir, line)
}
// Sort by size to get more restrictive mount points first
sort.Slice(mountPoints, func(i, j int) bool {
return len(mountPoints[i][0]) > len(mountPoints[j][0])
})
return mountPoints
}
如何使用它,在msys2 / git4win上,您给它
gitbashWindowsDir("/Workspace")
,它应该返回/Workspace
,因为它处理msys2 / git4win处理路径的非正交方式。当你给它
gitbashWindowsDir("Workspace/")
时,它返回与echo $PWD/Workspace/
基本相同的输出,但是以窗口格式我正在开发利用
strings.Prefix
内容的第一步补丁,这就是我这个贴沙发的
package main
import (
"fmt"
"os"
)
func main() {
mydir, err := os.Getwd()
if err != nil {
fmt.Println(err)
}
fmt.Println(os.Getenv("PWD"))
fmt.Println(mydir)
}
我想检查输入是否具有前缀
/
,如果有,只需将其作为字符串返回,这似乎是gitbashWindowsDir("/Workspace")
返回//Workspace
的简单解决方案但是我认为最难的部分将是
gitbashWindowsDir("Workspace/")
,因为它以Windows格式(echo $PWD/Workspace/
)返回与Z:\Workspace\
相同的输出。______________________________________________
______________________________________________
更新,我已经获得了修剪前缀的功能,(非常简单)
但是现在我遇到了这个问题
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"sort"
"strings"
"go.coder.com/flog"
)
func main() {
fmt.Println("RESOLVED: ", gitbashWindowsDir(os.Args[1]))
fmt.Println("RESOLVED: ", gitbashWindowsDir("C:\\msys64\\Workspace"))
}
func gitbashWindowsDir(dir string) string {
// if dir is left empty, line82:main.go will set it to `~`, this makes it so that
// if dir is `~`, return `~/` instead of continuing with the gitbashWindowsDir()
// function.
if dir == "~" {
return "~/"
}
mountPoints := gitbashMountPointsAndHome()
// Apply mount points
absDir, _ := filepath.Abs(dir)
absDir = filepath.ToSlash(absDir)
for _, mp := range mountPoints {
if strings.HasPrefix(absDir, mp[0]) {
resolved := strings.Replace(absDir, mp[0], mp[1], 1)
if strings.HasPrefix(resolved, "//") {
resolved = strings.TrimPrefix(resolved, "/")
flog.Info("DEBUG: strings.TrimPrefix")
flog.Info("Resolved windows path '%s' to '%s", dir, resolved)
flog.Info("'%s'", resolved)
return resolved
}
flog.Info("Resolved windows path '%s' to '%s", dir, resolved)
return resolved
}
}
return dir
}
// This function returns an array with MINGW64 mount points including relative home dir
func gitbashMountPointsAndHome() [][]string {
mountPoints := [][]string{{filepath.ToSlash(os.Getenv("HOME")), "~"}}
// Load mount points
out, err := exec.Command("mount").Output()
if err != nil {
//log.Error(err)
log.Println(err)
}
lines := strings.Split(string(out), "\n")
var mountRx = regexp.MustCompile(`^(.*) on (.*) type`)
for _, line := range lines {
extract := mountRx.FindStringSubmatch(line)
if len(extract) > 0 {
mountPoints = append(mountPoints, []string{extract[1], extract[2]})
}
}
// Sort by size to get more restrictive mount points first
sort.Slice(mountPoints, func(i, j int) bool {
return len(mountPoints[i][0]) > len(mountPoints[j][0])
})
return mountPoints
}
运行此命令时,它将返回
merith@DESKTOP-BQUQ80R MINGW64 /z/sshcode
$ go run ../debug.go Workspace
2019-11-27 10:49:38 INFO Resolved windows path 'Workspace' to '/z/sshcode/Workspace
RESOLVED: /z/sshcode/Workspace
2019-11-27 10:49:38 INFO DEBUG: strings.TrimPrefix
2019-11-27 10:49:38 INFO Resolved windows path 'C:\msys64\Workspace' to '/Workspace
2019-11-27 10:49:38 INFO '/Workspace'
RESOLVED: /Workspace
现在,我需要弄清楚如何检测和删除
/*/
前缀,以便/z/sshcode/Workspace
成为Workspace/
最佳答案
对于确定字符串是否以正斜杠作为前缀的第一个问题,可以使用如下函数:
func ensureSlashPrefix(path string) string {
return fmt.Sprintf("/%s", strings.Replace(path, "/", "", -1))
}
对于您的第二个问题,也许我误会了,但是此函数会将绝对路径返回到提供的相对路径,或者将类似的输出返回到
echo $PWD/path
。func cleanWindowsPath(path string) string {
base := filepath.Base(strings.Replace(path, "\\", "/", -1))
return ensureSlashPrefix(base)
}
(Go Playground)
关于windows - 解决和清理输出问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59011037/