-
Notifications
You must be signed in to change notification settings - Fork 0
/
wsl-helpers.go
64 lines (54 loc) · 1.52 KB
/
wsl-helpers.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
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"regexp"
"github.com/jxeng/shortcut"
"github.com/yuk7/wsllib-go"
)
func importWsl(distroName string) (err error) {
_ = wsllib.WslUnregisterDistribution(distroName)
path, err := filepath.Abs("image.tar")
if err != nil {
return err
}
var re = regexp.MustCompile(`~[\p{L}0-9\s]+`)
escapedPath := re.ReplaceAllString(distroName, `-`)
cmd := exec.Command("wsl", "--import", distroName, fmt.Sprintf("./%s", escapedPath), "image.tar", "--version", "2")
_, err = cmd.Output()
if err != nil {
return wsllib.WslRegisterDistribution(distroName, path)
}
return nil
}
func setDefaultWsl(distroName string) (err error) {
cmd := exec.Command("wsl", "--set-default", distroName)
_, err = cmd.Output()
return err
}
func addToStartMenu(distroName string) (err error) {
appData, exists := os.LookupEnv("APPDATA")
if !exists {
return fmt.Errorf("APPDATA environment variable not found")
}
programsPath := filepath.Join(appData,
"Microsoft", "Windows", "Start Menu", "Programs",
fmt.Sprintf("%s.lnk", distroName))
sc := shortcut.Shortcut{
ShortcutPath: programsPath,
Target: "wsl.exe",
Arguments: fmt.Sprintf("~ -d %s", distroName),
IconLocation: "%SystemRoot%\\System32\\SHELL32.dll,0",
Description: "",
Hotkey: "",
WindowStyle: "1",
WorkingDirectory: "",
}
return shortcut.Create(sc)
}
func launchWsl(distroName string) (err error) {
_, err = wsllib.WslLaunchInteractive(distroName, "", true)
return err
}