Files
HostKeeper/cmd/hostkeeper/delete.go
T
swanadiva 93957e3989 feat: auto-detect encrypted files + CLI --password flag
- Storage.IsDataEncrypted() checks if hosts.json is encrypted
- TUI auto-detects encrypted files, prompts password automatically
- Root command: --password flag for all CLI commands
- newStorage() helper applies password flag to storage
- add/list/edit/delete commands now support encrypted storage
- passwordSetMsg loads hosts after password is set
2026-06-25 14:34:41 +07:00

82 lines
1.8 KiB
Go

package main
import (
"context"
"fmt"
"github.com/spf13/cobra"
"git.tukangketik.id/swanadiva/hostkeeper/pkg/config"
)
var deleteForce bool
// deleteCmd represents the delete command
var deleteCmd = &cobra.Command{
Use: "delete <host-name-or-id>",
Short: "Delete a saved SSH host",
Long: `Delete a saved SSH host from HostKeeper.
You will be prompted for confirmation unless --force is used.
Examples:
# Delete a host with confirmation
hostkeeper delete myserver
# Delete without confirmation
hostkeeper delete myserver --force`,
Aliases: []string{"rm"},
Args: cobra.ExactArgs(1),
RunE: runDeleteHost,
}
func init() {
deleteCmd.Flags().BoolVar(&deleteForce, "force", false, "delete without confirmation")
rootCmd.AddCommand(deleteCmd)
}
func runDeleteHost(cmd *cobra.Command, args []string) error {
hostIdentifier := args[0]
cfg := appCfg
if cfg == nil {
var err error
cfg, err = config.New()
if err != nil {
return fmt.Errorf("failed to initialize config: %w", err)
}
}
store, err := newStorage(cfg)
if err != nil {
return fmt.Errorf("failed to initialize storage: %w", err)
}
ctx := context.Background()
host, err := findHost(ctx, store, hostIdentifier)
if err != nil {
return err
}
// Confirm deletion unless --force is used
if !deleteForce {
fmt.Printf("Are you sure you want to delete host '%s' (%s@%s:%d)? [y/N]: ",
host.Name, host.Username, host.Hostname, host.Port)
var response string
fmt.Scanln(&response)
if response != "y" && response != "Y" && response != "yes" {
fmt.Println("Deletion cancelled.")
return nil
}
}
if err := store.DeleteHost(ctx, host.ID); err != nil {
return fmt.Errorf("failed to delete host: %w", err)
}
fmt.Printf("✓ Host '%s' deleted successfully\n", host.Name)
return nil
}