All checks were successful
check / check (push) Successful in 2m31s
The banner is printed to stdout before cobra parses, and bannerSuppressedInArgs recognised only --quiet, -q and --cron. So every --json document was preceded by two banner lines and a blank one, and `vaultik snapshot list --json | jq` failed. Passing opts.JSON as extraQuiet did not help: that calls UI.SetQuiet in an fx OnStart hook, long after Entry has printed. The raw-argv scan is extended rather than the banner moved after parsing. root.go documents that the banner must survive cobra rejecting its arguments and --help, and no single post-parse location covers those paths. The subcommand-versus-persistent distinction does not decide it: --cron is already in the suppression list and is itself subcommand-only, existing on snapshot create alone, so this adds another instance of an accepted imprecision rather than a new kind. The error directions are asymmetric - a false positive loses a decorative banner, a false negative corrupts a document - so the scan errs toward suppression, which is also why --json=false suppresses, exactly as --quiet=false already does. Four of the five --json commands now pipe into jq cleanly with no other flags: snapshot list, snapshot verify, snapshot remove, remote info. prune does not, because pruneLocalSnapshots writes three prose lines to stdout with no --json awareness. That reproduces identically before this change and -q never suppressed it either, since printlnStdout and stdoutf bypass v.UI entirely. Tracked as #108. Also fixed: TTYHandler's human-readable byte formatting did not survive grouping, because the key check compared against the bare attribute name and a grouped record presents it qualified. AGENTS.md policy 9 keyed the log format on stdout's TTY-ness, which #82 made false by moving the logger to stderr; it now names the log stream. Vaultik.Stderr keeps its field with the comment amended to say outright that nothing writes to it, and the dead listEnv.stderr is removed.
218 lines
6.5 KiB
Go
218 lines
6.5 KiB
Go
// Package vaultik implements the core backup, restore, verify, prune,
|
|
// and snapshot-management operations behind the vaultik CLI.
|
|
package vaultik
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
|
|
"github.com/spf13/afero"
|
|
"go.uber.org/fx"
|
|
"sneak.berlin/go/vaultik/internal/config"
|
|
"sneak.berlin/go/vaultik/internal/crypto"
|
|
"sneak.berlin/go/vaultik/internal/database"
|
|
"sneak.berlin/go/vaultik/internal/globals"
|
|
"sneak.berlin/go/vaultik/internal/snapshot"
|
|
"sneak.berlin/go/vaultik/internal/storage"
|
|
"sneak.berlin/go/vaultik/internal/ui"
|
|
)
|
|
|
|
// Sentinel errors for misconfigured encryption settings.
|
|
var (
|
|
errNoAgeRecipients = errors.New("no age recipients configured")
|
|
errNoAgeSecretKey = errors.New("no age secret key configured")
|
|
)
|
|
|
|
// Vaultik contains all dependencies needed for vaultik operations
|
|
type Vaultik struct {
|
|
Globals *globals.Globals
|
|
Config *config.Config
|
|
DB *database.DB
|
|
Repositories *database.Repositories
|
|
Storage storage.Storer
|
|
ScannerFactory snapshot.ScannerFactory
|
|
SnapshotManager *snapshot.SnapshotManager
|
|
Shutdowner fx.Shutdowner
|
|
Fs afero.Fs
|
|
|
|
// Context management
|
|
ctx context.Context //nolint:containedctx // ctx bound at construction by design
|
|
cancel context.CancelFunc
|
|
|
|
// IO. Stdout carries the output the user asked for and nothing else,
|
|
// so that `--json | jq` works. Stderr completes the standard triple
|
|
// for anything a command needs to write there directly; diagnostics
|
|
// are not that — they go through internal/log, which writes to the
|
|
// process's stderr. No production code writes to Stderr today, so
|
|
// searching for its writers turns up nothing; it is kept as the
|
|
// injection point a direct stderr write would otherwise have to
|
|
// invent, and removing it would make the triple asymmetric for no
|
|
// gain.
|
|
Stdout io.Writer
|
|
Stderr io.Writer
|
|
Stdin io.Reader
|
|
|
|
// UI is the writer for user-facing status, progress, warnings, errors.
|
|
// See package internal/ui for formatting conventions. It always wraps
|
|
// Stdout and is never swapped out; under --cron (and --quiet) the cli
|
|
// layer instead calls UI.SetQuiet(true), which drops Begin, Complete,
|
|
// Info, Notice, Detail, Progress, and Banner messages. Warning and
|
|
// Error are still emitted in that mode, so callers must not assume
|
|
// that --cron makes this writer silent.
|
|
UI *ui.Writer
|
|
|
|
// restoreCacheObserver, if non-nil, is invoked once with the
|
|
// restore-side blob disk cache immediately after the cache is
|
|
// created and again immediately before it is closed. Only
|
|
// internal-package tests set this; the type is unexported so
|
|
// callers outside this package can't reach it.
|
|
restoreCacheObserver func(*blobDiskCache)
|
|
}
|
|
|
|
// Params contains all parameters for New that can be provided by fx
|
|
type Params struct {
|
|
fx.In
|
|
|
|
Globals *globals.Globals
|
|
Config *config.Config
|
|
DB *database.DB
|
|
Repositories *database.Repositories
|
|
Storage storage.Storer
|
|
ScannerFactory snapshot.ScannerFactory
|
|
SnapshotManager *snapshot.SnapshotManager
|
|
Shutdowner fx.Shutdowner
|
|
Fs afero.Fs `optional:"true"`
|
|
}
|
|
|
|
// New creates a new Vaultik instance with proper context management
|
|
// It automatically includes crypto capabilities if age_secret_key is configured
|
|
func New(params Params) *Vaultik {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
// Use provided filesystem or default to OS filesystem
|
|
fs := params.Fs
|
|
if fs == nil {
|
|
fs = afero.NewOsFs()
|
|
}
|
|
|
|
// Set filesystem on SnapshotManager
|
|
params.SnapshotManager.SetFilesystem(fs)
|
|
|
|
return &Vaultik{
|
|
Globals: params.Globals,
|
|
Config: params.Config,
|
|
DB: params.DB,
|
|
Repositories: params.Repositories,
|
|
Storage: params.Storage,
|
|
ScannerFactory: params.ScannerFactory,
|
|
SnapshotManager: params.SnapshotManager,
|
|
Shutdowner: params.Shutdowner,
|
|
Fs: fs,
|
|
ctx: ctx,
|
|
cancel: cancel,
|
|
Stdout: os.Stdout,
|
|
Stderr: os.Stderr,
|
|
Stdin: os.Stdin,
|
|
UI: ui.New(os.Stdout),
|
|
}
|
|
}
|
|
|
|
// Context returns the Vaultik's context
|
|
func (v *Vaultik) Context() context.Context {
|
|
return v.ctx
|
|
}
|
|
|
|
// SetContext sets the Vaultik's context (primarily for testing)
|
|
func (v *Vaultik) SetContext(ctx context.Context) {
|
|
v.ctx = ctx
|
|
}
|
|
|
|
// Cancel cancels the Vaultik's context
|
|
func (v *Vaultik) Cancel() {
|
|
v.cancel()
|
|
}
|
|
|
|
// CanDecrypt returns true if this Vaultik instance has decryption capabilities
|
|
func (v *Vaultik) CanDecrypt() bool {
|
|
return v.Config.AgeSecretKey != ""
|
|
}
|
|
|
|
// GetEncryptor creates a new Encryptor instance based on the configured age recipients
|
|
// Returns an error if no recipients are configured
|
|
func (v *Vaultik) GetEncryptor() (*crypto.Encryptor, error) {
|
|
if len(v.Config.AgeRecipients) == 0 {
|
|
return nil, errNoAgeRecipients
|
|
}
|
|
|
|
return crypto.NewEncryptor(v.Config.AgeRecipients)
|
|
}
|
|
|
|
// GetDecryptor creates a new Decryptor instance based on the configured age secret key
|
|
// Returns an error if no secret key is configured
|
|
func (v *Vaultik) GetDecryptor() (*crypto.Decryptor, error) {
|
|
if v.Config.AgeSecretKey == "" {
|
|
return nil, errNoAgeSecretKey
|
|
}
|
|
|
|
return crypto.NewDecryptor(v.Config.AgeSecretKey)
|
|
}
|
|
|
|
// GetFilesystem returns the filesystem instance used by Vaultik
|
|
//
|
|
//nolint:ireturn // afero.Fs is the filesystem abstraction by design
|
|
func (v *Vaultik) GetFilesystem() afero.Fs {
|
|
return v.Fs
|
|
}
|
|
|
|
// stdoutf writes formatted output to stdout.
|
|
func (v *Vaultik) stdoutf(format string, args ...any) {
|
|
_, _ = fmt.Fprintf(v.Stdout, format, args...)
|
|
}
|
|
|
|
// printlnStdout writes a line to stdout.
|
|
func (v *Vaultik) printlnStdout(args ...any) {
|
|
_, _ = fmt.Fprintln(v.Stdout, args...)
|
|
}
|
|
|
|
// scanStdin reads a line of input from stdin.
|
|
func (v *Vaultik) scanStdin(a ...any) (int, error) {
|
|
return fmt.Fscanln(v.Stdin, a...)
|
|
}
|
|
|
|
// TestVaultik wraps a Vaultik with captured stdout/stderr for testing
|
|
type TestVaultik struct {
|
|
*Vaultik
|
|
|
|
Stdout *bytes.Buffer
|
|
Stderr *bytes.Buffer
|
|
Stdin *bytes.Buffer
|
|
}
|
|
|
|
// NewForTesting creates a minimal Vaultik instance for testing purposes.
|
|
// Only the Storage field is populated; other fields are nil.
|
|
// Returns a TestVaultik that captures stdout/stderr in buffers.
|
|
func NewForTesting(storage storage.Storer) *TestVaultik {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
stdout := &bytes.Buffer{}
|
|
stderr := &bytes.Buffer{}
|
|
stdin := &bytes.Buffer{}
|
|
|
|
return &TestVaultik{
|
|
Vaultik: &Vaultik{
|
|
Storage: storage,
|
|
ctx: ctx,
|
|
cancel: cancel,
|
|
Stdout: stdout,
|
|
Stderr: stderr,
|
|
Stdin: stdin,
|
|
},
|
|
Stdout: stdout,
|
|
Stderr: stderr,
|
|
Stdin: stdin,
|
|
}
|
|
}
|