forked from goharbor/harbor-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request goharbor#170 from mahbub570/health
feat: add cli for health status
- Loading branch information
Showing
5 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters