From d1b49c8ef3877abbb1e318929c33d170ee997a10 Mon Sep 17 00:00:00 2001 From: mahbub570 Date: Fri, 16 Aug 2024 18:43:40 +0600 Subject: [PATCH 1/4] feat: add cli for health status Signed-off-by: mahbub570 Signed-off-by: axif --- cmd/harbor/root/cmd.go | 1 + cmd/harbor/root/health.go | 24 ++++++++++++++++++++ pkg/api/health_handler.go | 27 +++++++++++++++++++++++ pkg/views/health/view.go | 46 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 98 insertions(+) create mode 100644 cmd/harbor/root/health.go create mode 100644 pkg/api/health_handler.go create mode 100644 pkg/views/health/view.go diff --git a/cmd/harbor/root/cmd.go b/cmd/harbor/root/cmd.go index 7a3fbf3d..94c4f8a3 100644 --- a/cmd/harbor/root/cmd.go +++ b/cmd/harbor/root/cmd.go @@ -109,6 +109,7 @@ harbor help repositry.Repository(), user.User(), artifact.Artifact(), + HealthCommand(), ) return root diff --git a/cmd/harbor/root/health.go b/cmd/harbor/root/health.go new file mode 100644 index 00000000..9baf4dfa --- /dev/null +++ b/cmd/harbor/root/health.go @@ -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 +} diff --git a/pkg/api/health_handler.go b/pkg/api/health_handler.go new file mode 100644 index 00000000..e0893ad0 --- /dev/null +++ b/pkg/api/health_handler.go @@ -0,0 +1,27 @@ +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) { + credentialName := viper.GetString("current-credential-name") + client := utils.GetClientByCredentialName(credentialName) + + ctx := context.Background() + params := health.NewGetHealthParams().WithContext(ctx) + + response, err := client.Health.GetHealth(ctx, params) + if err != nil { + return nil, fmt.Errorf("error getting health status: %w", err) + } + + return response, nil +} + + \ No newline at end of file diff --git a/pkg/views/health/view.go b/pkg/views/health/view.go new file mode 100644 index 00000000..087d848c --- /dev/null +++ b/pkg/views/health/view.go @@ -0,0 +1,46 @@ +package health + +import ( + "fmt" + "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/base/tablelist" +) + +var columns = []table.Column{ + {Title: "Component", Width: 20}, + {Title: "Status", Width: 20}, +} + +type HealthModel struct { + tableModel tablelist.Model +} + +func NewHealthModel(status *health.GetHealthOK) HealthModel { + var rows []table.Row + + rows = append(rows, table.Row{"Overall", status.Payload.Status}) + + for _, component := range status.Payload.Components { + rows = append(rows, table.Row{ + component.Name, + 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.Println("Harbor Health Status:") + fmt.Printf("Status: %s\n", status.Payload.Status) + + p := tea.NewProgram(m.tableModel) + if _, err := p.Run(); err != nil { + fmt.Println("Error running program:", err) + return + } +} \ No newline at end of file From f95bc8cf2646f6fcd5eea81f2e29b9fc36b9299d Mon Sep 17 00:00:00 2001 From: mahbub570 Date: Fri, 16 Aug 2024 21:57:03 +0600 Subject: [PATCH 2/4] add style Signed-off-by: mahbub570 Signed-off-by: axif --- pkg/views/health/view.go | 18 +++++++++++------- pkg/views/styles.go | 2 ++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/pkg/views/health/view.go b/pkg/views/health/view.go index 087d848c..f2ad15d1 100644 --- a/pkg/views/health/view.go +++ b/pkg/views/health/view.go @@ -5,27 +5,32 @@ import ( "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: 20}, - {Title: "Status", Width: 20}, + {Title: "Status", Width: 30}, } type HealthModel struct { tableModel tablelist.Model } +func styleStatus(status string) string { + if status == "healthy" { + return views.GreenStyle.Render(status) + } + return views.RedStyle.Render(status) +} + func NewHealthModel(status *health.GetHealthOK) HealthModel { var rows []table.Row - - rows = append(rows, table.Row{"Overall", status.Payload.Status}) - for _, component := range status.Payload.Components { rows = append(rows, table.Row{ component.Name, - component.Status, + styleStatus(component.Status), }) } @@ -35,8 +40,7 @@ func NewHealthModel(status *health.GetHealthOK) HealthModel { func PrintHealthStatus(status *health.GetHealthOK) { m := NewHealthModel(status) - fmt.Println("Harbor Health Status:") - fmt.Printf("Status: %s\n", status.Payload.Status) + fmt.Printf("Harbor Health Status:: %s\n", styleStatus(status.Payload.Status)) p := tea.NewProgram(m.tableModel) if _, err := p.Run(); err != nil { diff --git a/pkg/views/styles.go b/pkg/views/styles.go index 89efa04e..b00c92be 100644 --- a/pkg/views/styles.go +++ b/pkg/views/styles.go @@ -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(). From 3fc7b3306da0d39c291e520677efd833b483629f Mon Sep 17 00:00:00 2001 From: mahbub570 Date: Sun, 18 Aug 2024 19:43:32 +0600 Subject: [PATCH 3/4] Update pkg/api/health_handler.go Co-authored-by: Prasanth B <89722848+bupd@users.noreply.github.com> Signed-off-by: mahbub570 Signed-off-by: axif --- pkg/api/health_handler.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/api/health_handler.go b/pkg/api/health_handler.go index e0893ad0..805e194d 100644 --- a/pkg/api/health_handler.go +++ b/pkg/api/health_handler.go @@ -10,10 +10,10 @@ import ( ) func GetHealth() (*health.GetHealthOK, error) { - credentialName := viper.GetString("current-credential-name") - client := utils.GetClientByCredentialName(credentialName) - - ctx := context.Background() + ctx, client, err := utils.ContextWithClient() + if err != nil { + return err + } params := health.NewGetHealthParams().WithContext(ctx) response, err := client.Health.GetHealth(ctx, params) From 8b6dac50af28d77de8610484291adbd5f063c1c0 Mon Sep 17 00:00:00 2001 From: axif Date: Wed, 11 Sep 2024 22:31:29 +0600 Subject: [PATCH 4/4] Altaf suggetion`s Signed-off-by: axif --- pkg/api/health_handler.go | 9 ++------- pkg/views/health/view.go | 40 ++++++++++++++++----------------------- 2 files changed, 18 insertions(+), 31 deletions(-) diff --git a/pkg/api/health_handler.go b/pkg/api/health_handler.go index 805e194d..d8eceb37 100644 --- a/pkg/api/health_handler.go +++ b/pkg/api/health_handler.go @@ -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 } - - \ No newline at end of file diff --git a/pkg/views/health/view.go b/pkg/views/health/view.go index f2ad15d1..31813d15 100644 --- a/pkg/views/health/view.go +++ b/pkg/views/health/view.go @@ -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" @@ -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) } \ No newline at end of file