101 lines
2.9 KiB
Go

package main
import (
"backea/internal/backup/strategy"
"context"
"fmt"
"log"
"os"
"text/tabwriter"
"time"
"github.com/joho/godotenv"
)
func main() {
ctx := context.Background()
configPath := "config.yml"
if err := godotenv.Load(); err != nil {
log.Printf("Warning: Error loading .env file: %v", err)
}
factoryBuilder := &strategy.DefaultFactoryBuilder{}
factory, err := factoryBuilder.BuildFactory(*configPath)
if err != nil {
log.Fatalf("Failed to create backup factory: %v", err)
}
config := factory.Config
// Process each service group
for groupName, serviceGroup := range factory.Config.Services {
fmt.Printf("Service Group: %s (Directory: %s)\n", groupName, serviceGroup.Source.Path)
// Process each backup config in the group
for configIndex := range serviceGroup.BackupConfigs {
// Format the full service name
serviceName := fmt.Sprintf("%s.%s", groupName, configIndex)
// Create the appropriate backup strategy using the factory
strategy, err := factory.CreateBackupStrategyForService(groupName, configIndex)
if err != nil {
log.Printf("Failed to create backup strategy for service %s: %v", serviceName, err)
continue
}
// List backups for this service
backups, err := strategy.ListBackups(ctx, serviceName)
if err != nil {
log.Printf("Failed to list backups for service %s: %v", serviceName, err)
continue
}
// Display the backups
fmt.Printf("Found %d backups for service %s:\n", len(backups), serviceName)
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0)
fmt.Fprintln(w, "----------------------------------------------------")
fmt.Fprintln(w, "ID | Date | Size | Retention")
fmt.Fprintln(w, "----------------------------------------------------")
for _, backup := range backups {
fmt.Fprintf(w, "%s | %s | %s | %s\n",
backup.ID,
backup.CreationTime.Format(time.RFC3339),
formatSize(backup.Size),
backup.RetentionTag,
)
}
fmt.Fprintln(w, "----------------------------------------------------")
w.Flush()
// Display storage usage
usage, err := strategy.GetStorageUsage(ctx, serviceName)
if err != nil {
log.Printf("Failed to get storage usage for service %s: %v", serviceName, err)
continue
}
fmt.Printf("Storage Usage for %s Repository\n", serviceName)
fmt.Println("---------------------------------------")
fmt.Printf("Provider: %s\n", usage.Provider)
fmt.Printf("Repository ID: %s\n", usage.ProviderID)
fmt.Printf("Physical Size: %s\n", formatSize(usage.TotalBytes))
fmt.Println() // Add a blank line between services for readability
}
}
}
func formatSize(size int64) string {
const unit = 1024
if size < unit {
return fmt.Sprintf("%d B", size)
}
div, exp := int64(unit), 0
for n := size / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.2f %cB", float64(size)/float64(div), "KMGTPE"[exp])
}