2 Commits
Author SHA1 Message Date
sneak 3187af27c7 Report the Go build info version when linker flags set none (closes #422)
check / check (push) Successful in 9m5s
A `go install` of a tag sets no linker flags, so the binary reported
`dev`. Version, commit and commit date that the linker flags leave
unset now come from the build info Go records. Go records a
pseudo-version for an untagged commit and a `+dirty` suffix for a
modified checkout; `IsDevVersion` now counts both as development
builds, using `golang.org/x/mod`.

Model: opus-5-5
2026-11-11 03:39:53 +00:00
clawbot 0d0c76fa24 Exit 1 when a deletion prompt is not confirmed (closes #423)
check / check (push) Failing after 21m27s
prune, snapshot purge, snapshot remove and database delete exited 0 when their confirmation prompt was declined or left unanswered, so a script could not tell that nothing was deleted. They now end with "deletion not confirmed" and exit 1. The [y/N] prompts accept y or Y; database delete still asks for yes.

Model: opus-5-5
2026-11-11 04:09:45 +01:00
13 changed files with 109 additions and 33 deletions

No files matched your search

+2 -2
View File
@@ -1070,8 +1070,8 @@ these:
Only a plain tag, such as `v1.0.0` or `1.0.0`, is a release. If `script/version` Only a plain tag, such as `v1.0.0` or `1.0.0`, is a release. If `script/version`
cannot be run at all, `make` stops with an error instead of building an cannot be run at all, `make` stops with an error instead of building an
unversioned binary, and a binary that somehow carries an empty version string unversioned binary. A binary stamped with an empty version reports the version
still reports itself as a development build. Go recorded in it; if Go recorded none, it is a development build.
### cutting a release ### cutting a release
+4 -3
View File
@@ -7,6 +7,7 @@ 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
@@ -90,12 +91,12 @@ Use --force to skip the confirmation prompt.`,
var confirm string var confirm string
_, err = fmt.Scanln(&confirm) // A failure to read the answer means no confirmation.
_, err = fmt.Fscanln(cmd.InOrStdin(), &confirm)
if err != nil || confirm != "yes" { if err != nil || confirm != "yes" {
_, _ = fmt.Fprintln(w, "Aborted.") _, _ = fmt.Fprintln(w, "Aborted.")
//nolint:nilerr // a failed/aborted confirmation is a clean abort return vaultik.ErrNotConfirmed
return nil
} }
} }
+48
View File
@@ -0,0 +1,48 @@
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())
}
}
+3 -3
View File
@@ -21,8 +21,8 @@ const shortCommitLen = 12
const exitCodeInterrupted = 130 const exitCodeInterrupted = 130
// Entry is the main entry point for the CLI application. // Entry is the main entry point for the CLI application.
// It first calls globals.UseBuildInfo, which fills in the version, // It first calls globals.FillUnsetFromBuildInfo, which fills in the
// commit and commit date that the linker flags did not set. // version, commit and commit date that the linker flags did not set.
// It prints the startup banner to stderr (unless a banner-suppressing // It prints the startup banner to stderr (unless a banner-suppressing
// flag is present in os.Args — see bannerSuppressedInArgs), executes the // flag is present in os.Args — see bannerSuppressedInArgs), executes the
// root cobra command, and routes any returned error through the // root cobra command, and routes any returned error through the
@@ -35,7 +35,7 @@ const exitCodeInterrupted = 130
// deferred profile writers run before the process ends. See run in // deferred profile writers run before the process ends. See run in
// cmd/vaultik/main.go. // cmd/vaultik/main.go.
func Entry() int { func Entry() int {
globals.UseBuildInfo() globals.FillUnsetFromBuildInfo()
emitStartupBanner(os.Args[1:], os.Stderr) emitStartupBanner(os.Args[1:], os.Stderr)
rootCmd := NewRootCommand() rootCmd := NewRootCommand()
+6 -6
View File
@@ -41,26 +41,26 @@ var CommitDate = Unknown //nolint:gochecknoglobals // set via -ldflags at build
// tell which version it built. // tell which version it built.
const develVersion = "(devel)" const develVersion = "(devel)"
// UseBuildInfo applies FromBuildInfo to Version, Commit and CommitDate, // FillUnsetFromBuildInfo applies FillUnset to Version, Commit and
// with the build info of the running binary. // CommitDate, with the build info of the running binary.
func UseBuildInfo() { func FillUnsetFromBuildInfo() {
info, ok := debug.ReadBuildInfo() info, ok := debug.ReadBuildInfo()
if !ok { if !ok {
return return
} }
Version, Commit, CommitDate = FromBuildInfo( Version, Commit, CommitDate = FillUnset(
info, Version, Commit, CommitDate) info, Version, Commit, CommitDate)
} }
// FromBuildInfo returns version, commit and commitDate. Each one that // FillUnset returns version, commit and commitDate. Each one that
// still holds its default ("dev" or empty for the version, "unknown" // still holds its default ("dev" or empty for the version, "unknown"
// for the others) is replaced by the value Go recorded in info. A // for the others) is replaced by the value Go recorded in info. A
// `go install` of a tag sets no linker flags, but Go records the tag // `go install` of a tag sets no linker flags, but Go records the tag
// as the main module's version. Go records the commit and its time // as the main module's version. Go records the commit and its time
// only when it builds from a git checkout. The time is cut to a date, // only when it builds from a git checkout. The time is cut to a date,
// as the Makefile stamps it. // as the Makefile stamps it.
func FromBuildInfo( func FillUnset(
info *debug.BuildInfo, version, commit, commitDate string, info *debug.BuildInfo, version, commit, commitDate string,
) (string, string, string) { ) (string, string, string) {
if (version == "" || version == DevVersion) && if (version == "" || version == DevVersion) &&
+3 -3
View File
@@ -125,10 +125,10 @@ func buildInfo(version, revision string) *debug.BuildInfo {
return info return info
} }
// TestFromBuildInfo covers a binary from `go install` of a tag, which // TestFillUnset covers a binary from `go install` of a tag, which
// carries no linker flags: it must report the tag. Values the linker // carries no linker flags: it must report the tag. Values the linker
// flags set are kept. // flags set are kept.
func TestFromBuildInfo(t *testing.T) { func TestFillUnset(t *testing.T) {
t.Parallel() t.Parallel()
const ( const (
@@ -196,7 +196,7 @@ func TestFromBuildInfo(t *testing.T) {
} }
for _, tc := range cases { for _, tc := range cases {
version, commit, date := globals.FromBuildInfo( version, commit, date := globals.FillUnset(
tc.info, tc.version, tc.commit, tc.date) tc.info, tc.version, tc.commit, tc.date)
if version != tc.wantVersion || commit != tc.wantCommit || if version != tc.wantVersion || commit != tc.wantCommit ||
+1 -1
View File
@@ -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 nil return ErrNotConfirmed
} }
v.deleteUnreferencedBlobs(unreferencedBlobs, allBlobs, result) v.deleteUnreferencedBlobs(unreferencedBlobs, allBlobs, result)
+2 -1
View File
@@ -176,7 +176,8 @@ func TestPruneDeclinedKeepsMetadataDirWithoutManifest(t *testing.T) {
env.v.Stdin = strings.NewReader("n\n") env.v.Stdin = strings.NewReader("n\n")
require.NoError(t, env.v.PruneBlobs(&vaultik.PruneOptions{})) require.ErrorIs(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")
+5 -3
View File
@@ -54,9 +54,10 @@ 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.
require.NoError(t, v.PurgeSnapshotsWithOptions(&vaultik.SnapshotPurgeOptions{ err := 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]")
@@ -77,7 +78,8 @@ 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.NoError(t, env.v.PruneBlobs(&vaultik.PruneOptions{})) require.ErrorIs(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")
+21
View File
@@ -337,6 +337,27 @@ 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()
+4 -9
View File
@@ -881,17 +881,12 @@ 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 { if err != nil || strings.ToLower(confirm) != "y" {
v.printlnStdout("Cancelled") v.printlnStdout("Cancelled")
return nil //nolint:nilerr // treat EOF or read error as "no" return ErrNotConfirmed
}
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:")
@@ -1490,7 +1485,7 @@ func (v *Vaultik) RemoveSnapshot(
} }
if !v.confirmRemoveSnapshot(snapshotID, opts) { if !v.confirmRemoveSnapshot(snapshotID, opts) {
return result, nil return result, ErrNotConfirmed
} }
// Checked before the local record is deleted: it may be the last // Checked before the local record is deleted: it may be the last
+3 -2
View File
@@ -362,8 +362,9 @@ 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.NoError(t, env.v.PurgeSnapshotsWithOptions( require.ErrorIs(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")
} }
+7
View File
@@ -5,6 +5,7 @@ package vaultik
import ( import (
"bytes" "bytes"
"context" "context"
"errors"
"fmt" "fmt"
"io" "io"
"os" "os"
@@ -184,6 +185,12 @@ 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...)