Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
lima-vm
GitHub Repository: lima-vm/lima
Path: blob/master/cmd/limactl/info.go
2613 views
1
// SPDX-FileCopyrightText: Copyright The Lima Authors
2
// SPDX-License-Identifier: Apache-2.0
3
4
package main
5
6
import (
7
"encoding/json"
8
"fmt"
9
10
"github.com/mikefarah/yq/v4/pkg/yqlib"
11
"github.com/spf13/cobra"
12
13
"github.com/lima-vm/lima/v2/pkg/limainfo"
14
"github.com/lima-vm/lima/v2/pkg/uiutil"
15
"github.com/lima-vm/lima/v2/pkg/yqutil"
16
)
17
18
func newInfoCommand() *cobra.Command {
19
infoCommand := &cobra.Command{
20
Use: "info",
21
Short: "Show diagnostic information",
22
Args: WrapArgsError(cobra.NoArgs),
23
RunE: infoAction,
24
GroupID: advancedCommand,
25
}
26
infoCommand.Flags().String("yq", ".", "Apply yq expression to output")
27
28
return infoCommand
29
}
30
31
func infoAction(cmd *cobra.Command, _ []string) error {
32
ctx := cmd.Context()
33
34
yq, err := cmd.Flags().GetString("yq")
35
if err != nil {
36
return err
37
}
38
39
info, err := limainfo.New(ctx)
40
if err != nil {
41
return err
42
}
43
j, err := json.MarshalIndent(info, "", " ")
44
if err != nil {
45
return err
46
}
47
48
encoderPrefs := yqlib.ConfiguredJSONPreferences.Copy()
49
encoderPrefs.Indent = 4
50
encoderPrefs.ColorsEnabled = uiutil.OutputIsTTY(cmd.OutOrStdout())
51
encoder := yqlib.NewJSONEncoder(encoderPrefs)
52
str, err := yqutil.EvaluateExpressionWithEncoder(yq, string(j), encoder)
53
if err == nil {
54
_, err = fmt.Fprint(cmd.OutOrStdout(), str)
55
}
56
return err
57
}
58
59