| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | package main |
| 7 | |
| 8 | import ( |
| 9 | "errors" |
| 10 | "fmt" |
| 11 | "os" |
| 12 | |
| 13 | "github.com/sirupsen/logrus" |
| 14 | "github.com/spf13/cobra" |
| 15 | |
| 16 | "github.com/lima-vm/lima/v2/pkg/autostart" |
| 17 | "github.com/lima-vm/lima/v2/pkg/store" |
| 18 | ) |
| 19 | |
| 20 | func autostartEnableAction(cmd *cobra.Command, args []string) error { |
| 21 | condition, err := cmd.Flags().GetString("condition") |
| 22 | if err != nil { |
| 23 | return err |
| 24 | } |
| 25 | if condition == "boot" { |
| 26 | return errors.New("--condition=boot is only supported on macOS") |
| 27 | } |
| 28 | |
| 29 | ctx := cmd.Context() |
| 30 | inst, err := store.Inspect(ctx, args[0]) |
| 31 | if err != nil { |
| 32 | if errors.Is(err, os.ErrNotExist) { |
| 33 | return fmt.Errorf("instance %q not found", args[0]) |
| 34 | } |
| 35 | return err |
| 36 | } |
| 37 | |
| 38 | keepAlive, err := cmd.Flags().GetBool("keep-alive") |
| 39 | if err != nil { |
| 40 | return err |
| 41 | } |
| 42 | mgr := autostart.ManagerWith(keepAlive) |
| 43 | if err := mgr.RegisterToStartAtLogin(ctx, inst); err != nil { |
| 44 | return fmt.Errorf("failed to register instance %#q to start at login: %w", inst.Name, err) |
| 45 | } |
| 46 | logrus.Infof("Instance %#q registered to start at login", inst.Name) |
| 47 | return nil |
| 48 | } |
| 49 | |
| 50 | func autostartDisableAction(cmd *cobra.Command, args []string) error { |
| 51 | ctx := cmd.Context() |
| 52 | inst, err := store.Inspect(ctx, args[0]) |
| 53 | if err != nil { |
| 54 | if errors.Is(err, os.ErrNotExist) { |
| 55 | return fmt.Errorf("instance %q not found", args[0]) |
| 56 | } |
| 57 | return err |
| 58 | } |
| 59 | |
| 60 | if registered, err := autostart.IsRegistered(ctx, inst); err != nil { |
| 61 | return err |
| 62 | } else if !registered { |
| 63 | logrus.Infof("Instance %#q is not registered for automatic startup", inst.Name) |
| 64 | return nil |
| 65 | } |
| 66 | |
| 67 | if err := autostart.UnregisterFromStartAtLogin(ctx, inst); err != nil { |
| 68 | return fmt.Errorf("failed to unregister instance %#q from start at login: %w", inst.Name, err) |
| 69 | } |
| 70 | logrus.Infof("Instance %#q unregistered from start at login", inst.Name) |
| 71 | return nil |
| 72 | } |