Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions cmd/caib/catalog/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,6 @@ func runAdd(cmd *cobra.Command, args []string) error {
token = os.Getenv("CAIB_TOKEN")
}

ns := namespace
if ns == "" {
ns = defaultNamespace
}

clilog.Infof("Adding image to catalog...\n")
clilog.Infof("✓ Validating registry URL\n")

Expand All @@ -137,7 +132,7 @@ func runAdd(cmd *cobra.Command, args []string) error {
return fmt.Errorf("failed to marshal request: %w", err)
}

reqURL := fmt.Sprintf("%s/v1/catalog/images?namespace=%s", server, ns)
reqURL := fmt.Sprintf("%s/v1/catalog/images", server)
req, err := http.NewRequest(http.MethodPost, reqURL, bytes.NewReader(bodyBytes))
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
Expand Down
3 changes: 0 additions & 3 deletions cmd/caib/catalog/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ import (
)

const (
defaultNamespace = "default"
outputFormatTable = "table"
)

var (
serverURL string
authToken string
namespace string
)

// NewCatalogCmd creates the catalog command with subcommands
Expand All @@ -60,7 +58,6 @@ func NewCatalogCmd() *cobra.Command {
func addCommonFlags(cmd *cobra.Command) {
cmd.Flags().StringVar(&serverURL, "server", "", "REST API server base URL (env: CAIB_SERVER)")
cmd.Flags().StringVar(&authToken, "token", "", "Bearer token for authentication (env: CAIB_TOKEN)")
cmd.Flags().StringVarP(&namespace, "namespace", "n", "", "Kubernetes namespace")
}

// getOutputFormat returns the output format from the root command's --output-format flag.
Expand Down
53 changes: 53 additions & 0 deletions cmd/caib/catalog/catalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package catalog

import (
"testing"
"time"

"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -47,3 +48,55 @@ func TestGetOutputFormat_FallbackWithoutFlag(t *testing.T) {
t.Errorf("expected fallback %q, got %q", testFormatTable, got)
}
}

func TestFormatAge(t *testing.T) {
tests := []struct {
name string
offset time.Duration
want string
}{
{"seconds ago", 30 * time.Second, "30s"},
{"minutes ago", 5 * time.Minute, "5m"},
{"hours ago", 3 * time.Hour, "3h"},
{"days ago", 2 * 24 * time.Hour, "2d"},
{"months ago", 60 * 24 * time.Hour, "2mo"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ts := time.Now().Add(-tt.offset).Format(time.RFC3339)
got := formatAge(ts)
if got != tt.want {
t.Errorf("formatAge(%q) = %q, want %q", ts, got, tt.want)
}
})
}
}

func TestFormatAge_InvalidTimestamp(t *testing.T) {
got := formatAge("not-a-timestamp")
if got != "not-a-timestamp" {
t.Errorf("expected raw string passthrough, got %q", got)
}
}

func TestFormatBytes(t *testing.T) {
tests := []struct {
input int64
want string
}{
{500, "500 B"},
{1024, "1.0 KiB"},
{1536 * 1024, "1.5 MiB"},
{2 * 1024 * 1024 * 1024, "2.0 GiB"},
}

for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
got := formatBytes(tt.input)
if got != tt.want {
t.Errorf("formatBytes(%d) = %q, want %q", tt.input, got, tt.want)
}
})
}
}
58 changes: 45 additions & 13 deletions cmd/caib/catalog/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,7 @@ func runGet(cmd *cobra.Command, args []string) error {
token = os.Getenv("CAIB_TOKEN")
}

ns := namespace
if ns == "" {
ns = defaultNamespace
}

reqURL := fmt.Sprintf("%s/v1/catalog/images/%s?namespace=%s", server, name, ns)
reqURL := fmt.Sprintf("%s/v1/catalog/images/%s", server, name)

req, err := http.NewRequest(http.MethodGet, reqURL, nil)
if err != nil {
Expand All @@ -87,7 +82,7 @@ func runGet(cmd *cobra.Command, args []string) error {
}()

if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("catalog image %q not found in namespace %q", name, ns)
return fmt.Errorf("catalog image %q not found", name)
}

if resp.StatusCode != http.StatusOK {
Expand Down Expand Up @@ -143,34 +138,71 @@ func printImageDetails(img CatalogImageResponse) {
for i, t := range img.Targets {
names[i] = t.Name
}
target = fmt.Sprintf("%v", names)
target = strings.Join(names, ", ")
}

rows := [][2]string{
{"Name", img.Name},
{"Namespace", img.Namespace},
{"Registry URL", img.RegistryURL},
{"Phase", img.Phase},
{"Architecture", img.Architecture},
{"Distro", img.Distro},
{"Targets", target},
{"Build Mode", img.BuildMode},
{"Export Format", img.ExportFormat},
{"Created At", img.CreatedAt},
}
if img.ScheduleName != "" {
rows = append(rows, [2]string{"Schedule", img.ScheduleName})
}
if img.SourceType != "" {
rows = append(rows, [2]string{"Source Type", img.SourceType})
}
if img.SourceImageBuild != "" {
rows = append(rows, [2]string{"Source Build", img.SourceImageBuild})
}
if len(img.Tags) > 0 {
rows = append(rows, [2]string{"Tags", strings.Join(img.Tags, ", ")})
}
if img.SizeBytes > 0 {
rows = append(rows, [2]string{"Size", formatBytes(img.SizeBytes)})
}
if img.DownloadURL != "" {
rows = append(rows, [2]string{"Download URL", img.DownloadURL})
}
if img.StatusReason != "" {
reason := img.StatusReason
if img.StatusMessage != "" {
reason += ": " + img.StatusMessage
}
rows = append(rows, [2]string{"Status Reason", reason})
}
if img.SizeBytes > 0 {
rows = append(rows, [2]string{"Size", fmt.Sprintf("%d bytes", img.SizeBytes)})
}

for _, row := range rows {
if _, err := fmt.Fprintf(w, "%s\t%s\n", row[0], row[1]); err != nil {
if row[1] == "" {
continue
}
if _, err := fmt.Fprintf(w, "%s:\t%s\n", row[0], row[1]); err != nil {
fmt.Fprintf(os.Stderr, "Warning: failed to write output row: %v\n", err)
return
}
}
}

func formatBytes(b int64) string {
const (
kb = 1024
mb = 1024 * kb
gb = 1024 * mb
)
switch {
case b >= gb:
return fmt.Sprintf("%.1f GiB", float64(b)/float64(gb))
case b >= mb:
return fmt.Sprintf("%.1f MiB", float64(b)/float64(mb))
case b >= kb:
return fmt.Sprintf("%.1f KiB", float64(b)/float64(kb))
default:
return fmt.Sprintf("%d B", b)
}
}
Loading
Loading