-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (50 loc) · 1.23 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
package main
import (
"errors"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/arixmkii/go-wsllinks/pkg/direct"
"github.com/arixmkii/go-wsllinks/pkg/wsl"
"github.com/gookit/ini/v2"
)
func main() {
exe, err := os.Executable()
if err != nil {
os.Exit(-1)
}
exeDir := filepath.Dir(exe)
targetCommand := strings.TrimSuffix(filepath.Base(exe), filepath.Ext(exe))
cfg := ini.NewWithOptions(func(opts *ini.Options) {
opts.Readonly = true
opts.ParseEnv = false
opts.ParseVar = false
})
err = cfg.LoadExists(filepath.Join(exeDir, "wsllinks.ini"), filepath.Join(exeDir, targetCommand+".ini"))
if err != nil {
os.Exit(-1)
}
var runnerBinary string
var runnerArgs []string
switch strings.TrimSpace(cfg.String("mode", "")) {
case "wsl", "":
runnerBinary, runnerArgs, err = wsl.ResovleCommand(targetCommand, cfg, os.Args[1:])
case "direct":
runnerBinary, runnerArgs, err = direct.ResovleCommand(exe, targetCommand, cfg, os.Args[1:])
default:
err = errors.New("Unsupported mode")
}
if err != nil {
os.Exit(-1)
}
cmd := exec.Command(runnerBinary, runnerArgs...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
e := cmd.Run()
if e != nil {
os.Exit(-1)
}
os.Exit(cmd.ProcessState.ExitCode())
}