-
Notifications
You must be signed in to change notification settings - Fork 615
/
main.go
151 lines (134 loc) · 3.62 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/lima-vm/lima/pkg/osutil"
"github.com/lima-vm/lima/pkg/store/dirnames"
"github.com/lima-vm/lima/pkg/version"
"github.com/mattn/go-isatty"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
const (
DefaultInstanceName = "default"
)
func main() {
if err := newApp().Execute(); err != nil {
handleExitCoder(err)
logrus.Fatal(err)
}
}
func newApp() *cobra.Command {
templatesDir := "$PREFIX/share/lima/templates"
if exe, err := os.Executable(); err == nil {
binDir := filepath.Dir(exe)
prefixDir := filepath.Dir(binDir)
templatesDir = filepath.Join(prefixDir, "share/lima/templates")
}
var rootCmd = &cobra.Command{
Use: "limactl",
Short: "Lima: Linux virtual machines",
Version: strings.TrimPrefix(version.Version, "v"),
Example: fmt.Sprintf(` Start the default instance:
$ limactl start
Open a shell:
$ lima
Run a container:
$ lima nerdctl run -d --name nginx -p 8080:80 nginx:alpine
Stop the default instance:
$ limactl stop
See also template YAMLs: %s`, templatesDir),
SilenceUsage: true,
SilenceErrors: true,
DisableAutoGenTag: true,
}
rootCmd.PersistentFlags().String("log-level", "", "Set the logging level [trace, debug, info, warn, error]")
rootCmd.PersistentFlags().Bool("debug", false, "debug mode")
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
l, _ := cmd.Flags().GetString("log-level")
if l != "" {
lvl, err := logrus.ParseLevel(l)
if err != nil {
return err
}
logrus.SetLevel(lvl)
}
debug, _ := cmd.Flags().GetBool("debug")
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
if osutil.IsBeingRosettaTranslated() {
// running under rosetta would provide inappropriate runtime.GOARCH info, see: https://github.com/lima-vm/lima/issues/543
return errors.New("limactl is running under rosetta, please reinstall lima with native arch")
}
if runtime.GOOS == "windows" && isatty.IsCygwinTerminal(os.Stdout.Fd()) {
formatter := new(logrus.TextFormatter)
// the default setting does not recognize cygwin on windows
formatter.ForceColors = true
logrus.StandardLogger().SetFormatter(formatter)
}
if os.Geteuid() == 0 && cmd.Name() != "generate-man" {
return errors.New("must not run as the root")
}
// Make sure either $HOME or $LIMA_HOME is defined, so we don't need
// to check for errors later
if _, err := dirnames.LimaDir(); err != nil {
return err
}
return nil
}
rootCmd.AddCommand(
newCreateCommand(),
newStartCommand(),
newStopCommand(),
newShellCommand(),
newCopyCommand(),
newListCommand(),
newDeleteCommand(),
newValidateCommand(),
newSudoersCommand(),
newPruneCommand(),
newHostagentCommand(),
newInfoCommand(),
newShowSSHCommand(),
newDebugCommand(),
newEditCommand(),
newFactoryResetCommand(),
newDiskCommand(),
newUsernetCommand(),
newGenManCommand(),
newSnapshotCommand(),
)
return rootCmd
}
type ExitCoder interface {
error
ExitCode() int
}
func handleExitCoder(err error) {
if err == nil {
return
}
if exitErr, ok := err.(ExitCoder); ok {
os.Exit(exitErr.ExitCode())
return
}
}
// WrapArgsError annotates cobra args error with some context, so the error message is more user-friendly
func WrapArgsError(argFn cobra.PositionalArgs) cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
err := argFn(cmd, args)
if err == nil {
return nil
}
return fmt.Errorf("%q %s.\nSee '%s --help'.\n\nUsage: %s\n\n%s",
cmd.CommandPath(), err.Error(),
cmd.CommandPath(),
cmd.UseLine(), cmd.Short,
)
}
}