-
Notifications
You must be signed in to change notification settings - Fork 336
/
Copy pathwsl-win.go
136 lines (120 loc) · 2.76 KB
/
wsl-win.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//go:build windows
// Copyright 2025, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0
package wsl
import (
"context"
"fmt"
"io"
"os"
"sync"
"github.com/ubuntu/gowsl"
)
var RegisteredDistros = gowsl.RegisteredDistros
var DefaultDistro = gowsl.DefaultDistro
type WslName struct {
Distro string `json:"distro"`
}
type Distro struct {
gowsl.Distro
}
type WslCmd struct {
c *gowsl.Cmd
wg *sync.WaitGroup
once *sync.Once
lock *sync.Mutex
waitErr error
}
func (d *Distro) WslCommand(ctx context.Context, cmd string) *WslCmd {
if ctx == nil {
panic("nil Context")
}
innerCmd := d.Command(ctx, cmd)
var wg sync.WaitGroup
var lock *sync.Mutex
return &WslCmd{innerCmd, &wg, new(sync.Once), lock, nil}
}
func (c *WslCmd) CombinedOutput() (out []byte, err error) {
return c.c.CombinedOutput()
}
func (c *WslCmd) Output() (out []byte, err error) {
return c.c.Output()
}
func (c *WslCmd) Run() error {
return c.c.Run()
}
func (c *WslCmd) Start() (err error) {
return c.c.Start()
}
func (c *WslCmd) StderrPipe() (r io.ReadCloser, err error) {
return c.c.StderrPipe()
}
func (c *WslCmd) StdinPipe() (w io.WriteCloser, err error) {
return c.c.StdinPipe()
}
func (c *WslCmd) StdoutPipe() (r io.ReadCloser, err error) {
return c.c.StdoutPipe()
}
func (c *WslCmd) Wait() (err error) {
c.wg.Add(1)
c.once.Do(func() {
c.waitErr = c.c.Wait()
})
c.wg.Done()
c.wg.Wait()
if c.waitErr != nil && c.waitErr.Error() == "not started" {
c.once = new(sync.Once)
return c.waitErr
}
return c.waitErr
}
func (c *WslCmd) ExitCode() int {
state := c.c.ProcessState
if state == nil {
return -1
}
return state.ExitCode()
}
func (c *WslCmd) GetProcess() *os.Process {
return c.c.Process
}
func (c *WslCmd) GetProcessState() *os.ProcessState {
return c.c.ProcessState
}
func (c *WslCmd) SetStdin(stdin io.Reader) {
c.c.Stdin = stdin
}
func (c *WslCmd) SetStdout(stdout io.Writer) {
c.c.Stdout = stdout
}
func (c *WslCmd) SetStderr(stderr io.Writer) {
c.c.Stdout = stderr
}
func GetDistroCmd(ctx context.Context, wslDistroName string, cmd string) (*WslCmd, error) {
distros, err := RegisteredDistros(ctx)
if err != nil {
return nil, err
}
for _, distro := range distros {
if distro.Name() != wslDistroName {
continue
}
wrappedDistro := Distro{distro}
return wrappedDistro.WslCommand(ctx, cmd), nil
}
return nil, fmt.Errorf("wsl distro %s not found", wslDistroName)
}
func GetDistro(ctx context.Context, wslDistroName WslName) (*Distro, error) {
distros, err := RegisteredDistros(ctx)
if err != nil {
return nil, err
}
for _, distro := range distros {
if distro.Name() != wslDistroName.Distro {
continue
}
wrappedDistro := Distro{distro}
return &wrappedDistro, nil
}
return nil, fmt.Errorf("wsl distro %s not found", wslDistroName)
}