|
| 1 | +package list |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/charmbracelet/bubbles/table" |
| 9 | + tea "github.com/charmbracelet/bubbletea" |
| 10 | + "github.com/goharbor/go-client/pkg/sdk/v2.0/models" |
| 11 | + "github.com/goharbor/harbor-cli/pkg/utils" |
| 12 | + "github.com/goharbor/harbor-cli/pkg/views/base/tablelist" |
| 13 | +) |
| 14 | + |
| 15 | +var columns = []table.Column{ |
| 16 | + {Title: "ID", Width: 4}, |
| 17 | + {Title: "Project", Width: 12}, |
| 18 | + {Title: "Owner Name", Width: 12}, |
| 19 | + {Title: "Storage", Width: 32}, |
| 20 | + {Title: "Creation Time", Width: 20}, |
| 21 | +} |
| 22 | + |
| 23 | +// Function to get project details |
| 24 | +func GetProjectDetails(ref models.QuotaRefObject) (string, string, error) { |
| 25 | + if refMap, ok := ref.(map[string]interface{}); ok { |
| 26 | + projectName, _ := refMap["name"].(string) |
| 27 | + ownerName, _ := refMap["owner_name"].(string) |
| 28 | + return projectName, ownerName, nil |
| 29 | + } |
| 30 | + return "", "", fmt.Errorf("Error: Ref is not of expected type") |
| 31 | +} |
| 32 | + |
| 33 | +// Function to convert bytes to human-readable storage |
| 34 | +func BytesToStorageString(bytes int64) string { |
| 35 | + const ( |
| 36 | + mebibyte = 1024 * 1024 |
| 37 | + gibibyte = 1024 * mebibyte |
| 38 | + ) |
| 39 | + |
| 40 | + mib := float64(bytes) / float64(mebibyte) |
| 41 | + |
| 42 | + if mib >= 1024 { |
| 43 | + gib := mib / 1024 |
| 44 | + return fmt.Sprintf("%.1f GiB", gib) |
| 45 | + } |
| 46 | + |
| 47 | + return fmt.Sprintf("%.2f MiB", mib) |
| 48 | +} |
| 49 | + |
| 50 | +// Function to calculate storage |
| 51 | +func CalculateStorage(hard models.ResourceList, used models.ResourceList) (string, string) { |
| 52 | + var storageUsed, storageGiven string |
| 53 | + |
| 54 | + if hard["storage"] == -1 { |
| 55 | + storageGiven = "Unlimited" |
| 56 | + } else { |
| 57 | + storageGiven = BytesToStorageString(hard["storage"]) |
| 58 | + } |
| 59 | + |
| 60 | + if used["storage"] == 0 { |
| 61 | + storageUsed = "0 MiB" |
| 62 | + } else { |
| 63 | + storageUsed = BytesToStorageString(used["storage"]) |
| 64 | + } |
| 65 | + |
| 66 | + return storageUsed, storageGiven |
| 67 | +} |
| 68 | + |
| 69 | +// Function to format storage |
| 70 | +func FormatStorage(hard models.ResourceList, used models.ResourceList) string { |
| 71 | + storageUsed, storageGiven := CalculateStorage(hard, used) |
| 72 | + return fmt.Sprintf("%v of %v", storageUsed, storageGiven) |
| 73 | +} |
| 74 | + |
| 75 | +// ListQuotas in table format |
| 76 | +func ListQuotas(quotas []*models.Quota) { |
| 77 | + var rows []table.Row |
| 78 | + for _, quota := range quotas { |
| 79 | + projectName, ownerName, err := GetProjectDetails(quota.Ref) |
| 80 | + if err != nil { |
| 81 | + fmt.Println(err) |
| 82 | + continue |
| 83 | + } |
| 84 | + |
| 85 | + storage := FormatStorage(quota.Hard, quota.Used) |
| 86 | + |
| 87 | + createdTime, _ := utils.FormatCreatedTime(quota.CreationTime.String()) |
| 88 | + rows = append(rows, table.Row{ |
| 89 | + strconv.FormatInt(quota.ID, 10), |
| 90 | + projectName, |
| 91 | + ownerName, |
| 92 | + storage, |
| 93 | + createdTime, |
| 94 | + }) |
| 95 | + } |
| 96 | + |
| 97 | + m := tablelist.NewModel(columns, rows, len(rows)) |
| 98 | + |
| 99 | + if _, err := tea.NewProgram(m).Run(); err != nil { |
| 100 | + fmt.Println("Error running program:", err) |
| 101 | + os.Exit(1) |
| 102 | + } |
| 103 | +} |
0 commit comments