Files
arimu1 9c4418bfbd pkg/ioutilx: move UTF-16 helpers to pkg/strutil
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>
2026-07-19 22:01:11 +07:00

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
}