|
| 1 | +// SPDX-FileCopyrightText: Copyright The Lima Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package main |
| 5 | + |
| 6 | +import ( |
| 7 | + "github.com/lima-vm/lima/pkg/instance" |
| 8 | + "github.com/lima-vm/lima/pkg/store" |
| 9 | + "github.com/spf13/cobra" |
| 10 | +) |
| 11 | + |
| 12 | +func newRestartCommand() *cobra.Command { |
| 13 | + restartCmd := &cobra.Command{ |
| 14 | + Use: "restart INSTANCE", |
| 15 | + Short: "Restart a running instance", |
| 16 | + Args: WrapArgsError(cobra.MinimumNArgs(1)), |
| 17 | + RunE: restartAction, |
| 18 | + Aliases: []string{"reboot"}, |
| 19 | + ValidArgsFunction: restartBashComplete, |
| 20 | + GroupID: basicCommand, |
| 21 | + } |
| 22 | + |
| 23 | + restartCmd.Flags().BoolP("force", "f", false, "force stop and restart the instance") |
| 24 | + return restartCmd |
| 25 | +} |
| 26 | + |
| 27 | +func restartAction(cmd *cobra.Command, args []string) error { |
| 28 | + instName := args[0] |
| 29 | + |
| 30 | + inst, err := store.Inspect(instName) |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + |
| 35 | + force, err := cmd.Flags().GetBool("force") |
| 36 | + if err != nil { |
| 37 | + return err |
| 38 | + } |
| 39 | + if force { |
| 40 | + err = instance.RestartForcibly(inst) |
| 41 | + } else { |
| 42 | + err = instance.Restart(inst) |
| 43 | + } |
| 44 | + return err |
| 45 | +} |
| 46 | + |
| 47 | +func restartBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 48 | + return bashCompleteInstanceNames(cmd) |
| 49 | +} |
0 commit comments