feat: add dry-run mode, GFS retention policies, and notifications
- Add --dry-run/-n flag for backup commands with comprehensive preflight checks - Database connectivity validation - Required tools availability check - Storage target and permissions verification - Backup size estimation - Encryption and cloud storage configuration validation - Implement GFS (Grandfather-Father-Son) retention policies - Daily/Weekly/Monthly/Yearly tier classification - Configurable retention counts per tier - Custom weekly day and monthly day settings - ISO week handling for proper week boundaries - Add notification system with SMTP and webhook support - SMTP email notifications with TLS/STARTTLS - Webhook HTTP notifications with HMAC-SHA256 signing - Slack-compatible webhook payload format - Event types: backup/restore started/completed/failed, cleanup, verify, PITR - Configurable severity levels and retry logic - Update README.md with documentation for all new features
This commit is contained in:
545
internal/checks/preflight.go
Normal file
545
internal/checks/preflight.go
Normal file
@@ -0,0 +1,545 @@
|
||||
package checks
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"dbbackup/internal/config"
|
||||
"dbbackup/internal/database"
|
||||
"dbbackup/internal/logger"
|
||||
)
|
||||
|
||||
// PreflightCheck represents a single preflight check result
|
||||
type PreflightCheck struct {
|
||||
Name string
|
||||
Status CheckStatus
|
||||
Message string
|
||||
Details string
|
||||
}
|
||||
|
||||
// CheckStatus represents the status of a preflight check
|
||||
type CheckStatus int
|
||||
|
||||
const (
|
||||
StatusPassed CheckStatus = iota
|
||||
StatusWarning
|
||||
StatusFailed
|
||||
StatusSkipped
|
||||
)
|
||||
|
||||
func (s CheckStatus) String() string {
|
||||
switch s {
|
||||
case StatusPassed:
|
||||
return "PASSED"
|
||||
case StatusWarning:
|
||||
return "WARNING"
|
||||
case StatusFailed:
|
||||
return "FAILED"
|
||||
case StatusSkipped:
|
||||
return "SKIPPED"
|
||||
default:
|
||||
return "UNKNOWN"
|
||||
}
|
||||
}
|
||||
|
||||
func (s CheckStatus) Icon() string {
|
||||
switch s {
|
||||
case StatusPassed:
|
||||
return "✓"
|
||||
case StatusWarning:
|
||||
return "⚠"
|
||||
case StatusFailed:
|
||||
return "✗"
|
||||
case StatusSkipped:
|
||||
return "○"
|
||||
default:
|
||||
return "?"
|
||||
}
|
||||
}
|
||||
|
||||
// PreflightResult contains all preflight check results
|
||||
type PreflightResult struct {
|
||||
Checks []PreflightCheck
|
||||
AllPassed bool
|
||||
HasWarnings bool
|
||||
FailureCount int
|
||||
WarningCount int
|
||||
DatabaseInfo *DatabaseInfo
|
||||
StorageInfo *StorageInfo
|
||||
EstimatedSize uint64
|
||||
}
|
||||
|
||||
// DatabaseInfo contains database connection details
|
||||
type DatabaseInfo struct {
|
||||
Type string
|
||||
Version string
|
||||
Host string
|
||||
Port int
|
||||
User string
|
||||
}
|
||||
|
||||
// StorageInfo contains storage target details
|
||||
type StorageInfo struct {
|
||||
Type string // "local" or "cloud"
|
||||
Path string
|
||||
AvailableBytes uint64
|
||||
TotalBytes uint64
|
||||
}
|
||||
|
||||
// PreflightChecker performs preflight checks before backup operations
|
||||
type PreflightChecker struct {
|
||||
cfg *config.Config
|
||||
log logger.Logger
|
||||
db database.Database
|
||||
}
|
||||
|
||||
// NewPreflightChecker creates a new preflight checker
|
||||
func NewPreflightChecker(cfg *config.Config, log logger.Logger) *PreflightChecker {
|
||||
return &PreflightChecker{
|
||||
cfg: cfg,
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
||||
// RunAllChecks runs all preflight checks for a backup operation
|
||||
func (p *PreflightChecker) RunAllChecks(ctx context.Context, dbName string) (*PreflightResult, error) {
|
||||
result := &PreflightResult{
|
||||
Checks: make([]PreflightCheck, 0),
|
||||
AllPassed: true,
|
||||
}
|
||||
|
||||
// 1. Database connectivity check
|
||||
dbCheck := p.checkDatabaseConnectivity(ctx)
|
||||
result.Checks = append(result.Checks, dbCheck)
|
||||
if dbCheck.Status == StatusFailed {
|
||||
result.AllPassed = false
|
||||
result.FailureCount++
|
||||
}
|
||||
|
||||
// Extract database info if connection succeeded
|
||||
if dbCheck.Status == StatusPassed && p.db != nil {
|
||||
version, _ := p.db.GetVersion(ctx)
|
||||
result.DatabaseInfo = &DatabaseInfo{
|
||||
Type: p.cfg.DisplayDatabaseType(),
|
||||
Version: version,
|
||||
Host: p.cfg.Host,
|
||||
Port: p.cfg.Port,
|
||||
User: p.cfg.User,
|
||||
}
|
||||
}
|
||||
|
||||
// 2. Required tools check
|
||||
toolsCheck := p.checkRequiredTools()
|
||||
result.Checks = append(result.Checks, toolsCheck)
|
||||
if toolsCheck.Status == StatusFailed {
|
||||
result.AllPassed = false
|
||||
result.FailureCount++
|
||||
}
|
||||
|
||||
// 3. Storage target check
|
||||
storageCheck := p.checkStorageTarget()
|
||||
result.Checks = append(result.Checks, storageCheck)
|
||||
if storageCheck.Status == StatusFailed {
|
||||
result.AllPassed = false
|
||||
result.FailureCount++
|
||||
} else if storageCheck.Status == StatusWarning {
|
||||
result.HasWarnings = true
|
||||
result.WarningCount++
|
||||
}
|
||||
|
||||
// Extract storage info
|
||||
diskCheck := CheckDiskSpace(p.cfg.BackupDir)
|
||||
result.StorageInfo = &StorageInfo{
|
||||
Type: "local",
|
||||
Path: p.cfg.BackupDir,
|
||||
AvailableBytes: diskCheck.AvailableBytes,
|
||||
TotalBytes: diskCheck.TotalBytes,
|
||||
}
|
||||
|
||||
// 4. Backup size estimation
|
||||
sizeCheck := p.estimateBackupSize(ctx, dbName)
|
||||
result.Checks = append(result.Checks, sizeCheck)
|
||||
if sizeCheck.Status == StatusFailed {
|
||||
result.AllPassed = false
|
||||
result.FailureCount++
|
||||
} else if sizeCheck.Status == StatusWarning {
|
||||
result.HasWarnings = true
|
||||
result.WarningCount++
|
||||
}
|
||||
|
||||
// 5. Encryption configuration check (if enabled)
|
||||
if p.cfg.CloudEnabled || os.Getenv("DBBACKUP_ENCRYPTION_KEY") != "" {
|
||||
encCheck := p.checkEncryptionConfig()
|
||||
result.Checks = append(result.Checks, encCheck)
|
||||
if encCheck.Status == StatusFailed {
|
||||
result.AllPassed = false
|
||||
result.FailureCount++
|
||||
}
|
||||
}
|
||||
|
||||
// 6. Cloud storage check (if enabled)
|
||||
if p.cfg.CloudEnabled {
|
||||
cloudCheck := p.checkCloudStorage(ctx)
|
||||
result.Checks = append(result.Checks, cloudCheck)
|
||||
if cloudCheck.Status == StatusFailed {
|
||||
result.AllPassed = false
|
||||
result.FailureCount++
|
||||
}
|
||||
|
||||
// Update storage info
|
||||
result.StorageInfo.Type = "cloud"
|
||||
result.StorageInfo.Path = fmt.Sprintf("%s://%s/%s", p.cfg.CloudProvider, p.cfg.CloudBucket, p.cfg.CloudPrefix)
|
||||
}
|
||||
|
||||
// 7. Permissions check
|
||||
permCheck := p.checkPermissions()
|
||||
result.Checks = append(result.Checks, permCheck)
|
||||
if permCheck.Status == StatusFailed {
|
||||
result.AllPassed = false
|
||||
result.FailureCount++
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// checkDatabaseConnectivity verifies database connection
|
||||
func (p *PreflightChecker) checkDatabaseConnectivity(ctx context.Context) PreflightCheck {
|
||||
check := PreflightCheck{
|
||||
Name: "Database Connection",
|
||||
}
|
||||
|
||||
// Create database connection
|
||||
db, err := database.New(p.cfg, p.log)
|
||||
if err != nil {
|
||||
check.Status = StatusFailed
|
||||
check.Message = "Failed to create database instance"
|
||||
check.Details = err.Error()
|
||||
return check
|
||||
}
|
||||
|
||||
// Connect
|
||||
if err := db.Connect(ctx); err != nil {
|
||||
check.Status = StatusFailed
|
||||
check.Message = "Connection failed"
|
||||
check.Details = fmt.Sprintf("Cannot connect to %s@%s:%d - %s",
|
||||
p.cfg.User, p.cfg.Host, p.cfg.Port, err.Error())
|
||||
return check
|
||||
}
|
||||
|
||||
// Ping
|
||||
if err := db.Ping(ctx); err != nil {
|
||||
check.Status = StatusFailed
|
||||
check.Message = "Ping failed"
|
||||
check.Details = err.Error()
|
||||
db.Close()
|
||||
return check
|
||||
}
|
||||
|
||||
// Get version
|
||||
version, err := db.GetVersion(ctx)
|
||||
if err != nil {
|
||||
version = "unknown"
|
||||
}
|
||||
|
||||
p.db = db
|
||||
check.Status = StatusPassed
|
||||
check.Message = fmt.Sprintf("OK (%s %s)", p.cfg.DisplayDatabaseType(), version)
|
||||
check.Details = fmt.Sprintf("Connected to %s@%s:%d", p.cfg.User, p.cfg.Host, p.cfg.Port)
|
||||
|
||||
return check
|
||||
}
|
||||
|
||||
// checkRequiredTools verifies backup tools are available
|
||||
func (p *PreflightChecker) checkRequiredTools() PreflightCheck {
|
||||
check := PreflightCheck{
|
||||
Name: "Required Tools",
|
||||
}
|
||||
|
||||
var requiredTools []string
|
||||
if p.cfg.IsPostgreSQL() {
|
||||
requiredTools = []string{"pg_dump", "pg_dumpall"}
|
||||
} else if p.cfg.IsMySQL() {
|
||||
requiredTools = []string{"mysqldump"}
|
||||
}
|
||||
|
||||
var found []string
|
||||
var missing []string
|
||||
var versions []string
|
||||
|
||||
for _, tool := range requiredTools {
|
||||
path, err := exec.LookPath(tool)
|
||||
if err != nil {
|
||||
missing = append(missing, tool)
|
||||
} else {
|
||||
found = append(found, tool)
|
||||
// Try to get version
|
||||
version := getToolVersion(tool)
|
||||
if version != "" {
|
||||
versions = append(versions, fmt.Sprintf("%s %s", tool, version))
|
||||
}
|
||||
}
|
||||
_ = path // silence unused
|
||||
}
|
||||
|
||||
if len(missing) > 0 {
|
||||
check.Status = StatusFailed
|
||||
check.Message = fmt.Sprintf("Missing tools: %s", strings.Join(missing, ", "))
|
||||
check.Details = "Install required database tools and ensure they're in PATH"
|
||||
return check
|
||||
}
|
||||
|
||||
check.Status = StatusPassed
|
||||
check.Message = fmt.Sprintf("%s found", strings.Join(found, ", "))
|
||||
if len(versions) > 0 {
|
||||
check.Details = strings.Join(versions, "; ")
|
||||
}
|
||||
|
||||
return check
|
||||
}
|
||||
|
||||
// checkStorageTarget verifies backup directory is writable
|
||||
func (p *PreflightChecker) checkStorageTarget() PreflightCheck {
|
||||
check := PreflightCheck{
|
||||
Name: "Storage Target",
|
||||
}
|
||||
|
||||
backupDir := p.cfg.BackupDir
|
||||
|
||||
// Check if directory exists
|
||||
info, err := os.Stat(backupDir)
|
||||
if os.IsNotExist(err) {
|
||||
// Try to create it
|
||||
if err := os.MkdirAll(backupDir, 0755); err != nil {
|
||||
check.Status = StatusFailed
|
||||
check.Message = "Cannot create backup directory"
|
||||
check.Details = err.Error()
|
||||
return check
|
||||
}
|
||||
} else if err != nil {
|
||||
check.Status = StatusFailed
|
||||
check.Message = "Cannot access backup directory"
|
||||
check.Details = err.Error()
|
||||
return check
|
||||
} else if !info.IsDir() {
|
||||
check.Status = StatusFailed
|
||||
check.Message = "Backup path is not a directory"
|
||||
check.Details = backupDir
|
||||
return check
|
||||
}
|
||||
|
||||
// Check disk space
|
||||
diskCheck := CheckDiskSpace(backupDir)
|
||||
|
||||
if diskCheck.Critical {
|
||||
check.Status = StatusFailed
|
||||
check.Message = "Insufficient disk space"
|
||||
check.Details = fmt.Sprintf("%s available (%.1f%% used)",
|
||||
formatBytes(diskCheck.AvailableBytes), diskCheck.UsedPercent)
|
||||
return check
|
||||
}
|
||||
|
||||
if diskCheck.Warning {
|
||||
check.Status = StatusWarning
|
||||
check.Message = fmt.Sprintf("%s (%s available, low space warning)",
|
||||
backupDir, formatBytes(diskCheck.AvailableBytes))
|
||||
check.Details = fmt.Sprintf("%.1f%% disk usage", diskCheck.UsedPercent)
|
||||
return check
|
||||
}
|
||||
|
||||
check.Status = StatusPassed
|
||||
check.Message = fmt.Sprintf("%s (%s available)", backupDir, formatBytes(diskCheck.AvailableBytes))
|
||||
check.Details = fmt.Sprintf("%.1f%% used", diskCheck.UsedPercent)
|
||||
|
||||
return check
|
||||
}
|
||||
|
||||
// estimateBackupSize estimates the backup size
|
||||
func (p *PreflightChecker) estimateBackupSize(ctx context.Context, dbName string) PreflightCheck {
|
||||
check := PreflightCheck{
|
||||
Name: "Estimated Backup Size",
|
||||
}
|
||||
|
||||
if p.db == nil {
|
||||
check.Status = StatusSkipped
|
||||
check.Message = "Skipped (no database connection)"
|
||||
return check
|
||||
}
|
||||
|
||||
// Get database size
|
||||
var dbSize int64
|
||||
var err error
|
||||
|
||||
if dbName != "" {
|
||||
dbSize, err = p.db.GetDatabaseSize(ctx, dbName)
|
||||
} else {
|
||||
// For cluster backup, we'd need to sum all databases
|
||||
// For now, just use the default database
|
||||
dbSize, err = p.db.GetDatabaseSize(ctx, p.cfg.Database)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
check.Status = StatusSkipped
|
||||
check.Message = "Could not estimate size"
|
||||
check.Details = err.Error()
|
||||
return check
|
||||
}
|
||||
|
||||
// Estimate compressed size
|
||||
estimatedSize := EstimateBackupSize(uint64(dbSize), p.cfg.CompressionLevel)
|
||||
|
||||
// Check if we have enough space
|
||||
diskCheck := CheckDiskSpace(p.cfg.BackupDir)
|
||||
if diskCheck.AvailableBytes < estimatedSize*2 { // 2x buffer
|
||||
check.Status = StatusWarning
|
||||
check.Message = fmt.Sprintf("~%s (may not fit)", formatBytes(estimatedSize))
|
||||
check.Details = fmt.Sprintf("Only %s available, need ~%s with safety margin",
|
||||
formatBytes(diskCheck.AvailableBytes), formatBytes(estimatedSize*2))
|
||||
return check
|
||||
}
|
||||
|
||||
check.Status = StatusPassed
|
||||
check.Message = fmt.Sprintf("~%s (from %s database)",
|
||||
formatBytes(estimatedSize), formatBytes(uint64(dbSize)))
|
||||
check.Details = fmt.Sprintf("Compression level %d", p.cfg.CompressionLevel)
|
||||
|
||||
return check
|
||||
}
|
||||
|
||||
// checkEncryptionConfig verifies encryption setup
|
||||
func (p *PreflightChecker) checkEncryptionConfig() PreflightCheck {
|
||||
check := PreflightCheck{
|
||||
Name: "Encryption",
|
||||
}
|
||||
|
||||
// Check for encryption key
|
||||
key := os.Getenv("DBBACKUP_ENCRYPTION_KEY")
|
||||
if key == "" {
|
||||
check.Status = StatusSkipped
|
||||
check.Message = "Not configured"
|
||||
check.Details = "Set DBBACKUP_ENCRYPTION_KEY to enable encryption"
|
||||
return check
|
||||
}
|
||||
|
||||
// Validate key length (should be at least 16 characters for AES)
|
||||
if len(key) < 16 {
|
||||
check.Status = StatusFailed
|
||||
check.Message = "Encryption key too short"
|
||||
check.Details = "Key must be at least 16 characters (32 recommended for AES-256)"
|
||||
return check
|
||||
}
|
||||
|
||||
check.Status = StatusPassed
|
||||
check.Message = "AES-256 configured"
|
||||
check.Details = fmt.Sprintf("Key length: %d characters", len(key))
|
||||
|
||||
return check
|
||||
}
|
||||
|
||||
// checkCloudStorage verifies cloud storage access
|
||||
func (p *PreflightChecker) checkCloudStorage(ctx context.Context) PreflightCheck {
|
||||
check := PreflightCheck{
|
||||
Name: "Cloud Storage",
|
||||
}
|
||||
|
||||
if !p.cfg.CloudEnabled {
|
||||
check.Status = StatusSkipped
|
||||
check.Message = "Not configured"
|
||||
return check
|
||||
}
|
||||
|
||||
// Check required cloud configuration
|
||||
if p.cfg.CloudBucket == "" {
|
||||
check.Status = StatusFailed
|
||||
check.Message = "No bucket configured"
|
||||
check.Details = "Set --cloud-bucket or use --cloud URI"
|
||||
return check
|
||||
}
|
||||
|
||||
if p.cfg.CloudProvider == "" {
|
||||
check.Status = StatusFailed
|
||||
check.Message = "No provider configured"
|
||||
check.Details = "Set --cloud-provider (s3, minio, azure, gcs)"
|
||||
return check
|
||||
}
|
||||
|
||||
// Note: Actually testing cloud connectivity would require initializing the cloud backend
|
||||
// For now, just validate configuration is present
|
||||
check.Status = StatusPassed
|
||||
check.Message = fmt.Sprintf("%s://%s configured", p.cfg.CloudProvider, p.cfg.CloudBucket)
|
||||
if p.cfg.CloudPrefix != "" {
|
||||
check.Details = fmt.Sprintf("Prefix: %s", p.cfg.CloudPrefix)
|
||||
}
|
||||
|
||||
return check
|
||||
}
|
||||
|
||||
// checkPermissions verifies write permissions
|
||||
func (p *PreflightChecker) checkPermissions() PreflightCheck {
|
||||
check := PreflightCheck{
|
||||
Name: "Write Permissions",
|
||||
}
|
||||
|
||||
// Try to create a test file
|
||||
testFile := filepath.Join(p.cfg.BackupDir, ".dbbackup_preflight_test")
|
||||
f, err := os.Create(testFile)
|
||||
if err != nil {
|
||||
check.Status = StatusFailed
|
||||
check.Message = "Cannot write to backup directory"
|
||||
check.Details = err.Error()
|
||||
return check
|
||||
}
|
||||
f.Close()
|
||||
os.Remove(testFile)
|
||||
|
||||
check.Status = StatusPassed
|
||||
check.Message = "OK"
|
||||
check.Details = fmt.Sprintf("Can write to %s", p.cfg.BackupDir)
|
||||
|
||||
return check
|
||||
}
|
||||
|
||||
// Close closes any resources (like database connection)
|
||||
func (p *PreflightChecker) Close() error {
|
||||
if p.db != nil {
|
||||
return p.db.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// getToolVersion tries to get the version of a command-line tool
|
||||
func getToolVersion(tool string) string {
|
||||
var cmd *exec.Cmd
|
||||
|
||||
switch tool {
|
||||
case "pg_dump", "pg_dumpall", "pg_restore", "psql":
|
||||
cmd = exec.Command(tool, "--version")
|
||||
case "mysqldump", "mysql":
|
||||
cmd = exec.Command(tool, "--version")
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
|
||||
output, err := cmd.Output()
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
// Extract version from output
|
||||
line := strings.TrimSpace(string(output))
|
||||
// Usually format is "tool (PostgreSQL) X.Y.Z" or "tool Ver X.Y.Z"
|
||||
parts := strings.Fields(line)
|
||||
if len(parts) >= 3 {
|
||||
// Try to find version number
|
||||
for _, part := range parts {
|
||||
if len(part) > 0 && (part[0] >= '0' && part[0] <= '9') {
|
||||
return part
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
134
internal/checks/preflight_test.go
Normal file
134
internal/checks/preflight_test.go
Normal file
@@ -0,0 +1,134 @@
|
||||
package checks
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPreflightResult(t *testing.T) {
|
||||
result := &PreflightResult{
|
||||
Checks: []PreflightCheck{},
|
||||
AllPassed: true,
|
||||
DatabaseInfo: &DatabaseInfo{
|
||||
Type: "postgres",
|
||||
Version: "PostgreSQL 15.0",
|
||||
Host: "localhost",
|
||||
Port: 5432,
|
||||
User: "postgres",
|
||||
},
|
||||
StorageInfo: &StorageInfo{
|
||||
Type: "local",
|
||||
Path: "/backups",
|
||||
AvailableBytes: 10 * 1024 * 1024 * 1024,
|
||||
TotalBytes: 100 * 1024 * 1024 * 1024,
|
||||
},
|
||||
EstimatedSize: 1 * 1024 * 1024 * 1024,
|
||||
}
|
||||
|
||||
if !result.AllPassed {
|
||||
t.Error("Result should be AllPassed")
|
||||
}
|
||||
|
||||
if result.DatabaseInfo.Type != "postgres" {
|
||||
t.Errorf("DatabaseInfo.Type = %q, expected postgres", result.DatabaseInfo.Type)
|
||||
}
|
||||
|
||||
if result.StorageInfo.Path != "/backups" {
|
||||
t.Errorf("StorageInfo.Path = %q, expected /backups", result.StorageInfo.Path)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreflightCheck(t *testing.T) {
|
||||
check := PreflightCheck{
|
||||
Name: "Database Connectivity",
|
||||
Status: StatusPassed,
|
||||
Message: "Connected successfully",
|
||||
Details: "PostgreSQL 15.0",
|
||||
}
|
||||
|
||||
if check.Status != StatusPassed {
|
||||
t.Error("Check status should be passed")
|
||||
}
|
||||
|
||||
if check.Name != "Database Connectivity" {
|
||||
t.Errorf("Check name = %q", check.Name)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckStatusString(t *testing.T) {
|
||||
tests := []struct {
|
||||
status CheckStatus
|
||||
expected string
|
||||
}{
|
||||
{StatusPassed, "PASSED"},
|
||||
{StatusFailed, "FAILED"},
|
||||
{StatusWarning, "WARNING"},
|
||||
{StatusSkipped, "SKIPPED"},
|
||||
}
|
||||
|
||||
for _, tc := range tests {
|
||||
result := tc.status.String()
|
||||
if result != tc.expected {
|
||||
t.Errorf("Status.String() = %q, expected %q", result, tc.expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatPreflightReport(t *testing.T) {
|
||||
result := &PreflightResult{
|
||||
Checks: []PreflightCheck{
|
||||
{Name: "Test Check", Status: StatusPassed, Message: "OK"},
|
||||
},
|
||||
AllPassed: true,
|
||||
DatabaseInfo: &DatabaseInfo{
|
||||
Type: "postgres",
|
||||
Version: "PostgreSQL 15.0",
|
||||
Host: "localhost",
|
||||
Port: 5432,
|
||||
},
|
||||
StorageInfo: &StorageInfo{
|
||||
Type: "local",
|
||||
Path: "/backups",
|
||||
AvailableBytes: 10 * 1024 * 1024 * 1024,
|
||||
},
|
||||
}
|
||||
|
||||
report := FormatPreflightReport(result, "testdb", false)
|
||||
if report == "" {
|
||||
t.Error("Report should not be empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatPreflightReportPlain(t *testing.T) {
|
||||
result := &PreflightResult{
|
||||
Checks: []PreflightCheck{
|
||||
{Name: "Test Check", Status: StatusFailed, Message: "Connection failed"},
|
||||
},
|
||||
AllPassed: false,
|
||||
FailureCount: 1,
|
||||
}
|
||||
|
||||
report := FormatPreflightReportPlain(result, "testdb")
|
||||
if report == "" {
|
||||
t.Error("Report should not be empty")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatPreflightReportJSON(t *testing.T) {
|
||||
result := &PreflightResult{
|
||||
Checks: []PreflightCheck{},
|
||||
AllPassed: true,
|
||||
}
|
||||
|
||||
report, err := FormatPreflightReportJSON(result, "testdb")
|
||||
if err != nil {
|
||||
t.Errorf("FormatPreflightReportJSON() error = %v", err)
|
||||
}
|
||||
|
||||
if len(report) == 0 {
|
||||
t.Error("Report should not be empty")
|
||||
}
|
||||
|
||||
if report[0] != '{' {
|
||||
t.Error("Report should start with '{'")
|
||||
}
|
||||
}
|
||||
184
internal/checks/report.go
Normal file
184
internal/checks/report.go
Normal file
@@ -0,0 +1,184 @@
|
||||
package checks
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// FormatPreflightReport formats preflight results for display
|
||||
func FormatPreflightReport(result *PreflightResult, dbName string, verbose bool) string {
|
||||
var sb strings.Builder
|
||||
|
||||
sb.WriteString("\n")
|
||||
sb.WriteString("╔══════════════════════════════════════════════════════════════╗\n")
|
||||
sb.WriteString("║ [DRY RUN] Preflight Check Results ║\n")
|
||||
sb.WriteString("╚══════════════════════════════════════════════════════════════╝\n")
|
||||
sb.WriteString("\n")
|
||||
|
||||
// Database info
|
||||
if result.DatabaseInfo != nil {
|
||||
sb.WriteString(fmt.Sprintf(" Database: %s %s\n", result.DatabaseInfo.Type, result.DatabaseInfo.Version))
|
||||
sb.WriteString(fmt.Sprintf(" Target: %s@%s:%d",
|
||||
result.DatabaseInfo.User, result.DatabaseInfo.Host, result.DatabaseInfo.Port))
|
||||
if dbName != "" {
|
||||
sb.WriteString(fmt.Sprintf("/%s", dbName))
|
||||
}
|
||||
sb.WriteString("\n\n")
|
||||
}
|
||||
|
||||
// Check results
|
||||
sb.WriteString(" Checks:\n")
|
||||
sb.WriteString(" ─────────────────────────────────────────────────────────────\n")
|
||||
|
||||
for _, check := range result.Checks {
|
||||
icon := check.Status.Icon()
|
||||
color := getStatusColor(check.Status)
|
||||
reset := "\033[0m"
|
||||
|
||||
sb.WriteString(fmt.Sprintf(" %s%s%s %-25s %s\n",
|
||||
color, icon, reset, check.Name+":", check.Message))
|
||||
|
||||
if verbose && check.Details != "" {
|
||||
sb.WriteString(fmt.Sprintf(" └─ %s\n", check.Details))
|
||||
}
|
||||
}
|
||||
|
||||
sb.WriteString(" ─────────────────────────────────────────────────────────────\n")
|
||||
sb.WriteString("\n")
|
||||
|
||||
// Summary
|
||||
if result.AllPassed {
|
||||
if result.HasWarnings {
|
||||
sb.WriteString(" ⚠️ All checks passed with warnings\n")
|
||||
sb.WriteString("\n")
|
||||
sb.WriteString(" Ready to backup. Remove --dry-run to execute.\n")
|
||||
} else {
|
||||
sb.WriteString(" ✅ All checks passed\n")
|
||||
sb.WriteString("\n")
|
||||
sb.WriteString(" Ready to backup. Remove --dry-run to execute.\n")
|
||||
}
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf(" ❌ %d check(s) failed\n", result.FailureCount))
|
||||
sb.WriteString("\n")
|
||||
sb.WriteString(" Fix the issues above before running backup.\n")
|
||||
}
|
||||
|
||||
sb.WriteString("\n")
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// FormatPreflightReportPlain formats preflight results without colors
|
||||
func FormatPreflightReportPlain(result *PreflightResult, dbName string) string {
|
||||
var sb strings.Builder
|
||||
|
||||
sb.WriteString("\n")
|
||||
sb.WriteString("[DRY RUN] Preflight Check Results\n")
|
||||
sb.WriteString("==================================\n")
|
||||
sb.WriteString("\n")
|
||||
|
||||
// Database info
|
||||
if result.DatabaseInfo != nil {
|
||||
sb.WriteString(fmt.Sprintf("Database: %s %s\n", result.DatabaseInfo.Type, result.DatabaseInfo.Version))
|
||||
sb.WriteString(fmt.Sprintf("Target: %s@%s:%d",
|
||||
result.DatabaseInfo.User, result.DatabaseInfo.Host, result.DatabaseInfo.Port))
|
||||
if dbName != "" {
|
||||
sb.WriteString(fmt.Sprintf("/%s", dbName))
|
||||
}
|
||||
sb.WriteString("\n\n")
|
||||
}
|
||||
|
||||
// Check results
|
||||
sb.WriteString("Checks:\n")
|
||||
|
||||
for _, check := range result.Checks {
|
||||
status := fmt.Sprintf("[%s]", check.Status.String())
|
||||
sb.WriteString(fmt.Sprintf(" %-10s %-25s %s\n", status, check.Name+":", check.Message))
|
||||
if check.Details != "" {
|
||||
sb.WriteString(fmt.Sprintf(" └─ %s\n", check.Details))
|
||||
}
|
||||
}
|
||||
|
||||
sb.WriteString("\n")
|
||||
|
||||
// Summary
|
||||
if result.AllPassed {
|
||||
sb.WriteString("Result: READY\n")
|
||||
sb.WriteString("Remove --dry-run to execute backup.\n")
|
||||
} else {
|
||||
sb.WriteString(fmt.Sprintf("Result: FAILED (%d issues)\n", result.FailureCount))
|
||||
sb.WriteString("Fix the issues above before running backup.\n")
|
||||
}
|
||||
|
||||
sb.WriteString("\n")
|
||||
|
||||
return sb.String()
|
||||
}
|
||||
|
||||
// FormatPreflightReportJSON formats preflight results as JSON
|
||||
func FormatPreflightReportJSON(result *PreflightResult, dbName string) ([]byte, error) {
|
||||
type CheckJSON struct {
|
||||
Name string `json:"name"`
|
||||
Status string `json:"status"`
|
||||
Message string `json:"message"`
|
||||
Details string `json:"details,omitempty"`
|
||||
}
|
||||
|
||||
type ReportJSON struct {
|
||||
DryRun bool `json:"dry_run"`
|
||||
AllPassed bool `json:"all_passed"`
|
||||
HasWarnings bool `json:"has_warnings"`
|
||||
FailureCount int `json:"failure_count"`
|
||||
WarningCount int `json:"warning_count"`
|
||||
Database *DatabaseInfo `json:"database,omitempty"`
|
||||
Storage *StorageInfo `json:"storage,omitempty"`
|
||||
TargetDB string `json:"target_database,omitempty"`
|
||||
Checks []CheckJSON `json:"checks"`
|
||||
}
|
||||
|
||||
report := ReportJSON{
|
||||
DryRun: true,
|
||||
AllPassed: result.AllPassed,
|
||||
HasWarnings: result.HasWarnings,
|
||||
FailureCount: result.FailureCount,
|
||||
WarningCount: result.WarningCount,
|
||||
Database: result.DatabaseInfo,
|
||||
Storage: result.StorageInfo,
|
||||
TargetDB: dbName,
|
||||
Checks: make([]CheckJSON, len(result.Checks)),
|
||||
}
|
||||
|
||||
for i, check := range result.Checks {
|
||||
report.Checks[i] = CheckJSON{
|
||||
Name: check.Name,
|
||||
Status: check.Status.String(),
|
||||
Message: check.Message,
|
||||
Details: check.Details,
|
||||
}
|
||||
}
|
||||
|
||||
// Use standard library json encoding
|
||||
return marshalJSON(report)
|
||||
}
|
||||
|
||||
// marshalJSON is a simple JSON marshaler
|
||||
func marshalJSON(v interface{}) ([]byte, error) {
|
||||
return json.MarshalIndent(v, "", " ")
|
||||
}
|
||||
|
||||
// getStatusColor returns ANSI color code for status
|
||||
func getStatusColor(status CheckStatus) string {
|
||||
switch status {
|
||||
case StatusPassed:
|
||||
return "\033[32m" // Green
|
||||
case StatusWarning:
|
||||
return "\033[33m" // Yellow
|
||||
case StatusFailed:
|
||||
return "\033[31m" // Red
|
||||
case StatusSkipped:
|
||||
return "\033[90m" // Gray
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user