| 1 | |
| 2 | |
| 3 | |
| 4 | package main |
| 5 | |
| 6 | import ( |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "runtime" |
| 12 | "strings" |
| 13 | |
| 14 | "github.com/mattn/go-isatty" |
| 15 | "github.com/sirupsen/logrus" |
| 16 | "github.com/spf13/cobra" |
| 17 | |
| 18 | "github.com/lima-vm/lima/v2/cmd/yq" |
| 19 | "github.com/lima-vm/lima/v2/pkg/debugutil" |
| 20 | "github.com/lima-vm/lima/v2/pkg/driver/external/server" |
| 21 | "github.com/lima-vm/lima/v2/pkg/envutil" |
| 22 | "github.com/lima-vm/lima/v2/pkg/fsutil" |
| 23 | "github.com/lima-vm/lima/v2/pkg/limatype/dirnames" |
| 24 | "github.com/lima-vm/lima/v2/pkg/osutil" |
| 25 | "github.com/lima-vm/lima/v2/pkg/plugins" |
| 26 | "github.com/lima-vm/lima/v2/pkg/version" |
| 27 | ) |
| 28 | |
| 29 | const ( |
| 30 | DefaultInstanceName = "default" |
| 31 | basicCommand = "basic" |
| 32 | advancedCommand = "advanced" |
| 33 | ) |
| 34 | |
| 35 | func main() { |
| 36 | if os.Geteuid() == 0 && (len(os.Args) < 2 || os.Args[1] != "generate-doc") { |
| 37 | fmt.Fprint(os.Stderr, "limactl: must not run as the root user\n") |
| 38 | os.Exit(1) |
| 39 | } |
| 40 | |
| 41 | yq.MaybeRunYQ() |
| 42 | if runtime.GOOS == "windows" { |
| 43 | extras, hasExtra := os.LookupEnv("_LIMA_WINDOWS_EXTRA_PATH") |
| 44 | if hasExtra && strings.TrimSpace(extras) != "" { |
| 45 | p := os.Getenv("PATH") |
| 46 | err := os.Setenv("PATH", strings.TrimSpace(extras)+string(filepath.ListSeparator)+p) |
| 47 | if err != nil { |
| 48 | logrus.Warning("Can't add extras to PATH, relying entirely on system PATH") |
| 49 | } |
| 50 | } |
| 51 | p := os.Getenv("PATH") |
| 52 | newPath := envutil.AppendDirsToPath(p, envutil.PlatformCommonToolDirs) |
| 53 | if newPath != p { |
| 54 | logrus.Info("Temporarily add the common tool (QEMU etc.) directories to PATH") |
| 55 | if err := os.Setenv("PATH", newPath); err != nil { |
| 56 | logrus.WithError(err).Warning("Can't add common tool directories to PATH, relying entirely on system PATH") |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | err := newApp().Execute() |
| 61 | server.Disconnect() |
| 62 | osutil.HandleExitError(err) |
| 63 | if err != nil { |
| 64 | logrus.Fatal(err) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func processGlobalFlags(rootCmd *cobra.Command) error { |
| 69 | |
| 70 | if debug, _ := rootCmd.Flags().GetBool("debug"); debug { |
| 71 | logrus.SetLevel(logrus.DebugLevel) |
| 72 | debugutil.Debug = true |
| 73 | } |
| 74 | |
| 75 | l, _ := rootCmd.Flags().GetString("log-level") |
| 76 | if l != "" { |
| 77 | lvl, err := logrus.ParseLevel(l) |
| 78 | if err != nil { |
| 79 | return err |
| 80 | } |
| 81 | logrus.SetLevel(lvl) |
| 82 | } |
| 83 | |
| 84 | logFormat, _ := rootCmd.Flags().GetString("log-format") |
| 85 | switch logFormat { |
| 86 | case "json": |
| 87 | formatter := new(logrus.JSONFormatter) |
| 88 | logrus.StandardLogger().SetFormatter(formatter) |
| 89 | case "text": |
| 90 | |
| 91 | if runtime.GOOS == "windows" && isatty.IsCygwinTerminal(os.Stderr.Fd()) { |
| 92 | formatter := new(logrus.TextFormatter) |
| 93 | |
| 94 | formatter.ForceColors = true |
| 95 | logrus.StandardLogger().SetFormatter(formatter) |
| 96 | } |
| 97 | default: |
| 98 | return fmt.Errorf("unsupported log-format: %#q", logFormat) |
| 99 | } |
| 100 | return nil |
| 101 | } |
| 102 | |
| 103 | func newApp() *cobra.Command { |
| 104 | templatesDir := "$PREFIX/share/lima/templates" |
| 105 | if exe, err := os.Executable(); err == nil { |
| 106 | binDir := filepath.Dir(exe) |
| 107 | prefixDir := filepath.Dir(binDir) |
| 108 | templatesDir = filepath.Join(prefixDir, "share/lima/templates") |
| 109 | } |
| 110 | |
| 111 | rootCmd := &cobra.Command{ |
| 112 | Use: "limactl", |
| 113 | Short: "Lima: Linux virtual machines", |
| 114 | Version: strings.TrimPrefix(version.Version, "v"), |
| 115 | Example: fmt.Sprintf(` Start the default instance: |
| 116 | $ limactl start |
| 117 | |
| 118 | Open a shell: |
| 119 | $ lima |
| 120 | |
| 121 | Run a container: |
| 122 | $ lima nerdctl run -d --name nginx -p 8080:80 nginx:alpine |
| 123 | |
| 124 | Stop the default instance: |
| 125 | $ limactl stop |
| 126 | |
| 127 | See also template YAMLs: %s`, templatesDir), |
| 128 | SilenceUsage: true, |
| 129 | SilenceErrors: true, |
| 130 | DisableAutoGenTag: true, |
| 131 | } |
| 132 | rootCmd.PersistentFlags().String("log-level", "", "Set the logging level [trace, debug, info, warn, error]") |
| 133 | rootCmd.PersistentFlags().String("log-format", "text", "Set the logging format [text, json]") |
| 134 | rootCmd.PersistentFlags().Bool("debug", false, "Debug mode") |
| 135 | TODO: |
| 136 | rootCmd.PersistentFlags().Bool("tty", isatty.IsTerminal(os.Stdout.Fd()), "Enable TUI interactions such as opening an editor. Defaults to true when stdout is a terminal. Set to false for automation.") |
| 137 | rootCmd.PersistentFlags().BoolP("yes", "y", false, "Alias of --tty=false") |
| 138 | rootCmd.PersistentPreRunE = func(cmd *cobra.Command, _ []string) error { |
| 139 | if err := processGlobalFlags(rootCmd); err != nil { |
| 140 | return err |
| 141 | } |
| 142 | |
| 143 | if osutil.IsBeingRosettaTranslated() && cmd.Parent().Name() != "completion" && cmd.Name() != "generate-doc" && cmd.Name() != "validate" { |
| 144 | |
| 145 | |
| 146 | return errors.New("limactl is running under rosetta, please reinstall lima with native arch") |
| 147 | } |
| 148 | |
| 149 | |
| 150 | |
| 151 | dir, err := dirnames.LimaDir() |
| 152 | if err != nil { |
| 153 | return err |
| 154 | } |
| 155 | nfs, err := fsutil.IsNFS(dir) |
| 156 | if err != nil { |
| 157 | return err |
| 158 | } |
| 159 | if nfs { |
| 160 | logrus.Warn("LIMA_HOME should not be on an NFS mount") |
| 161 | } |
| 162 | |
| 163 | if cmd.Flags().Changed("yes") && cmd.Flags().Changed("tty") { |
| 164 | return errors.New("cannot use both --tty and --yes flags at the same time") |
| 165 | } |
| 166 | |
| 167 | if cmd.Flags().Changed("yes") { |
| 168 | switch cmd.Name() { |
| 169 | case "clone", "edit", "rename": |
| 170 | logrus.Warn("--yes flag is deprecated (--tty=false is still supported and works in the same way. Also consider using --start)") |
| 171 | } |
| 172 | |
| 173 | |
| 174 | yesValue, _ := cmd.Flags().GetBool("yes") |
| 175 | if yesValue { |
| 176 | |
| 177 | err := cmd.Flags().Set("tty", "false") |
| 178 | if err != nil { |
| 179 | return err |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | return nil |
| 184 | } |
| 185 | rootCmd.AddGroup(&cobra.Group{ID: "basic", Title: "Basic Commands:"}) |
| 186 | rootCmd.AddGroup(&cobra.Group{ID: "advanced", Title: "Advanced Commands:"}) |
| 187 | rootCmd.AddGroup(&cobra.Group{ID: "plugin", Title: "Available Plugins (Experimental):"}) |
| 188 | |
| 189 | rootCmd.AddCommand( |
| 190 | newCreateCommand(), |
| 191 | newStartCommand(), |
| 192 | newStopCommand(), |
| 193 | newShellCommand(), |
| 194 | newCopyCommand(), |
| 195 | newListCommand(), |
| 196 | newDeleteCommand(), |
| 197 | newValidateCommand(), |
| 198 | newPruneCommand(), |
| 199 | newHostagentCommand(), |
| 200 | newGuestInstallCommand(), |
| 201 | newInfoCommand(), |
| 202 | newShowSSHCommand(), |
| 203 | newDebugCommand(), |
| 204 | newEditCommand(), |
| 205 | newFactoryResetCommand(), |
| 206 | newDiskCommand(), |
| 207 | newUsernetCommand(), |
| 208 | newGenDocCommand(), |
| 209 | newGenSchemaCommand(), |
| 210 | newSnapshotCommand(), |
| 211 | newScreenshotCommand(), |
| 212 | newProtectCommand(), |
| 213 | newUnprotectCommand(), |
| 214 | newTunnelCommand(), |
| 215 | newTemplateCommand(), |
| 216 | newRestartCommand(), |
| 217 | newSudoersCommand(), |
| 218 | newAutostartCommand(), |
| 219 | newStartAtLoginCommand(), |
| 220 | newNetworkCommand(), |
| 221 | newCloneCommand(), |
| 222 | newRenameCommand(), |
| 223 | newWatchCommand(), |
| 224 | newYQRestrictionsHelpCommand(), |
| 225 | ) |
| 226 | addPluginCommands(rootCmd) |
| 227 | |
| 228 | return rootCmd |
| 229 | } |
| 230 | |
| 231 | func addPluginCommands(rootCmd *cobra.Command) { |
| 232 | |
| 233 | |
| 234 | if len(os.Args) > 1 && os.Args[1] == "--debug" { |
| 235 | logrus.SetLevel(logrus.DebugLevel) |
| 236 | } |
| 237 | |
| 238 | allPlugins, err := plugins.Discover() |
| 239 | if err != nil { |
| 240 | logrus.Warnf("Failed to discover plugins: %v", err) |
| 241 | return |
| 242 | } |
| 243 | |
| 244 | for _, plugin := range allPlugins { |
| 245 | pluginCmd := &cobra.Command{ |
| 246 | Use: plugin.Name, |
| 247 | Short: plugin.Description, |
| 248 | GroupID: "plugin", |
| 249 | DisableFlagParsing: true, |
| 250 | SilenceErrors: true, |
| 251 | SilenceUsage: true, |
| 252 | PreRunE: func(*cobra.Command, []string) error { |
| 253 | for i, arg := range os.Args { |
| 254 | if arg == plugin.Name { |
| 255 | |
| 256 | err := rootCmd.ParseFlags(os.Args[1:i]) |
| 257 | if err == nil { |
| 258 | err = processGlobalFlags(rootCmd) |
| 259 | } |
| 260 | return err |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | return nil |
| 265 | }, |
| 266 | Run: func(cmd *cobra.Command, _ []string) { |
| 267 | for i, arg := range os.Args { |
| 268 | if arg == plugin.Name { |
| 269 | |
| 270 | plugin.Run(cmd.Context(), os.Args[i+1:]) |
| 271 | |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | }, |
| 276 | } |
| 277 | |
| 278 | if strings.HasPrefix(plugin.Name, "url-") { |
| 279 | pluginCmd.Hidden = true |
| 280 | } |
| 281 | rootCmd.AddCommand(pluginCmd) |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | |
| 286 | func WrapArgsError(argFn cobra.PositionalArgs) cobra.PositionalArgs { |
| 287 | return func(cmd *cobra.Command, args []string) error { |
| 288 | err := argFn(cmd, args) |
| 289 | if err == nil { |
| 290 | return nil |
| 291 | } |
| 292 | |
| 293 | return fmt.Errorf("%#q %s.\nSee `%s --help`.\n\nUsage: %s\n\n%s", |
| 294 | cmd.CommandPath(), err.Error(), |
| 295 | cmd.CommandPath(), |
| 296 | cmd.UseLine(), cmd.Short, |
| 297 | ) |
| 298 | } |
| 299 | } |