Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add ping command #211

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ harbor help
user.User(),
artifact.Artifact(),
HealthCommand(),
pingCommand(),
)

return root
Expand Down
41 changes: 41 additions & 0 deletions cmd/harbor/root/ping.go
Original file line number Diff line number Diff line change
@@ -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
}
Comment on lines +26 to +41
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest moving this to pkg/api/ping_handler.go. to maintain consistency with other commands.