goharbor / harbor-cli

[Sandbox] Official Harbor CLI
Apache License 2.0
38 stars 45 forks source link

feat: add cli for health status #170

Closed mahbub570 closed 1 month ago

mahbub570 commented 2 months ago

This pr provides a status check of various system components, allowing administrators to quickly assess the overall health and operational status of the Harbor registry system.

Command: harbor health : Get the health status of Harbor components

health

bupd commented 2 months ago

@mahbub570 Have a look at #81. which does the same with colors.

mahbub570 commented 2 months ago

@bupd like this way?

health

bupd commented 2 months ago

Adjust the boudaries of the box

Althaf66 commented 1 month ago

@mahbub570 Make these changes in pkg/views/health/view.go

package health

import (
    "fmt"
    "os"

    "github.com/charmbracelet/bubbles/table"
    "github.com/charmbracelet/bubbletea"
    "github.com/goharbor/go-client/pkg/sdk/v2.0/client/health"
    "github.com/goharbor/harbor-cli/pkg/views"
    "github.com/goharbor/harbor-cli/pkg/views/base/tablelist"
)

var columns = []table.Column{
    {Title: "Component", Width: 18},
    {Title: "Status", Width: 26},
}

func PrintHealthStatus(status *health.GetHealthOK) {
    var rows []table.Row
    fmt.Printf("Harbor Health Status:: %s\n", styleStatus(status.Payload.Status))
    for _, component := range status.Payload.Components {
        rows = append(rows, table.Row{
            component.Name,
            styleStatus(component.Status),
        })
    }

    m := tablelist.NewModel(columns, rows, len(rows))

    if _, err := tea.NewProgram(m).Run(); err != nil {
        fmt.Println("Error running program:", err)
        os.Exit(1)
    }
}

func styleStatus(status string) string {
    if status == "healthy" {
        return views.GreenStyle.Render(status)
    }
    return views.RedStyle.Render(status)
}
Althaf66 commented 1 month ago

pkg/api/health_handler.go

package api

import (
    "fmt"

    "github.com/goharbor/go-client/pkg/sdk/v2.0/client/health"
    "github.com/goharbor/harbor-cli/pkg/utils"
)

func GetHealth() (*health.GetHealthOK, error) {
    ctx, client, err := utils.ContextWithClient()
    if err != nil {
        return nil, err
    }

    response, err := client.Health.GetHealth(ctx,&health.GetHealthParams{})
    if err != nil {
        return nil, fmt.Errorf("error getting health status: %w", err)
    }

    return response, nil
}
mahbub570 commented 1 month ago

thanks @Althaf66 for your suggestions.

Althaf66 commented 1 month ago

ready for review