-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlxrunoffline.go
119 lines (98 loc) · 3.22 KB
/
lxrunoffline.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package lxrunoffline
import "errors"
const (
powershell = "powershell.exe"
registry_path = "Software\\Microsoft\\Windows\\CurrentVersion\\Lxss\\"
lxRunOffline_libs_main = "LxRunOffline.exe"
lxRunOffline_libs_path = "libs\\LxRunOffline.exe"
)
var (
args_powershell = []string{"-c", "chcp", "65001", ">", "$null", ";"}
args_list_installed = []string{"list"}
args_summary = []string{"sm", "-n"}
args_get_default = []string{"gd"}
args_install = func(nameOfDistro string, dirToInstall string, tarFile string) []string {
return []string{"i", "-n", nameOfDistro, "-d", dirToInstall, "-f", tarFile}
}
args_export = func(nameOfDistro string, tarDirFile string) []string {
return []string{"e", "-n", nameOfDistro, "-f", tarDirFile}
}
args_duplicate = func(nameOfCurrentDistro string, dirToDuplicate string, newDistroName string) []string {
return []string{"d", "-n", nameOfCurrentDistro, "-d", dirToDuplicate, "-N", newDistroName}
}
registry_default_distro = "DefaultDistribution"
registry_distro_name = "DistributionName"
registry_dir = "BasePath"
registry_state = "State"
registry_version = "Version"
registry_env = "DefaultEnvironment"
registry_uid = "DefaultUid"
registry_kernel_cmd = "KernelCommandLine"
registry_flags = "Flags"
wsl2_flags = 8
)
type LxRunOffline struct {
Options
}
type Options struct {
LibsPath string
}
// Init(options) can be used to obtain custom location of lxrunoffline.exe
func Init(options Options) *LxRunOffline {
if options.LibsPath == "" {
options.LibsPath = lxRunOffline_libs_path
}
lx := &LxRunOffline{
Options: options,
}
return lx
}
/* New()
Initialize and find where lxrunoffline.exe is installed to the machine.
Use this initialize method if you have installed lxrunoffline via Chocolatey or Scoop.
Also if you correctly install lxrunoffline.exe manually then added to Windows PATH
*/
func New() (*LxRunOffline, error) {
lxLocation, err := WhereLx()
if err != nil {
return nil, errors.New("no lxrunoffline installed")
}
lx := &LxRunOffline{
Options{
LibsPath: lxLocation,
},
}
return lx, nil
}
/* lx.ListInstalled()
Return list of distro with some infos. This function read Windows Registrys
*/
func (lx *LxRunOffline) ListInstalled() ([]*Distro, error) {
var distros []*Distro
distro_uids, err := lx.GetRegistrySubkey(registry_path, "")
if err != nil {
return []*Distro{}, err
}
for i := range distro_uids {
d, err := lx.GetDistroSummary(distro_uids[i])
if err != nil {
return []*Distro{}, err
}
distros = append(distros, d)
}
return distros, nil
}
/* lx.GetDefaultDistro()
Return distro name, distro_uid. Empty string will return if error occured. This function read Windows Registry
*/
func (lx *LxRunOffline) GetDefaultDistro() (string, string, error) {
distro_uid, _, err := lx.GetRegistryValue("", registry_default_distro)
if err != nil {
return "", "", err
}
distro_name, _, err := lx.GetRegistryValue(addPathPrefix(distro_uid), registry_distro_name)
if err != nil {
return "", "", err
}
return distro_name, distro_uid, nil
}