Files
docker-compose-backup/cmd/backup/list.go
T
2026-07-04 17:20:28 +08:00

83 lines
1.8 KiB
Go

package backup
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"github.com/spf13/cobra"
"git.misaka.ren/M1saka/docker_backup/internal/backup"
"git.misaka.ren/M1saka/docker_backup/internal/config"
)
var listCmd = &cobra.Command{
Use: "list",
Short: "List configured projects and their backups",
RunE: runList,
}
func init() {
rootCmd.AddCommand(listCmd)
}
func runList(cmd *cobra.Command, args []string) error {
cfg, err := config.Load(cfgFile)
if err != nil {
return fmt.Errorf("load config: %w", err)
}
fmt.Printf("Config: %s\n\n", cfgFile)
for _, proj := range cfg.Projects {
fmt.Printf(" [%s]\n", proj.Name)
fmt.Printf(" path: %s\n", proj.Path)
fmt.Printf(" compose_file: %s\n", proj.ComposeFile)
if proj.Cron != "" {
fmt.Printf(" cron: %s\n", proj.Cron)
}
if len(proj.Exclude) > 0 {
fmt.Printf(" exclude: %s\n", strings.Join(proj.Exclude, ", "))
}
fmt.Printf(" retention: %d copies", proj.Retention.Count)
if proj.Retention.Days > 0 {
fmt.Printf(", %d days", proj.Retention.Days)
}
fmt.Println()
// List existing backups.
backupDir := filepath.Join(cfg.Global.BackupDir, proj.Name)
entries, err := os.ReadDir(backupDir)
if err != nil {
fmt.Printf(" backups: none (dir not found)\n\n")
continue
}
var backups []string
for _, e := range entries {
if !e.IsDir() {
backups = append(backups, e.Name())
}
}
sort.Sort(sort.Reverse(sort.StringSlice(backups)))
if len(backups) == 0 {
fmt.Printf(" backups: none\n\n")
continue
}
fmt.Printf(" backups:\n")
for _, b := range backups {
info, _ := os.Stat(filepath.Join(backupDir, b))
size := ""
if info != nil {
size = backup.FormatSize(info.Size())
}
fmt.Printf(" %s %s\n", b, size)
}
fmt.Println()
}
return nil
}