-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrapper.go
49 lines (41 loc) · 1.07 KB
/
wrapper.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
package main
import (
"fmt"
"log"
"path/filepath"
"regexp"
"strings"
)
var (
wslPath = regexp.MustCompile(`^(\\\\wsl\$\\[\w-.]+)(.*)$`)
winPath = regexp.MustCompile(`^([A-Z]):\\(.*)$`)
)
type Runnable interface {
Run() error
}
type Command func(name string, args ...string) Runnable
type CommandWrapper struct {
command Command
}
func NewCommandWrapper(command Command) *CommandWrapper {
return &CommandWrapper{command}
}
func (c *CommandWrapper) Run(name string, args ...string) error {
args = append([]string{name}, args...)
log.Printf("Original args: %+v", args)
for i, arg := range args {
if wslPath.MatchString(arg) {
arg = wslPath.ReplaceAllString(arg, "$2")
args[i] = filepath.ToSlash(arg)
} else if winPath.MatchString(arg) {
matches := winPath.FindStringSubmatch(arg)
arg = fmt.Sprintf("/mnt/%s/%s", strings.ToLower(matches[1]), matches[2])
args[i] = filepath.ToSlash(arg)
} else if filepath.IsLocal(arg) {
args[i] = filepath.ToSlash(arg)
}
}
log.Printf("Modified args: %+v", args)
cmd := c.command("wsl", args...)
return cmd.Run()
}