-
Notifications
You must be signed in to change notification settings - Fork 653
/
Copy pathmain.go
113 lines (100 loc) · 2.45 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
package main
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"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 {
examplesDir := "$PREFIX/share/doc/lima/examples"
if exe, err := os.Executable(); err == nil {
binDir := filepath.Dir(exe)
prefixDir := filepath.Dir(binDir)
examplesDir = filepath.Join(prefixDir, "share/doc/lima/examples")
}
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 example YAMLs: %s`, examplesDir),
SilenceUsage: true,
SilenceErrors: true,
}
rootCmd.PersistentFlags().Bool("debug", false, "debug mode")
rootCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
debug, _ := cmd.Flags().GetBool("debug")
if debug {
logrus.SetLevel(logrus.DebugLevel)
}
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 {
// 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(
newStartCommand(),
newStopCommand(),
newShellCommand(),
newCopyCommand(),
newListCommand(),
newDeleteCommand(),
newValidateCommand(),
newSudoersCommand(),
newPruneCommand(),
newHostagentCommand(),
newInfoCommand(),
newShowSSHCommand(),
newDebugCommand(),
newEditCommand(),
newFactoryResetCommand(),
newDiskCommand(),
)
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
}
}