-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0984401
commit 130b949
Showing
6 changed files
with
94 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package subcommands | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
"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" | ||
) | ||
|
||
var ListAppBackupsCmd = &cobra.Command{ | ||
Use: "list-backups [app]", | ||
Short: "List app backups", | ||
Long: "Lists all the backups of an app", | ||
Args: cobra.MinimumNArgs(1), | ||
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", args[0]) | ||
|
||
// Check if folder exists | ||
_, pathCheckErr := os.Stat(backupPath) | ||
if pathCheckErr != nil { | ||
fmt.Printf("%s App backup path doesn't exist\n", constants.Red("✗")) | ||
fmt.Printf("Error: %s\n", pathCheckErr) | ||
os.Exit(1) | ||
} | ||
|
||
// 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", pathCheckErr) | ||
os.Exit(1) | ||
} | ||
|
||
// List backups | ||
backupsTable := table.New(os.Stdout) | ||
backupsTable.SetHeaders("Name", "Size", "Date Created") | ||
for _, backup := range backups { | ||
backupInfo, _ := backup.Info() | ||
backupSize := utils.FormatFileSize(float64(backupInfo.Size())) | ||
backupDateIso, _ := strconv.ParseInt(strings.Replace(strings.Replace(strings.Replace(backup.Name(), args[0], "", 1), ".tar.gz", "", 1), "-", "", 1), 10, 64) | ||
backupDate := time.UnixMilli(backupDateIso).String() | ||
backupsTable.AddRow(backup.Name(), backupSize, backupDate) | ||
} | ||
|
||
backupsTable.Render() | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
) | ||
|
||
func FormatFileSize(fileSize float64) (string) { | ||
if fileSize < (1<<10) { | ||
return fmt.Sprintf("%sB", strconv.Itoa(int(fileSize))) | ||
} else if fileSize > (1<<10) && fileSize < (1<<20) { | ||
return fmt.Sprintf("%.2fKB", fileSize/(1<<10)) | ||
} else if fileSize > (1<<20) && fileSize < (1<<30) { | ||
return fmt.Sprintf("%2.fMB", fileSize/(1<<20)) | ||
} else if fileSize > (1<<30) && fileSize < (1<<40) { | ||
return fmt.Sprintf("%.2fGB", fileSize/(1<<30)) | ||
} else if fileSize > (1<<40) { | ||
return fmt.Sprintf("%.2fTB", fileSize/(1<<40)) | ||
} | ||
return fmt.Sprintf("%sB", strconv.Itoa(int(fileSize))) | ||
} |