Skip to content

Commit

Permalink
feat: list app backups command
Browse files Browse the repository at this point in the history
  • Loading branch information
steveiliop56 committed Sep 10, 2024
1 parent 0984401 commit 130b949
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ func AppCmd() *cobra.Command {
cmd.AddCommand(subcommands.UpdateAppCmd)
cmd.AddCommand(subcommands.UninstallAppCmd)
cmd.AddCommand(subcommands.StartAllCmd)
cmd.AddCommand(subcommands.ListAppBackupsCmd)
return cmd
}
64 changes: 64 additions & 0 deletions cmd/app/subcommands/list-backups.go
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()
},
}
3 changes: 2 additions & 1 deletion cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/steveiliop56/runtipi-cli-go/internal/constants"
"github.com/steveiliop56/runtipi-cli-go/internal/env"
"github.com/steveiliop56/runtipi-cli-go/internal/system"
"github.com/steveiliop56/runtipi-cli-go/internal/utils"
)

func init() {
Expand Down Expand Up @@ -85,7 +86,7 @@ var debugCmd = &cobra.Command{
sysInfoTable := table.New(os.Stdout)
sysInfoTable.AddRow("OS", operatingSystem)
sysInfoTable.AddRow("OS Version", string(kernel[:]))
sysInfoTable.AddRow("Memory (GB)", fmt.Sprintf("%.2f", float64(memory.Total)/(1<<30)))
sysInfoTable.AddRow("Memory (GB)", utils.FormatFileSize(float64(memory.Total)))
sysInfoTable.AddRow("Architecture", arch)
sysInfoTable.Render()
fmt.Println()
Expand Down
6 changes: 3 additions & 3 deletions cmd/system/subcommands/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ var StatusCmd = &cobra.Command{
spinner.Stop()

// Print status
fmt.Printf("Your CPU usage is %s %% \n", constants.Blue(fmt.Sprintf("%.2f", status.Data.CpuLoad)))
fmt.Printf("Your Disk size is %s GB, you are using %s GB which is %s %% \n", constants.Blue(status.Data.DiskSize), constants.Blue(status.Data.DiskUsed), constants.Blue(status.Data.PercentUsed))
fmt.Printf("Your Memory size is %s GB and you are using %s %% \n", constants.Blue(status.Data.MemoryTotal), constants.Blue(status.Data.PercentUsedMemory))
fmt.Printf("Your CPU usage is %s\n", constants.Blue(fmt.Sprintf("%.2f%%", status.Data.CpuLoad)))
fmt.Printf("Your Disk size is %s, you are using %s which is %s\n", constants.Blue(fmt.Sprintf("%dGB", status.Data.DiskSize)), constants.Blue(fmt.Sprintf("%dGB", status.Data.DiskUsed)), constants.Blue(fmt.Sprintf("%0.f%%", status.Data.PercentUsed)))
fmt.Printf("Your Memory size is %s and you are using %s\n", constants.Blue(fmt.Sprintf("%dGB", status.Data.MemoryTotal)), constants.Blue(fmt.Sprintf("%0.f%%", status.Data.PercentUsedMemory)))
},
}
6 changes: 3 additions & 3 deletions internal/schemas/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ type Settings struct {

type SystemStatus struct {
// Disk
DiskUsed float64 `json:"diskUsed"`
DiskSize float64 `json:"diskSize"`
DiskUsed int64 `json:"diskUsed"`
DiskSize int64 `json:"diskSize"`
PercentUsed float64 `json:"percentUsed"`

// Cpu
CpuLoad float64 `json:"cpuLoad"`

// Memory
MemoryTotal int `json:"memoryTotal"`
MemoryTotal int64 `json:"memoryTotal"`
PercentUsedMemory float64 `json:"percentUsedMemory"`
}

Expand Down
21 changes: 21 additions & 0 deletions internal/utils/utils.go
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)))
}

0 comments on commit 130b949

Please sign in to comment.