Skip to content

Commit

Permalink
Merge pull request #170 from mahbub570/health
Browse files Browse the repository at this point in the history
feat: add cli for health status
  • Loading branch information
Vad1mo authored Oct 1, 2024
2 parents bdf0233 + 8b6dac5 commit f839a3c
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ harbor help
repositry.Repository(),
user.User(),
artifact.Artifact(),
HealthCommand(),
)

return root
Expand Down
24 changes: 24 additions & 0 deletions cmd/harbor/root/health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package root

import (
"github.com/goharbor/harbor-cli/pkg/api"
"github.com/spf13/cobra"
"github.com/goharbor/harbor-cli/pkg/views/health"
)

func HealthCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "health",
Short: "Get the health status of Harbor components",
RunE: func(cmd *cobra.Command, args []string) error {
status, err := api.GetHealth()
if err != nil {
return err
}
health.PrintHealthStatus(status)
return nil
},
}

return cmd
}
22 changes: 22 additions & 0 deletions pkg/api/health_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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
}
42 changes: 42 additions & 0 deletions pkg/views/health/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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)
}
2 changes: 2 additions & 0 deletions pkg/views/styles.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var (
SelectedItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("170"))
PaginationStyle = list.DefaultStyles().PaginationStyle.PaddingLeft(4)
HelpStyle = list.DefaultStyles().HelpStyle.PaddingLeft(4).PaddingBottom(1)
RedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#FF0000"))
GreenStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("#04B575"))
)

var BaseStyle = lipgloss.NewStyle().
Expand Down

0 comments on commit f839a3c

Please sign in to comment.