From b306a78105f129703f5b05d329186e79404709b5 Mon Sep 17 00:00:00 2001 From: Stavros Date: Wed, 11 Sep 2024 11:19:36 +0300 Subject: [PATCH] feat: list-backups command --- cmd/app/subcommands/list-backups.go | 2 +- cmd/list-backups.go | 69 +++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 cmd/list-backups.go diff --git a/cmd/app/subcommands/list-backups.go b/cmd/app/subcommands/list-backups.go index b3772c7..b66d040 100644 --- a/cmd/app/subcommands/list-backups.go +++ b/cmd/app/subcommands/list-backups.go @@ -44,7 +44,7 @@ var ListAppBackupsCmd = &cobra.Command{ backups, readErr := os.ReadDir(backupPath) if readErr != nil { fmt.Printf("%s Failed to read app backups folder\n", constants.Red("✗")) - fmt.Printf("Error: %s\n", pathCheckErr) + fmt.Printf("Error: %s\n", readErr) os.Exit(1) } diff --git a/cmd/list-backups.go b/cmd/list-backups.go new file mode 100644 index 0000000..f797752 --- /dev/null +++ b/cmd/list-backups.go @@ -0,0 +1,69 @@ +package cmd + +import ( + "fmt" + "os" + "path" + "path/filepath" + "strconv" + "strings" + "time" + + "github.com/aquasecurity/table" + "github.com/spf13/cobra" + "github.com/steveiliop56/runtipi-cli-go/internal/constants" + "github.com/steveiliop56/runtipi-cli-go/internal/utils" +) + +func init() { + rootCmd.AddCommand(listBackupsCmd) +} + +var listBackupsCmd = &cobra.Command{ + Use: "list-backups", + Short: "List Runtipi backups", + Long: "Use this command to list all the available Runtipi backups", + Run: func(cmd *cobra.Command, args []string) { + // Get root folder + rootFolder, osErr := os.Getwd() + + if osErr != nil { + fmt.Printf("%s Failed to get root folder\n", constants.Red("✗")) + fmt.Printf("Error: %s\n", osErr) + os.Exit(1) + } + + // Define paths + backupPath := path.Join(rootFolder, "backups") + + // Read directory + backups, readErr := os.ReadDir(backupPath) + if readErr != nil { + fmt.Printf("%s Failed to read app backups folder\n", constants.Red("✗")) + fmt.Printf("Error: %s\n", readErr) + os.Exit(1) + } + + // List backups + backupsTable := table.New(os.Stdout) + backupsTable.SetHeaders("Name", "Size", "Date Created") + backupNum := 0 + for _, backup := range backups { + if !backup.IsDir() { + backupInfo, _ := backup.Info() + backupSize := utils.FormatFileSize(float64(backupInfo.Size())) + backupDateIso, _ := strconv.ParseInt(strings.Replace(strings.Replace(strings.Replace(backup.Name(), filepath.Base(rootFolder), "", 1), ".tar.gz", "", 1), "-", "", 1), 10, 64) + backupDate := time.UnixMilli(backupDateIso).String() + backupsTable.AddRow(backup.Name(), backupSize, backupDate) + backupNum += 1 + } + } + + if backupNum == 0 { + fmt.Printf("%s No backups found\n", constants.Red("✗")) + os.Exit(0) + } + + backupsTable.Render() + }, +} \ No newline at end of file