mirror of
https://github.com/lima-vm/lima.git
synced 2026-10-28 19:54:35 +08:00
Keep ReadAtMaximum in ioutilx (io-like). Move FromUTF16le / FromUTF16leToString to pkg/strutil so ioutilx only holds ioutil-style helpers. Updates the executil Windows UTF-16 command helper. Use gotest.tools/v3/assert in the new strutil UTF-16 tests instead of t.Fatal to satisfy forbidigo. Fixes #5283 Signed-off-by: arimu1 <19286898+arimu1@users.noreply.github.com>
55 lines
1.0 KiB
Go
55 lines
1.0 KiB
Go
// SPDX-FileCopyrightText: Copyright The Lima Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package executil
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"fmt"
|
|
"os/exec"
|
|
|
|
"github.com/lima-vm/lima/v2/pkg/strutil"
|
|
)
|
|
|
|
type options struct {
|
|
ctx context.Context
|
|
}
|
|
|
|
type Opt func(*options) error
|
|
|
|
// WithContext runs the command with CommandContext.
|
|
func WithContext(ctx context.Context) Opt {
|
|
return func(o *options) error {
|
|
o.ctx = ctx
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func RunUTF16leCommand(args []string, opts ...Opt) (string, error) {
|
|
var o options
|
|
for _, f := range opts {
|
|
if err := f(&o); err != nil {
|
|
return "", err
|
|
}
|
|
}
|
|
|
|
var cmd *exec.Cmd
|
|
ctx := o.ctx
|
|
if ctx == nil {
|
|
ctx = context.Background()
|
|
}
|
|
cmd = exec.CommandContext(ctx, args[0], args[1:]...)
|
|
|
|
outString := ""
|
|
out, err := cmd.CombinedOutput()
|
|
if out != nil {
|
|
s, err := strutil.FromUTF16leToString(bytes.NewReader(out))
|
|
if err != nil {
|
|
return "", fmt.Errorf("failed to convert output from UTF16 when running command %v, err: %w", args, err)
|
|
}
|
|
outString = s
|
|
}
|
|
return outString, err
|
|
}
|