847989df75
- git mv cmd/ internal/ pkg/ test/ go.mod go.sum Makefile build.sh docs/ v1/ - Create v1/README.md with V1 documentation - Update root README for V1 + V2 structure - V1 still builds (cd v1 && go build ./cmd/hostkeeper) and 105 tests pass - Root is now clean for V2 development
82 lines
1.8 KiB
Go
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
|
|
}
|