-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
limactl protect <INSTANCE>
to prohibit accidental removal
Fix issue 1595 Signed-off-by: Akihiro Suda <[email protected]>
- Loading branch information
1 parent
b4d4c31
commit cdb64c5
Showing
7 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/lima-vm/lima/pkg/store" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newProtectCommand() *cobra.Command { | ||
var protectCommand = &cobra.Command{ | ||
Use: "protect INSTANCE [INSTANCE, ...]", | ||
Short: "Protect an instance to prohibit accidental removal", | ||
Long: `Protect an instance to prohibit accidental removal via the 'limactl delete' command. | ||
The instance is not being protected against removal via '/bin/rm', Finder, etc.`, | ||
Args: WrapArgsError(cobra.MinimumNArgs(1)), | ||
RunE: protectAction, | ||
ValidArgsFunction: protectBashComplete, | ||
} | ||
return protectCommand | ||
} | ||
|
||
func protectAction(_ *cobra.Command, args []string) error { | ||
var errs []error | ||
for _, instName := range args { | ||
inst, err := store.Inspect(instName) | ||
if err != nil { | ||
errs = append(errs, fmt.Errorf("failed to inspect instance %q: %w", instName, err)) | ||
continue | ||
} | ||
if inst.Protected { | ||
logrus.Warnf("Instance %q is already protected. Skipping.", instName) | ||
continue | ||
} | ||
if err := inst.Protect(); err != nil { | ||
errs = append(errs, fmt.Errorf("failed to protect instance %q: %w", instName, err)) | ||
continue | ||
} | ||
logrus.Infof("Protected %q", instName) | ||
} | ||
return errors.Join(errs...) | ||
} | ||
|
||
func protectBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { | ||
return bashCompleteInstanceNames(cmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
|
||
"github.com/lima-vm/lima/pkg/store" | ||
"github.com/sirupsen/logrus" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newUnprotectCommand() *cobra.Command { | ||
var unprotectCommand = &cobra.Command{ | ||
Use: "unprotect INSTANCE [INSTANCE, ...]", | ||
Short: "Unprotect an instance", | ||
Args: WrapArgsError(cobra.MinimumNArgs(1)), | ||
RunE: unprotectAction, | ||
ValidArgsFunction: unprotectBashComplete, | ||
} | ||
return unprotectCommand | ||
} | ||
|
||
func unprotectAction(_ *cobra.Command, args []string) error { | ||
var errs []error | ||
for _, instName := range args { | ||
inst, err := store.Inspect(instName) | ||
if err != nil { | ||
errs = append(errs, fmt.Errorf("failed to inspect instance %q: %w", instName, err)) | ||
continue | ||
} | ||
if !inst.Protected { | ||
logrus.Warnf("Instance %q isn't protected. Skipping.", instName) | ||
continue | ||
} | ||
if err := inst.Unprotect(); err != nil { | ||
errs = append(errs, fmt.Errorf("failed to unprotect instance %q: %w", instName, err)) | ||
continue | ||
} | ||
logrus.Infof("Unprotected %q", instName) | ||
} | ||
return errors.Join(errs...) | ||
} | ||
|
||
func unprotectBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { | ||
return bashCompleteInstanceNames(cmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters