From 8f5cfe508a3810127ddfac2745bc073bf9aa2659 Mon Sep 17 00:00:00 2001 From: Tyler Auerbeck Date: Wed, 16 Oct 2024 21:25:55 -0400 Subject: [PATCH] add ping command Signed-off-by: Tyler Auerbeck --- cmd/harbor/root/cmd.go | 1 + cmd/harbor/root/ping.go | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 cmd/harbor/root/ping.go diff --git a/cmd/harbor/root/cmd.go b/cmd/harbor/root/cmd.go index 94c4f8a3..c86baae8 100644 --- a/cmd/harbor/root/cmd.go +++ b/cmd/harbor/root/cmd.go @@ -110,6 +110,7 @@ harbor help user.User(), artifact.Artifact(), HealthCommand(), + pingCommand(), ) return root diff --git a/cmd/harbor/root/ping.go b/cmd/harbor/root/ping.go new file mode 100644 index 00000000..8f8beac7 --- /dev/null +++ b/cmd/harbor/root/ping.go @@ -0,0 +1,41 @@ +package root + +import ( + "fmt" + + "github.com/goharbor/go-client/pkg/sdk/v2.0/client/ping" + "github.com/goharbor/harbor-cli/pkg/utils" + "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +func pingCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "ping", + Short: "Ping the Harbor server", + Long: `Ping the Harbor server to check if it is alive.`, + Example: ` harbor ping`, + RunE: func(cmd *cobra.Command, args []string) error { + return runPing() + }, + } + + return cmd +} + +func runPing() error { + ctx, client, err := utils.ContextWithClient() + if err != nil { + logrus.Errorf("failed to get client: %v", err) + return err + } + + resp, err := client.Ping.GetPing(ctx, &ping.GetPingParams{}) + if err != nil { + logrus.Errorf("failed to ping the server: %v", err) + return err + } + + fmt.Printf("Ping: %s\n", resp.Payload) + return nil +}