check / check (push) Successful in 9m10s
`prune`, `snapshot purge`, `snapshot remove` and `database delete` printed "Cancelled" or "Aborted." and exited 0 when the prompt was answered no or stdin ended. They now return vaultik.ErrNotConfirmed, so the command reports "deletion not confirmed" and exits 1. `database delete` reads its answer from the command's stdin instead of os.Stdin, so a test can supply it. Model: opus-5-5
126 lines
3.3 KiB
Go
126 lines
3.3 KiB
Go
package cli
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/spf13/cobra"
|
|
"sneak.berlin/go/vaultik/internal/config"
|
|
"sneak.berlin/go/vaultik/internal/log"
|
|
"sneak.berlin/go/vaultik/internal/vaultik"
|
|
)
|
|
|
|
// NewDatabaseCommand creates the database command group
|
|
func NewDatabaseCommand() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "database",
|
|
Short: "Manage the local state database",
|
|
Long: `Commands for managing the local SQLite state database.`,
|
|
// cobra checks Args only on a command that has a Run: without
|
|
// one, a mistyped subcommand printed help and exited 0.
|
|
Args: cobra.NoArgs,
|
|
Run: func(cmd *cobra.Command, _ []string) {
|
|
_ = cmd.Help()
|
|
},
|
|
}
|
|
|
|
cmd.AddCommand(
|
|
newDatabaseDeleteCommand(),
|
|
)
|
|
|
|
return cmd
|
|
}
|
|
|
|
// newDatabaseDeleteCommand creates the database delete command.
|
|
// (Renamed from "purge"; the operation removes the SQLite file
|
|
// entirely, which is a delete, not a purge of content.)
|
|
func newDatabaseDeleteCommand() *cobra.Command {
|
|
var force bool
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "delete",
|
|
Short: "Delete the local state database file",
|
|
Long: `Completely removes the local SQLite state database.
|
|
|
|
This will erase all local tracking of:
|
|
- File metadata and change detection state
|
|
- Chunk and blob mappings
|
|
- Local snapshot records
|
|
- The storage-binding record
|
|
|
|
The remote storage is NOT affected. After deletion, the next backup
|
|
will perform a full scan and re-deduplicate against existing remote
|
|
blobs, and the local index will re-bind to the currently configured
|
|
storage destination on that run.
|
|
|
|
Use --force to skip the confirmation prompt.`,
|
|
Args: cobra.NoArgs,
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
|
// Resolve config path
|
|
configPath, err := ResolveConfigPath()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Load config to get database path
|
|
cfg, err := config.Load(configPath)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to load config: %w", err)
|
|
}
|
|
|
|
dbPath := cfg.IndexPath
|
|
out := commandUI(cmd)
|
|
|
|
// Check if database exists
|
|
_, err = os.Stat(dbPath)
|
|
if os.IsNotExist(err) {
|
|
out.Infof("Local state database does not exist: %s.", dbPath)
|
|
|
|
return nil
|
|
}
|
|
|
|
// Confirm unless --force. The prompt and its immediate result
|
|
// are an interactive exchange the operator must see, so they go
|
|
// straight to stdout rather than through the UI and --quiet does
|
|
// not silence them.
|
|
if !force {
|
|
w := cmd.OutOrStdout()
|
|
_, _ = fmt.Fprintf(w,
|
|
"This will delete the local state database at:\n %s\n\n", dbPath)
|
|
_, _ = fmt.Fprint(w, "Are you sure? Type 'yes' to confirm: ")
|
|
|
|
var confirm string
|
|
|
|
// A failure to read the answer means no confirmation.
|
|
_, err = fmt.Fscanln(cmd.InOrStdin(), &confirm)
|
|
if err != nil || confirm != "yes" {
|
|
_, _ = fmt.Fprintln(w, "Aborted.")
|
|
|
|
return vaultik.ErrNotConfirmed
|
|
}
|
|
}
|
|
|
|
// Delete the database file
|
|
err = os.Remove(dbPath)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to delete database: %w", err)
|
|
}
|
|
|
|
// Also delete WAL and SHM files if they exist
|
|
walPath := dbPath + "-wal"
|
|
shmPath := dbPath + "-shm"
|
|
_ = os.Remove(walPath) // Ignore errors - files may not exist
|
|
_ = os.Remove(shmPath)
|
|
|
|
out.Infof("Local state database deleted: %s.", dbPath)
|
|
log.Info("Local state database deleted", "path", dbPath)
|
|
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.Flags().BoolVar(&force, "force", false, "Skip confirmation prompt")
|
|
|
|
return cmd
|
|
}
|