Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c40ff647c3 |
No files matched your search
@@ -7,7 +7,6 @@ import (
|
|||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
"sneak.berlin/go/vaultik/internal/config"
|
"sneak.berlin/go/vaultik/internal/config"
|
||||||
"sneak.berlin/go/vaultik/internal/log"
|
"sneak.berlin/go/vaultik/internal/log"
|
||||||
"sneak.berlin/go/vaultik/internal/vaultik"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewDatabaseCommand creates the database command group
|
// NewDatabaseCommand creates the database command group
|
||||||
@@ -91,12 +90,12 @@ Use --force to skip the confirmation prompt.`,
|
|||||||
|
|
||||||
var confirm string
|
var confirm string
|
||||||
|
|
||||||
// A failure to read the answer means no confirmation.
|
_, err = fmt.Scanln(&confirm)
|
||||||
_, err = fmt.Fscanln(cmd.InOrStdin(), &confirm)
|
|
||||||
if err != nil || confirm != "yes" {
|
if err != nil || confirm != "yes" {
|
||||||
_, _ = fmt.Fprintln(w, "Aborted.")
|
_, _ = fmt.Fprintln(w, "Aborted.")
|
||||||
|
|
||||||
return vaultik.ErrNotConfirmed
|
//nolint:nilerr // a failed/aborted confirmation is a clean abort
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,48 +0,0 @@
|
|||||||
package cli //nolint:testpackage // sets the unexported rootFlags directly
|
|
||||||
|
|
||||||
import (
|
|
||||||
"bytes"
|
|
||||||
"errors"
|
|
||||||
"io"
|
|
||||||
"os"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"sneak.berlin/go/vaultik/internal/vaultik"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestDatabaseDeleteDeclinedIsError checks that answering anything but
|
|
||||||
// "yes" keeps the database and returns ErrNotConfirmed, so the command
|
|
||||||
// exits non-zero.
|
|
||||||
//
|
|
||||||
//nolint:paralleltest // mutates the process-global rootFlags
|
|
||||||
func TestDatabaseDeleteDeclinedIsError(t *testing.T) {
|
|
||||||
dir := t.TempDir()
|
|
||||||
dbPath, cfgPath := seedDatabaseDeleteConfig(t, dir)
|
|
||||||
|
|
||||||
setRootFlags(t, RootFlags{ConfigPath: cfgPath})
|
|
||||||
|
|
||||||
var out bytes.Buffer
|
|
||||||
|
|
||||||
cmd := newDatabaseDeleteCommand()
|
|
||||||
cmd.SetOut(&out)
|
|
||||||
cmd.SetErr(io.Discard)
|
|
||||||
cmd.SetIn(strings.NewReader("no\n"))
|
|
||||||
cmd.SetArgs(nil)
|
|
||||||
|
|
||||||
err := cmd.Execute()
|
|
||||||
if !errors.Is(err, vaultik.ErrNotConfirmed) {
|
|
||||||
t.Errorf("declined database delete returned %v, want %v",
|
|
||||||
err, vaultik.ErrNotConfirmed)
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err = os.Stat(dbPath)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("declined database delete removed the file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.Contains(out.String(), "Aborted.") {
|
|
||||||
t.Errorf("declined database delete printed %q, want Aborted.",
|
|
||||||
out.String())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -267,7 +267,7 @@ func (v *Vaultik) PruneBlobs(opts *PruneOptions) error {
|
|||||||
|
|
||||||
if !v.confirmPrune(opts, len(unreferencedBlobs), totalSize,
|
if !v.confirmPrune(opts, len(unreferencedBlobs), totalSize,
|
||||||
dirsWithoutManifest) {
|
dirsWithoutManifest) {
|
||||||
return ErrNotConfirmed
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
v.deleteUnreferencedBlobs(unreferencedBlobs, allBlobs, result)
|
v.deleteUnreferencedBlobs(unreferencedBlobs, allBlobs, result)
|
||||||
|
|||||||
@@ -176,8 +176,7 @@ func TestPruneDeclinedKeepsMetadataDirWithoutManifest(t *testing.T) {
|
|||||||
|
|
||||||
env.v.Stdin = strings.NewReader("n\n")
|
env.v.Stdin = strings.NewReader("n\n")
|
||||||
|
|
||||||
require.ErrorIs(t, env.v.PruneBlobs(&vaultik.PruneOptions{}),
|
require.NoError(t, env.v.PruneBlobs(&vaultik.PruneOptions{}))
|
||||||
vaultik.ErrNotConfirmed)
|
|
||||||
|
|
||||||
assert.True(t, env.store.hasKey(leftDB),
|
assert.True(t, env.store.hasKey(leftDB),
|
||||||
"a declined prune must keep the directory")
|
"a declined prune must keep the directory")
|
||||||
|
|||||||
@@ -54,10 +54,9 @@ func TestPurgeSnapshots_QuietPromptListsSnapshots(t *testing.T) {
|
|||||||
v.UI.SetQuiet(true)
|
v.UI.SetQuiet(true)
|
||||||
|
|
||||||
// Stdin is empty, so the prompt reads no answer and cancels.
|
// Stdin is empty, so the prompt reads no answer and cancels.
|
||||||
err := v.PurgeSnapshotsWithOptions(&vaultik.SnapshotPurgeOptions{
|
require.NoError(t, v.PurgeSnapshotsWithOptions(&vaultik.SnapshotPurgeOptions{
|
||||||
KeepLatest: true,
|
KeepLatest: true,
|
||||||
})
|
}))
|
||||||
require.ErrorIs(t, err, vaultik.ErrNotConfirmed)
|
|
||||||
|
|
||||||
assert.Contains(t, out.String(), snapHomeT0)
|
assert.Contains(t, out.String(), snapHomeT0)
|
||||||
assert.Contains(t, out.String(), "Delete 1 snapshot(s)? [y/N]")
|
assert.Contains(t, out.String(), "Delete 1 snapshot(s)? [y/N]")
|
||||||
@@ -78,8 +77,7 @@ func TestPruneBlobs_QuietPromptShowsBlobTotal(t *testing.T) {
|
|||||||
addBlob(t, env.store.testStorer, testBlobHashA)
|
addBlob(t, env.store.testStorer, testBlobHashA)
|
||||||
|
|
||||||
// Stdin is empty, so the prompt reads no answer and cancels.
|
// Stdin is empty, so the prompt reads no answer and cancels.
|
||||||
require.ErrorIs(t, env.v.PruneBlobs(&vaultik.PruneOptions{}),
|
require.NoError(t, env.v.PruneBlobs(&vaultik.PruneOptions{}))
|
||||||
vaultik.ErrNotConfirmed)
|
|
||||||
|
|
||||||
assert.Contains(t, env.stdout.String(),
|
assert.Contains(t, env.stdout.String(),
|
||||||
"Found 1 unreferenced blob(s) totaling")
|
"Found 1 unreferenced blob(s) totaling")
|
||||||
|
|||||||
@@ -337,27 +337,6 @@ func TestRemoveSnapshot_DefaultRemovesMetadataNotBlobs(t *testing.T) {
|
|||||||
assert.Contains(t, out, "vaultik prune")
|
assert.Contains(t, out, "vaultik prune")
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestRemoveSnapshot_DeclinedIsError checks that answering no to the
|
|
||||||
// prompt keeps the snapshot and returns ErrNotConfirmed, so the command
|
|
||||||
// exits non-zero.
|
|
||||||
func TestRemoveSnapshot_DeclinedIsError(t *testing.T) {
|
|
||||||
log.Initialize(log.Config{})
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
store := newTestStorer()
|
|
||||||
addManifest(t, store, "snapshot-001", nil)
|
|
||||||
|
|
||||||
tv := vaultik.NewForTesting(store)
|
|
||||||
tv.Repositories = newLocalIndex(t, "snapshot-001")
|
|
||||||
tv.Stdin.WriteString("n\n")
|
|
||||||
|
|
||||||
_, err := tv.RemoveSnapshot("snapshot-001", &vaultik.RemoveOptions{})
|
|
||||||
|
|
||||||
require.ErrorIs(t, err, vaultik.ErrNotConfirmed)
|
|
||||||
assert.True(t, store.hasKey(remoteKeyPath("snapshot-001")))
|
|
||||||
assert.Contains(t, tv.Stdout.String(), "Cancelled")
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRemoveSnapshot_DryRun(t *testing.T) {
|
func TestRemoveSnapshot_DryRun(t *testing.T) {
|
||||||
log.Initialize(log.Config{})
|
log.Initialize(log.Config{})
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|||||||
@@ -881,12 +881,17 @@ func (v *Vaultik) confirmAndExecutePurge(
|
|||||||
|
|
||||||
var confirm string
|
var confirm string
|
||||||
|
|
||||||
// A failure to read the answer means no confirmation.
|
|
||||||
_, err := v.scanStdin(&confirm)
|
_, err := v.scanStdin(&confirm)
|
||||||
if err != nil || strings.ToLower(confirm) != "y" {
|
if err != nil {
|
||||||
v.printlnStdout("Cancelled")
|
v.printlnStdout("Cancelled")
|
||||||
|
|
||||||
return ErrNotConfirmed
|
return nil //nolint:nilerr // treat EOF or read error as "no"
|
||||||
|
}
|
||||||
|
|
||||||
|
if strings.ToLower(confirm) != "y" {
|
||||||
|
v.printlnStdout("Cancelled")
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
} else if !quiet {
|
} else if !quiet {
|
||||||
v.UI.Infof("The following snapshots will be deleted:")
|
v.UI.Infof("The following snapshots will be deleted:")
|
||||||
@@ -1485,7 +1490,7 @@ func (v *Vaultik) RemoveSnapshot(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !v.confirmRemoveSnapshot(snapshotID, opts) {
|
if !v.confirmRemoveSnapshot(snapshotID, opts) {
|
||||||
return result, ErrNotConfirmed
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checked before the local record is deleted: it may be the last
|
// Checked before the local record is deleted: it may be the last
|
||||||
|
|||||||
@@ -362,9 +362,8 @@ func TestSnapshotListAndPurgePrintSizesLikeRemoteInfo(t *testing.T) {
|
|||||||
// Without --force and with nothing on stdin the purge lists what it
|
// Without --force and with nothing on stdin the purge lists what it
|
||||||
// would delete, reads no confirmation, and deletes nothing.
|
// would delete, reads no confirmation, and deletes nothing.
|
||||||
env.stdout.Reset()
|
env.stdout.Reset()
|
||||||
require.ErrorIs(t, env.v.PurgeSnapshotsWithOptions(
|
require.NoError(t, env.v.PurgeSnapshotsWithOptions(
|
||||||
&vaultik.SnapshotPurgeOptions{OlderThan: "1d"}),
|
&vaultik.SnapshotPurgeOptions{OlderThan: "1d"}))
|
||||||
vaultik.ErrNotConfirmed)
|
|
||||||
assert.Contains(t, findTableRow(t, env.stdout.String(), listLocalID),
|
assert.Contains(t, findTableRow(t, env.stdout.String(), listLocalID),
|
||||||
wantSize, "snapshot purge")
|
wantSize, "snapshot purge")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ package vaultik
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
@@ -185,12 +184,6 @@ func (v *Vaultik) printlnStdout(args ...any) {
|
|||||||
_, _ = fmt.Fprintln(v.Stdout, args...)
|
_, _ = fmt.Fprintln(v.Stdout, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ErrNotConfirmed is returned when a confirmation prompt gets any answer
|
|
||||||
// but the one it asks for (`y` or `Y`, or `yes` for `database delete`), or
|
|
||||||
// stdin ends first. Nothing the prompt asked about is deleted, and the
|
|
||||||
// command exits non-zero.
|
|
||||||
var ErrNotConfirmed = errors.New("deletion not confirmed")
|
|
||||||
|
|
||||||
// scanStdin reads a line of input from stdin.
|
// scanStdin reads a line of input from stdin.
|
||||||
func (v *Vaultik) scanStdin(a ...any) (int, error) {
|
func (v *Vaultik) scanStdin(a ...any) (int, error) {
|
||||||
return fmt.Fscanln(v.Stdin, a...)
|
return fmt.Fscanln(v.Stdin, a...)
|
||||||
|
|||||||
Reference in new issue
Block a user