Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/pkg/executil/command.go
2611 views
1
// SPDX-FileCopyrightText: Copyright The Lima Authors
2
// SPDX-License-Identifier: Apache-2.0
3
4
package executil
5
6
import (
7
"bytes"
8
"context"
9
"fmt"
10
"os/exec"
11
12
"github.com/lima-vm/lima/v2/pkg/ioutilx"
13
)
14
15
type options struct {
16
ctx context.Context
17
}
18
19
type Opt func(*options) error
20
21
// WithContext runs the command with CommandContext.
22
func WithContext(ctx context.Context) Opt {
23
return func(o *options) error {
24
o.ctx = ctx
25
return nil
26
}
27
}
28
29
func RunUTF16leCommand(args []string, opts ...Opt) (string, error) {
30
var o options
31
for _, f := range opts {
32
if err := f(&o); err != nil {
33
return "", err
34
}
35
}
36
37
var cmd *exec.Cmd
38
ctx := o.ctx
39
if ctx == nil {
40
ctx = context.Background()
41
}
42
cmd = exec.CommandContext(ctx, args[0], args[1:]...)
43
44
outString := ""
45
out, err := cmd.CombinedOutput()
46
if out != nil {
47
s, err := ioutilx.FromUTF16leToString(bytes.NewReader(out))
48
if err != nil {
49
return "", fmt.Errorf("failed to convert output from UTF16 when running command %v, err: %w", args, err)
50
}
51
outString = s
52
}
53
return outString, err
54
}
55
56