-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist.go
More file actions
80 lines (68 loc) · 2.06 KB
/
list.go
File metadata and controls
80 lines (68 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package clickhouse
import (
"fmt"
"sort"
"github.com/spf13/cobra"
"github.com/stackvista/stackstate-backup-cli/cmd/cmdutils"
"github.com/stackvista/stackstate-backup-cli/internal/app"
"github.com/stackvista/stackstate-backup-cli/internal/foundation/config"
"github.com/stackvista/stackstate-backup-cli/internal/foundation/output"
"github.com/stackvista/stackstate-backup-cli/internal/orchestration/portforward"
)
func listCmd(globalFlags *config.CLIGlobalFlags) *cobra.Command {
return &cobra.Command{
Use: "list",
Short: "List available Clickhouse backups",
Long: `List all Clickhouse backups from the ClickHouse Backup API.`,
Run: func(_ *cobra.Command, _ []string) {
cmdutils.Run(globalFlags, runList, cmdutils.StorageIsRequired)
},
}
}
func runList(appCtx *app.Context) error {
// Setup port-forward to ClickHouse Backup API
pf, err := portforward.SetupPortForward(
appCtx.K8sClient,
appCtx.Namespace,
appCtx.Config.Clickhouse.BackupService.Name,
appCtx.Config.Clickhouse.BackupService.Port,
appCtx.Logger,
)
if err != nil {
return err
}
defer close(pf.StopChan)
// Create CH client with backup API port only
chClient, err := appCtx.NewCHClient(pf.LocalPort, 0)
if err != nil {
return fmt.Errorf("failed to create ClickHouse client: %w", err)
}
// List backups
appCtx.Logger.Infof("Listing Clickhouse backups...")
appCtx.Logger.Println()
backups, err := chClient.ListBackups(appCtx.Context)
if err != nil {
return fmt.Errorf("failed to list backups: %w", err)
}
if len(backups) == 0 {
appCtx.Formatter.PrintMessage("No backups found")
return nil
}
// Sort by created time (most recent first)
sort.Slice(backups, func(i, j int) bool {
return backups[i].Created > backups[j].Created
})
table := output.Table{
Headers: []string{"NAME", "CREATED", "SIZE"},
Rows: make([][]string, 0, len(backups)),
}
for _, backup := range backups {
row := []string{
backup.Name,
backup.Created,
output.FormatBytes(backup.Size),
}
table.Rows = append(table.Rows, row)
}
return appCtx.Formatter.PrintTable(table)
}