Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/pkg/limactlutil/limactlutil.go
2614 views
1
// SPDX-FileCopyrightText: Copyright The Lima Authors
2
// SPDX-License-Identifier: Apache-2.0
3
4
package limactlutil
5
6
import (
7
"bytes"
8
"cmp"
9
"context"
10
"encoding/json"
11
"fmt"
12
"os"
13
"os/exec"
14
15
"github.com/lima-vm/lima/v2/pkg/limatype"
16
)
17
18
// Path returns the path to the `limactl` executable.
19
func Path() (string, error) {
20
limactl := cmp.Or(os.Getenv("LIMACTL"), "limactl")
21
return exec.LookPath(limactl)
22
}
23
24
// Inspect runs `limactl list --json INST` and parses the output.
25
func Inspect(ctx context.Context, limactl, instName string) (*limatype.Instance, error) {
26
var stdout, stderr bytes.Buffer
27
cmd := exec.CommandContext(ctx, limactl, "list", "--json", instName)
28
cmd.Stdout = &stdout
29
cmd.Stderr = &stderr
30
if err := cmd.Run(); err != nil {
31
return nil, fmt.Errorf("failed to run %v: stdout=%q, stderr=%q: %w", cmd.Args, stdout.String(), stderr.String(), err)
32
}
33
var inst limatype.Instance
34
if err := json.Unmarshal(stdout.Bytes(), &inst); err != nil {
35
return nil, err
36
}
37
return &inst, nil
38
}
39
40