Module path changed from git.eeqj.de/sneak/vaultik to sneak.berlin/go/vaultik (vanity redirect). All imports, ldflags, Dockerfile, goreleaser config, and docs updated. App data/config directories now use plain "vaultik" instead of the reverse-DNS name. README: - New copy-pasteable quickstart at top: go install, config init, age keypair, config set for key + file:// destination, home backup - All command names in command details are code-quoted - config set/get gained sequence index support (age_recipients.0) so lists are settable from the CLI - Dockerfile build is CGO_ENABLED=0 to match the pure-Go build
135 lines
3.9 KiB
Go
135 lines
3.9 KiB
Go
package vaultik
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"sneak.berlin/go/vaultik/internal/types"
|
|
)
|
|
|
|
// SnapshotInfo contains information about a snapshot
|
|
type SnapshotInfo struct {
|
|
ID types.SnapshotID `json:"id"`
|
|
Timestamp time.Time `json:"timestamp"`
|
|
CompressedSize int64 `json:"compressed_size"`
|
|
}
|
|
|
|
// formatNumber formats a number with commas
|
|
func formatNumber(n int) string {
|
|
str := fmt.Sprintf("%d", n)
|
|
var result []string
|
|
for i, digit := range str {
|
|
if i > 0 && (len(str)-i)%3 == 0 {
|
|
result = append(result, ",")
|
|
}
|
|
result = append(result, string(digit))
|
|
}
|
|
return strings.Join(result, "")
|
|
}
|
|
|
|
// formatDuration formats a duration in a human-readable way
|
|
func formatDuration(d time.Duration) string {
|
|
if d < time.Second {
|
|
return fmt.Sprintf("%dms", d.Milliseconds())
|
|
}
|
|
if d < time.Minute {
|
|
return fmt.Sprintf("%.1fs", d.Seconds())
|
|
}
|
|
if d < time.Hour {
|
|
mins := int(d.Minutes())
|
|
secs := int(d.Seconds()) % 60
|
|
return fmt.Sprintf("%dm %ds", mins, secs)
|
|
}
|
|
hours := int(d.Hours())
|
|
mins := int(d.Minutes()) % 60
|
|
return fmt.Sprintf("%dh %dm", hours, mins)
|
|
}
|
|
|
|
// formatBytes formats bytes in a human-readable format
|
|
func formatBytes(bytes int64) string {
|
|
const unit = 1024
|
|
if bytes < unit {
|
|
return fmt.Sprintf("%d B", bytes)
|
|
}
|
|
div, exp := int64(unit), 0
|
|
for n := bytes / unit; n >= unit; n /= unit {
|
|
div *= unit
|
|
exp++
|
|
}
|
|
return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
|
|
}
|
|
|
|
// parseSnapshotTimestamp extracts the timestamp from a snapshot ID
|
|
// Format: hostname_snapshotname_2026-01-12T14:41:15Z
|
|
func parseSnapshotTimestamp(snapshotID string) (time.Time, error) {
|
|
parts := strings.Split(snapshotID, "_")
|
|
if len(parts) < 2 {
|
|
return time.Time{}, fmt.Errorf("invalid snapshot ID format: expected hostname_snapshotname_timestamp")
|
|
}
|
|
|
|
// Last part is the RFC3339 timestamp
|
|
timestampStr := parts[len(parts)-1]
|
|
timestamp, err := time.Parse(time.RFC3339, timestampStr)
|
|
if err != nil {
|
|
return time.Time{}, fmt.Errorf("invalid timestamp: %w", err)
|
|
}
|
|
|
|
return timestamp.UTC(), nil
|
|
}
|
|
|
|
// parseSnapshotName extracts the snapshot name from a snapshot ID.
|
|
// Format: hostname_snapshotname_timestamp — the middle part(s) between hostname
|
|
// and the RFC3339 timestamp are the snapshot name (may contain underscores).
|
|
// Returns the snapshot name, or empty string if the ID is malformed.
|
|
func parseSnapshotName(snapshotID string) string {
|
|
parts := strings.Split(snapshotID, "_")
|
|
if len(parts) < 3 {
|
|
// Format: hostname_timestamp — no snapshot name
|
|
return ""
|
|
}
|
|
// Format: hostname_name_timestamp — middle parts are the name.
|
|
// The last part is the RFC3339 timestamp, the first part is the hostname,
|
|
// everything in between is the snapshot name (which may itself contain underscores).
|
|
return strings.Join(parts[1:len(parts)-1], "_")
|
|
}
|
|
|
|
// parseDuration parses a duration string with support for human-friendly units:
|
|
// d/day/days, w/week/weeks, mo/month/months, y/year/years, plus standard Go
|
|
// duration units (h, m, s).
|
|
func parseDuration(s string) (time.Duration, error) {
|
|
if d, err := time.ParseDuration(s); err == nil {
|
|
return d, nil
|
|
}
|
|
|
|
re := regexp.MustCompile(`(\d+)\s*([a-zA-Z]+)`)
|
|
matches := re.FindAllStringSubmatch(s, -1)
|
|
if len(matches) == 0 {
|
|
return 0, fmt.Errorf("invalid duration: %q", s)
|
|
}
|
|
|
|
var total time.Duration
|
|
for _, match := range matches {
|
|
n, err := strconv.Atoi(match[1])
|
|
if err != nil {
|
|
return 0, fmt.Errorf("invalid number %q: %w", match[1], err)
|
|
}
|
|
unit := strings.ToLower(match[2])
|
|
switch unit {
|
|
case "d", "day", "days":
|
|
total += time.Duration(n) * 24 * time.Hour
|
|
case "w", "week", "weeks":
|
|
total += time.Duration(n) * 7 * 24 * time.Hour
|
|
case "mo", "month", "months":
|
|
total += time.Duration(n) * 30 * 24 * time.Hour
|
|
case "y", "year", "years":
|
|
total += time.Duration(n) * 365 * 24 * time.Hour
|
|
default:
|
|
return 0, fmt.Errorf("unknown time unit %q", unit)
|
|
}
|
|
}
|
|
return total, nil
|
|
}
|