初始化
This commit is contained in:
35
platform/wsl.go
Normal file
35
platform/wsl.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package platform
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os/exec"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ListWSLDistros 返回本机已安装的 WSL 发行版;未安装 WSL 时返回稳定错误码。
|
||||
func ListWSLDistros(ctx context.Context) ([]string, error) {
|
||||
if _, e := exec.LookPath("wsl.exe"); e != nil {
|
||||
return nil, &PlatformError{Code: "WSL_NOT_INSTALLED", Detail: e.Error()}
|
||||
}
|
||||
out, e := RunHidden(ctx, "wsl.exe", "--list", "--quiet")
|
||||
if e != nil {
|
||||
return nil, &PlatformError{Code: "WSL_LIST_FAILED", Detail: e.Error()}
|
||||
}
|
||||
items := []string{}
|
||||
for _, v := range strings.FieldsFunc(out, func(r rune) bool { return r == '\r' || r == '\n' || r == 0 }) {
|
||||
v = strings.TrimSpace(v)
|
||||
if v != "" {
|
||||
items = append(items, v)
|
||||
}
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
type PlatformError struct{ Code, Detail string }
|
||||
|
||||
func (e *PlatformError) Error() string {
|
||||
if e.Detail == "" {
|
||||
return e.Code
|
||||
}
|
||||
return e.Code + ": " + e.Detail
|
||||
}
|
||||
Reference in New Issue
Block a user