This repository has been archived by the owner on Jul 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
89 lines (78 loc) · 2.67 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"regexp"
"strings"
"syscall"
)
func main() {
baseName := filepath.Base(os.Args[0])
if !strings.HasPrefix(baseName, "wsl") {
log.Fatalf("Basename `%s` does not have the prefix. Rename this binary to command name with prefix `wsl`. For example, rename to `wslgit` to run git command on WSL.", baseName)
}
commandName := strings.TrimSuffix(strings.TrimPrefix(baseName, "wsl"), ".exe")
commandArgs := translateWindowsPathInArgs(os.Args[1:])
commandLine := append([]string{commandName}, commandArgs...)
cmd := exec.Command("wsl", commandLine...)
cmd.Stderr = os.Stderr
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Fatalf("Could not create a pipe to standard output: %s", err)
}
if err := cmd.Start(); err != nil {
log.Fatalf("Could not start command: %s", err)
}
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
fmt.Println(translateWslPath(scanner.Text()))
}
if err := cmd.Wait(); err != nil {
status := 1
if exitError, ok := err.(*exec.ExitError); ok {
if waitStatus, ok := exitError.Sys().(syscall.WaitStatus); ok {
status = waitStatus.ExitStatus()
}
}
os.Exit(status)
}
}
var wslDrivePathPattern = regexp.MustCompile("/mnt/([[:alpha:]])/")
func translateWslPath(line string) string {
return wslDrivePathPattern.ReplaceAllString(line, "$1:/")
}
func translateWindowsPathInArgs(windowsPathArgs []string) []string {
unixPathArgs := make([]string, len(windowsPathArgs))
for i, windowsPathArg := range windowsPathArgs {
unixPathArgs[i] = translateWindowsPathInArg(windowsPathArg)
}
return unixPathArgs
}
var windowsDrivePathPattern = regexp.MustCompile("([[:alpha:]]):\\\\")
var windowsDrivePathPatternPHPStorm = regexp.MustCompile("([[:alpha:]]):/")
func translateWindowsPathInArg(arg string) string {
// Fix for PHPStorm Paths in Cross-Environemnt
if windowsDrivePathPatternPHPStorm.FindStringIndex(arg) != nil {
driveReplaced := windowsDrivePathPatternPHPStorm.ReplaceAllStringFunc(arg, func(drivePath string) string {
m := windowsDrivePathPatternPHPStorm.FindStringSubmatch(drivePath)
drive := strings.ToLower(m[1])
return fmt.Sprintf("/mnt/%s/", drive)
})
backslashReplaced := strings.Replace(driveReplaced, "\\", "/", -1)
return backslashReplaced
}
if windowsDrivePathPattern.FindStringIndex(arg) == nil {
return arg
}
driveReplaced := windowsDrivePathPattern.ReplaceAllStringFunc(arg, func(drivePath string) string {
m := windowsDrivePathPattern.FindStringSubmatch(drivePath)
drive := strings.ToLower(m[1])
return fmt.Sprintf("/mnt/%s/", drive)
})
backslashReplaced := strings.Replace(driveReplaced, "\\", "/", -1)
return backslashReplaced
}