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 ", 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 }