Skip to content

Commit

Permalink
Altaf suggetion`s
Browse files Browse the repository at this point in the history
Signed-off-by: axif <[email protected]>
  • Loading branch information
axif0 committed Sep 11, 2024
1 parent 3fc7b33 commit 8b6dac5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
9 changes: 2 additions & 7 deletions pkg/api/health_handler.go
Original file line number Diff line number Diff line change
@@ -1,27 +1,22 @@
package api

import (
"context"
"fmt"

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

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

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

return response, nil
}


40 changes: 16 additions & 24 deletions pkg/views/health/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ 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"
Expand All @@ -10,41 +12,31 @@ import (
)

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

type HealthModel struct {
tableModel tablelist.Model
{Title: "Component", Width: 18},
{Title: "Status", Width: 26},
}

func styleStatus(status string) string {
if status == "healthy" {
return views.GreenStyle.Render(status)
}
return views.RedStyle.Render(status)
}

func NewHealthModel(status *health.GetHealthOK) HealthModel {
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),
})
}

tbl := tablelist.NewModel(columns, rows, len(rows)+1) // +1 for header
return HealthModel{tableModel: tbl}
}

func PrintHealthStatus(status *health.GetHealthOK) {
m := NewHealthModel(status)
fmt.Printf("Harbor Health Status:: %s\n", styleStatus(status.Payload.Status))
m := tablelist.NewModel(columns, rows, len(rows))

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

func styleStatus(status string) string {
if status == "healthy" {
return views.GreenStyle.Render(status)
}
return views.RedStyle.Render(status)
}

0 comments on commit 8b6dac5

Please sign in to comment.