| 1 | |
| 2 | |
| 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 | |
| 19 | func Path() (string, error) { |
| 20 | limactl := cmp.Or(os.Getenv("LIMACTL"), "limactl") |
| 21 | return exec.LookPath(limactl) |
| 22 | } |
| 23 | |
| 24 | |
| 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 | } |