36 lines
895 B
Go
36 lines
895 B
Go
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
|
|
}
|