All checks were successful
check / check (push) Successful in 5s
Updates golangci-lint to v2.12.2 everywhere it is pinned and installs the canonical `.golangci.yml`, then remediates every finding the new linter/config surfaces so `make check` is green. ## Version bump - `Dockerfile` lint stage: `golangci/golangci-lint:v2.11.3-alpine` -> `v2.12.2-alpine` (digest-pinned, date comment updated) - `Makefile` `deps` target: `go install` moved from the old v1 module path at `@latest` to the pinned `github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.12.2` - `.golangci.yml` replaced with the canonical config (v2 schema; settings under `linters.settings` so the thresholds actually apply; `default: all` with the standard six disables) - `script/bootstrap` installs golangci-lint via the system package manager and carries no version pin, so it is unchanged - CI (`.gitea/workflows/check.yml`) only runs `script/cibuild`, so it needed no change ## Lint remediation The canonical config surfaced ~3,300 findings across 56k lines. All are fixed, behavior-preserving; incorporates and supersedes the per-package mechanical passes already merged to `main` (refs #61). Highlights: - `err113`: dynamic errors replaced with package sentinels + `%w` wrapping; comparisons via `errors.Is` - `goprintffuncname`: printf-style helpers renamed with an `f` suffix (`ui.Writer` message methods, `cli.ReportErrorf`, `database.Fatalf`) and all call sites updated - `revive` stutter renames: `blob.Handler`, `blob.WithReader`, `blob.ChunkPosition`, `storage.URL`, `storage.Info`; missing doc comments added - `contextcheck`/`noctx`: `context.Context` threaded through `blob.Packer` and the scanner call sites; context-aware `exec`/`sql` variants - `funlen`/`cyclop`/`gocognit`/`dupl`: oversized and duplicated functions split into focused helpers (production and test code) - tests: `t.Parallel()` added where safe (global logger init kept in the serial phase for `-race`), `t.TempDir()`/`t.Helper()` adopted, several suites converted to external test packages - `gosec`: bounded integer conversions, `ReadHeaderTimeout` on the test HTTP server; remaining warnings suppressed per-site with justifications - remaining `nolint` directives are rare, targeted, and each carries a reason (e.g. `nilnil` not-found contract in the repository layer, fx module globals, on-disk snake_case struct tags) - removed the deprecated `log.LogOptions` alias (callers migrated to `log.Options`) `make check` (tests with `-race`, lint, fmt-check) passes. Co-authored-by: sneak <sneak@sneak.berlin> Reviewed-on: #62 Co-authored-by: clawbot <clawbot@noreply.example.org> Co-committed-by: clawbot <clawbot@noreply.example.org>
484 lines
13 KiB
Go
484 lines
13 KiB
Go
package vaultik
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"runtime"
|
|
"sort"
|
|
"strings"
|
|
|
|
"github.com/dustin/go-humanize"
|
|
"sneak.berlin/go/vaultik/internal/log"
|
|
"sneak.berlin/go/vaultik/internal/snapshot"
|
|
)
|
|
|
|
// ShowInfo displays system and configuration information
|
|
func (v *Vaultik) ShowInfo() error {
|
|
// System Information
|
|
v.stdoutf("=== System Information ===\n")
|
|
v.stdoutf("OS/Architecture: %s/%s\n", runtime.GOOS, runtime.GOARCH)
|
|
v.stdoutf("Version: %s\n", v.Globals.Version)
|
|
v.stdoutf("Commit: %s\n", v.Globals.Commit)
|
|
v.stdoutf("Go Version: %s\n", runtime.Version())
|
|
v.printlnStdout()
|
|
|
|
v.showStorageConfig()
|
|
v.showBackupSettings()
|
|
|
|
// Encryption Configuration
|
|
v.stdoutf("=== Encryption Configuration ===\n")
|
|
v.stdoutf("Recipients:\n")
|
|
|
|
for _, recipient := range v.Config.AgeRecipients {
|
|
v.stdoutf(" - %s\n", recipient)
|
|
}
|
|
|
|
v.printlnStdout()
|
|
v.showLocalDatabase()
|
|
|
|
return nil
|
|
}
|
|
|
|
// showStorageConfig prints the storage configuration section. The
|
|
// backend is selected by storage_url (s3://, file://, rclone://); the
|
|
// legacy s3.* fields are only printed when they're actually populated,
|
|
// since the URL scheme is the primary configuration.
|
|
func (v *Vaultik) showStorageConfig() {
|
|
v.stdoutf("=== Storage Configuration ===\n")
|
|
storageInfo := v.Storage.Info()
|
|
v.stdoutf("Type: %s\n", storageInfo.Type)
|
|
v.stdoutf("Location: %s\n", storageInfo.Location)
|
|
|
|
if v.Config.StorageURL != "" {
|
|
v.stdoutf("Storage URL: %s\n", v.Config.StorageURL)
|
|
}
|
|
|
|
if v.Config.S3.Bucket != "" {
|
|
v.stdoutf("S3 Bucket: %s\n", v.Config.S3.Bucket)
|
|
}
|
|
|
|
if v.Config.S3.Prefix != "" {
|
|
v.stdoutf("S3 Prefix: %s\n", v.Config.S3.Prefix)
|
|
}
|
|
|
|
if v.Config.S3.Endpoint != "" {
|
|
v.stdoutf("S3 Endpoint: %s\n", v.Config.S3.Endpoint)
|
|
}
|
|
|
|
if v.Config.S3.Region != "" {
|
|
v.stdoutf("S3 Region: %s\n", v.Config.S3.Region)
|
|
}
|
|
|
|
v.printlnStdout()
|
|
}
|
|
|
|
// showBackupSettings prints the configured snapshots, exclude patterns,
|
|
// and chunking/compression settings.
|
|
func (v *Vaultik) showBackupSettings() {
|
|
v.stdoutf("=== Backup Settings ===\n")
|
|
|
|
// Show configured snapshots
|
|
v.stdoutf("Snapshots:\n")
|
|
|
|
for _, name := range v.Config.SnapshotNames() {
|
|
snap := v.Config.Snapshots[name]
|
|
v.stdoutf(" %s:\n", name)
|
|
|
|
for _, path := range snap.Paths {
|
|
v.stdoutf(" - %s\n", path)
|
|
}
|
|
|
|
if len(snap.Exclude) > 0 {
|
|
v.stdoutf(" exclude: %s\n", strings.Join(snap.Exclude, ", "))
|
|
}
|
|
}
|
|
|
|
// Global exclude patterns
|
|
if len(v.Config.Exclude) > 0 {
|
|
v.stdoutf("Global Exclude: %s\n", strings.Join(v.Config.Exclude, ", "))
|
|
}
|
|
|
|
v.stdoutf("Compression: zstd level %d\n", v.Config.CompressionLevel)
|
|
v.stdoutf("Chunk Size: %s\n", ubytes(int64(v.Config.ChunkSize)))
|
|
v.stdoutf("Blob Size Limit: %s\n", ubytes(int64(v.Config.BlobSizeLimit)))
|
|
v.printlnStdout()
|
|
}
|
|
|
|
// showLocalDatabase prints the local index database section, including
|
|
// record counts when the index exists.
|
|
func (v *Vaultik) showLocalDatabase() {
|
|
v.stdoutf("=== Local Database ===\n")
|
|
v.stdoutf("Index Path: %s\n", v.Config.IndexPath)
|
|
|
|
// Check if index file exists and get its size
|
|
info, err := v.Fs.Stat(v.Config.IndexPath)
|
|
if err != nil {
|
|
v.stdoutf("Index Size: (not created)\n")
|
|
|
|
return
|
|
}
|
|
|
|
v.stdoutf("Index Size: %s\n", ubytes(info.Size()))
|
|
|
|
// Get snapshot count from database
|
|
query := `SELECT COUNT(*) FROM snapshots WHERE completed_at IS NOT NULL`
|
|
|
|
var snapshotCount int
|
|
|
|
err = v.DB.Conn().QueryRowContext(v.ctx, query).Scan(&snapshotCount)
|
|
if err == nil {
|
|
v.stdoutf("Snapshots: %d\n", snapshotCount)
|
|
}
|
|
|
|
// Get blob count from database
|
|
query = `SELECT COUNT(*) FROM blobs`
|
|
|
|
var blobCount int
|
|
|
|
err = v.DB.Conn().QueryRowContext(v.ctx, query).Scan(&blobCount)
|
|
if err == nil {
|
|
v.stdoutf("Blobs: %d\n", blobCount)
|
|
}
|
|
|
|
// Get file count from database
|
|
query = `SELECT COUNT(*) FROM files`
|
|
|
|
var fileCount int
|
|
|
|
err = v.DB.Conn().QueryRowContext(v.ctx, query).Scan(&fileCount)
|
|
if err == nil {
|
|
v.stdoutf("Files: %d\n", fileCount)
|
|
}
|
|
}
|
|
|
|
// Table layout constants for the human-readable remote info output.
|
|
const (
|
|
// snapshotIDColWidth is the SNAPSHOT column width in the remote
|
|
// info table.
|
|
snapshotIDColWidth = 45
|
|
|
|
// metadataKeyParts is the minimum "/"-separated segment count of a
|
|
// metadata object key (metadata/<snapshot-id>/<filename>).
|
|
metadataKeyParts = 3
|
|
|
|
// blobKeyParts is the minimum "/"-separated segment count of a blob
|
|
// object key (blobs/<aa>/<bb>/<hash>).
|
|
blobKeyParts = 4
|
|
)
|
|
|
|
// SnapshotMetadataInfo contains information about a single snapshot's metadata
|
|
//
|
|
//nolint:tagliatelle // snake_case is the established JSON output format
|
|
type SnapshotMetadataInfo struct {
|
|
SnapshotID string `json:"snapshot_id"`
|
|
ManifestSize int64 `json:"manifest_size"`
|
|
DatabaseSize int64 `json:"database_size"`
|
|
TotalSize int64 `json:"total_size"`
|
|
BlobCount int `json:"blob_count"`
|
|
BlobsSize int64 `json:"blobs_size"`
|
|
}
|
|
|
|
// RemoteInfoResult contains all remote storage information
|
|
//
|
|
//nolint:tagliatelle // snake_case is the established JSON output format
|
|
type RemoteInfoResult struct {
|
|
// Storage info
|
|
StorageType string `json:"storage_type"`
|
|
StorageLocation string `json:"storage_location"`
|
|
|
|
// Snapshot metadata
|
|
Snapshots []SnapshotMetadataInfo `json:"snapshots"`
|
|
TotalMetadataSize int64 `json:"total_metadata_size"`
|
|
TotalMetadataCount int `json:"total_metadata_count"`
|
|
|
|
// All blobs on remote
|
|
TotalBlobCount int `json:"total_blob_count"`
|
|
TotalBlobSize int64 `json:"total_blob_size"`
|
|
|
|
// Referenced blobs (from manifests)
|
|
ReferencedBlobCount int `json:"referenced_blob_count"`
|
|
ReferencedBlobSize int64 `json:"referenced_blob_size"`
|
|
|
|
// Orphaned blobs
|
|
OrphanedBlobCount int `json:"orphaned_blob_count"`
|
|
OrphanedBlobSize int64 `json:"orphaned_blob_size"`
|
|
}
|
|
|
|
// RemoteInfo displays information about remote storage
|
|
func (v *Vaultik) RemoteInfo(jsonOutput bool) error {
|
|
log.Info("Starting remote storage info gathering")
|
|
|
|
result := &RemoteInfoResult{}
|
|
|
|
storageInfo := v.Storage.Info()
|
|
result.StorageType = storageInfo.Type
|
|
result.StorageLocation = storageInfo.Location
|
|
|
|
if !jsonOutput {
|
|
v.stdoutf("=== Remote Storage ===\n")
|
|
v.stdoutf("Type: %s\n", storageInfo.Type)
|
|
v.stdoutf("Location: %s\n", storageInfo.Location)
|
|
v.printlnStdout()
|
|
v.stdoutf("Scanning snapshot metadata...\n")
|
|
}
|
|
|
|
snapshotMetadata, snapshotIDs, err := v.collectSnapshotMetadata()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if !jsonOutput {
|
|
v.stdoutf("Downloading %d manifest(s)...\n", len(snapshotIDs))
|
|
}
|
|
|
|
referencedBlobs := v.collectReferencedBlobsFromManifests(snapshotIDs, snapshotMetadata)
|
|
|
|
v.populateRemoteInfoResult(result, snapshotMetadata, snapshotIDs, referencedBlobs)
|
|
|
|
err = v.scanRemoteBlobStorage(result, referencedBlobs, jsonOutput)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
log.Info("Remote info complete",
|
|
"snapshots", result.TotalMetadataCount,
|
|
"total_blobs", result.TotalBlobCount,
|
|
"referenced_blobs", result.ReferencedBlobCount,
|
|
"orphaned_blobs", result.OrphanedBlobCount)
|
|
|
|
if jsonOutput {
|
|
enc := json.NewEncoder(v.Stdout)
|
|
enc.SetIndent("", " ")
|
|
|
|
return enc.Encode(result)
|
|
}
|
|
|
|
v.printRemoteInfoTable(result)
|
|
|
|
return nil
|
|
}
|
|
|
|
// collectSnapshotMetadata scans remote metadata and returns
|
|
// per-snapshot info and sorted IDs.
|
|
func (v *Vaultik) collectSnapshotMetadata() (
|
|
map[string]*SnapshotMetadataInfo, []string, error,
|
|
) {
|
|
snapshotMetadata := make(map[string]*SnapshotMetadataInfo)
|
|
|
|
metadataCh := v.Storage.ListStream(v.ctx, "metadata/")
|
|
for obj := range metadataCh {
|
|
if obj.Err != nil {
|
|
return nil, nil, fmt.Errorf("listing metadata: %w", obj.Err)
|
|
}
|
|
|
|
parts := strings.Split(obj.Key, "/")
|
|
if len(parts) < metadataKeyParts {
|
|
continue
|
|
}
|
|
|
|
snapshotID := parts[1]
|
|
|
|
if _, exists := snapshotMetadata[snapshotID]; !exists {
|
|
snapshotMetadata[snapshotID] = &SnapshotMetadataInfo{SnapshotID: snapshotID}
|
|
}
|
|
|
|
info := snapshotMetadata[snapshotID]
|
|
|
|
filename := parts[2]
|
|
if strings.HasPrefix(filename, "manifest") {
|
|
info.ManifestSize = obj.Size
|
|
} else if strings.HasPrefix(filename, "db") {
|
|
info.DatabaseSize = obj.Size
|
|
}
|
|
|
|
info.TotalSize = info.ManifestSize + info.DatabaseSize
|
|
}
|
|
|
|
var snapshotIDs []string
|
|
for id := range snapshotMetadata {
|
|
snapshotIDs = append(snapshotIDs, id)
|
|
}
|
|
|
|
sort.Strings(snapshotIDs)
|
|
|
|
return snapshotMetadata, snapshotIDs, nil
|
|
}
|
|
|
|
// collectReferencedBlobsFromManifests downloads manifests and returns
|
|
// referenced blob hashes with sizes.
|
|
func (v *Vaultik) collectReferencedBlobsFromManifests(
|
|
snapshotIDs []string, snapshotMetadata map[string]*SnapshotMetadataInfo,
|
|
) map[string]int64 {
|
|
referencedBlobs := make(map[string]int64)
|
|
|
|
for _, snapshotID := range snapshotIDs {
|
|
manifestKey := fmt.Sprintf("metadata/%s/manifest.json.zst", snapshotID)
|
|
|
|
reader, err := v.Storage.Get(v.ctx, manifestKey)
|
|
if err != nil {
|
|
log.Warn("Failed to get manifest", "snapshot", snapshotID, "error", err)
|
|
|
|
continue
|
|
}
|
|
|
|
manifest, err := snapshot.DecodeManifest(reader)
|
|
_ = reader.Close()
|
|
|
|
if err != nil {
|
|
log.Warn("Failed to decode manifest", "snapshot", snapshotID, "error", err)
|
|
|
|
continue
|
|
}
|
|
|
|
info := snapshotMetadata[snapshotID]
|
|
info.BlobCount = manifest.BlobCount
|
|
|
|
var blobsSize int64
|
|
|
|
for _, blob := range manifest.Blobs {
|
|
referencedBlobs[blob.Hash] = blob.CompressedSize
|
|
blobsSize += blob.CompressedSize
|
|
}
|
|
|
|
info.BlobsSize = blobsSize
|
|
}
|
|
|
|
return referencedBlobs
|
|
}
|
|
|
|
// populateRemoteInfoResult fills in the result's snapshot and
|
|
// referenced blob stats.
|
|
func (v *Vaultik) populateRemoteInfoResult(
|
|
result *RemoteInfoResult,
|
|
snapshotMetadata map[string]*SnapshotMetadataInfo,
|
|
snapshotIDs []string,
|
|
referencedBlobs map[string]int64,
|
|
) {
|
|
var totalMetadataSize int64
|
|
|
|
for _, id := range snapshotIDs {
|
|
info := snapshotMetadata[id]
|
|
result.Snapshots = append(result.Snapshots, *info)
|
|
totalMetadataSize += info.TotalSize
|
|
}
|
|
|
|
result.TotalMetadataSize = totalMetadataSize
|
|
result.TotalMetadataCount = len(snapshotIDs)
|
|
|
|
for _, size := range referencedBlobs {
|
|
result.ReferencedBlobCount++
|
|
result.ReferencedBlobSize += size
|
|
}
|
|
}
|
|
|
|
// scanRemoteBlobStorage lists all blobs on remote and computes orphan stats
|
|
func (v *Vaultik) scanRemoteBlobStorage(
|
|
result *RemoteInfoResult, referencedBlobs map[string]int64, jsonOutput bool,
|
|
) error {
|
|
if !jsonOutput {
|
|
v.stdoutf("Scanning blobs...\n")
|
|
}
|
|
|
|
blobCh := v.Storage.ListStream(v.ctx, "blobs/")
|
|
allBlobs := make(map[string]int64)
|
|
|
|
for obj := range blobCh {
|
|
if obj.Err != nil {
|
|
return fmt.Errorf("listing blobs: %w", obj.Err)
|
|
}
|
|
|
|
parts := strings.Split(obj.Key, "/")
|
|
if len(parts) < blobKeyParts {
|
|
continue
|
|
}
|
|
|
|
hash := parts[3]
|
|
allBlobs[hash] = obj.Size
|
|
result.TotalBlobCount++
|
|
result.TotalBlobSize += obj.Size
|
|
}
|
|
|
|
for hash, size := range allBlobs {
|
|
if _, referenced := referencedBlobs[hash]; !referenced {
|
|
result.OrphanedBlobCount++
|
|
result.OrphanedBlobSize += size
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// printRemoteInfoTable renders the human-readable remote info output
|
|
func (v *Vaultik) printRemoteInfoTable(result *RemoteInfoResult) {
|
|
const (
|
|
rowFormat = "%-45s %12s %12s %12s %10s %12s\n"
|
|
sizeColWidth = 12
|
|
countColWidth = 10
|
|
)
|
|
|
|
v.stdoutf("\n=== Snapshot Metadata ===\n")
|
|
|
|
if len(result.Snapshots) == 0 {
|
|
v.stdoutf("No snapshots found\n")
|
|
} else {
|
|
separator := fmt.Sprintf(rowFormat,
|
|
strings.Repeat("-", snapshotIDColWidth),
|
|
strings.Repeat("-", sizeColWidth),
|
|
strings.Repeat("-", sizeColWidth),
|
|
strings.Repeat("-", sizeColWidth),
|
|
strings.Repeat("-", countColWidth),
|
|
strings.Repeat("-", sizeColWidth))
|
|
|
|
v.stdoutf(rowFormat,
|
|
"SNAPSHOT", "MANIFEST", "DATABASE", "TOTAL", "BLOBS", "BLOB SIZE")
|
|
v.stdoutf("%s", separator)
|
|
|
|
for _, info := range result.Snapshots {
|
|
v.stdoutf(rowFormat,
|
|
truncateString(info.SnapshotID, snapshotIDColWidth),
|
|
ubytes(info.ManifestSize),
|
|
ubytes(info.DatabaseSize),
|
|
ubytes(info.TotalSize),
|
|
humanize.Comma(int64(info.BlobCount)),
|
|
ubytes(info.BlobsSize),
|
|
)
|
|
}
|
|
|
|
v.stdoutf("%s", separator)
|
|
v.stdoutf("%-45s %12s %12s %12s\n",
|
|
fmt.Sprintf("Total (%d snapshots)", result.TotalMetadataCount),
|
|
"", "", ubytes(result.TotalMetadataSize))
|
|
}
|
|
|
|
v.stdoutf("\n=== Blob Storage ===\n")
|
|
v.stdoutf("Total blobs on remote: %s (%s)\n",
|
|
humanize.Comma(int64(result.TotalBlobCount)),
|
|
ubytes(result.TotalBlobSize))
|
|
v.stdoutf("Referenced by snapshots: %s (%s)\n",
|
|
humanize.Comma(int64(result.ReferencedBlobCount)),
|
|
ubytes(result.ReferencedBlobSize))
|
|
v.stdoutf("Orphaned (unreferenced): %s (%s)\n",
|
|
humanize.Comma(int64(result.OrphanedBlobCount)),
|
|
ubytes(result.OrphanedBlobSize))
|
|
|
|
if result.OrphanedBlobCount > 0 {
|
|
v.stdoutf("\nRun 'vaultik prune' to remove orphaned blobs.\n")
|
|
}
|
|
}
|
|
|
|
// ellipsis is appended by truncateString when it shortens a string.
|
|
const ellipsis = "..."
|
|
|
|
// truncateString truncates a string to maxLen, adding "..." if truncated
|
|
func truncateString(s string, maxLen int) string {
|
|
if len(s) <= maxLen {
|
|
return s
|
|
}
|
|
|
|
if maxLen <= len(ellipsis) {
|
|
return s[:maxLen]
|
|
}
|
|
|
|
return s[:maxLen-len(ellipsis)] + ellipsis
|
|
}
|