diff --git a/src/install/install.go b/src/install/install.go index b911c99..edf3488 100644 --- a/src/install/install.go +++ b/src/install/install.go @@ -4,8 +4,11 @@ import ( "bufio" "fmt" "os" + "os/exec" "path/filepath" + "strconv" + "github.com/yuk7/wsldl/lib/preset" "github.com/yuk7/wsldl/lib/utils" "github.com/yuk7/wsldl/lib/wslapi" ) @@ -37,6 +40,18 @@ func Execute(name string, args []string) { } err := Install(name, rootPath, showProgress) + if err != nil { + utils.ErrorExit(err, showProgress, true, args == nil) + } + + json, err2 := preset.ReadParsePreset() + if err2 == nil { + if json.WslVersion == 1 || json.WslVersion == 2 { + wslexe := os.Getenv("SystemRoot") + "\\System32\\wsl.exe" + _, err = exec.Command(wslexe, "--set-version", name, strconv.Itoa(json.WslVersion)).Output() + } + } + if err == nil { if showProgress { utils.StdoutGreenPrintln("Installation complete") diff --git a/src/lib/preset/model_preset.go b/src/lib/preset/model_preset.go new file mode 100644 index 0000000..2117dd2 --- /dev/null +++ b/src/lib/preset/model_preset.go @@ -0,0 +1,5 @@ +package preset + +type Preset struct { + WslVersion int `json:"wslversion,omitempty"` +} diff --git a/src/lib/preset/preset.go b/src/lib/preset/preset.go new file mode 100644 index 0000000..f9431d4 --- /dev/null +++ b/src/lib/preset/preset.go @@ -0,0 +1,43 @@ +package preset + +import ( + "io/ioutil" + "os" + "path/filepath" + + "muzzammil.xyz/jsonc" +) + +// ReadPresetJSON reads preset.json configuration json file +func ReadPresetJSON() (res string, err error) { + efPath, _ := os.Executable() + dir := filepath.Dir(efPath) + json := filepath.Join(dir, "preset.json") + b, err := ioutil.ReadFile(json) + if err != nil { + return + } + res = string(b) + return +} + +// ParsePresetJSON parses preset.json configuration json string +func ParsePresetJSON(str string) (res Preset, err error) { + var c Preset + err = jsonc.Unmarshal([]byte(str), &c) + if err != nil { + return + } + res = c + return +} + +// ReadParsePreset reads and parses preset.json configuration file +func ReadParsePreset() (conf Preset, err error) { + json, err := ReadPresetJSON() + if err != nil { + return + } + conf, err = ParsePresetJSON(json) + return +}