feat: implement edit and delete host commands

- Add edit command with flag-based and interactive update modes
- Add delete command with confirmation prompt and --force flag
- Include delete alias 'rm' for convenience
- Add comprehensive tests for both commands
- Update project state documentation
This commit is contained in:
swanadiva
2026-06-23 13:52:10 +07:00
parent 368b7cdadb
commit d6b810de47
5 changed files with 453 additions and 8 deletions
+82
View File
@@ -0,0 +1,82 @@
package main
import (
"context"
"fmt"
"github.com/spf13/cobra"
"git.tukangketik.id/swanadiva/hostkeeper/pkg/config"
"git.tukangketik.id/swanadiva/hostkeeper/pkg/storage"
)
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 := storage.NewJSONStorage(cfg.GetDataDir())
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
}