Skip to content

Commit

Permalink
src: add preset for select wsl version on install
Browse files Browse the repository at this point in the history
  • Loading branch information
yuk7 committed Jul 24, 2021
1 parent 5443249 commit 6d91a00
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/install/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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")
Expand Down
5 changes: 5 additions & 0 deletions src/lib/preset/model_preset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package preset

type Preset struct {
WslVersion int `json:"wslversion,omitempty"`
}
43 changes: 43 additions & 0 deletions src/lib/preset/preset.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 6d91a00

Please sign in to comment.