mirror of
https://github.com/luxfi/netrunner.git
synced 2026-07-27 00:04:23 +00:00
74 lines
1.7 KiB
Go
74 lines
1.7 KiB
Go
// Copyright (C) 2021-2025, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package ping
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/luxfi/netrunner/client"
|
|
"github.com/luxfi/netrunner/utils/constants"
|
|
"github.com/luxfi/netrunner/ux"
|
|
luxlog "github.com/luxfi/log"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
var (
|
|
logLevel string
|
|
endpoint string
|
|
dialTimeout time.Duration
|
|
requestTimeout time.Duration
|
|
)
|
|
|
|
func NewCommand() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "ping [options]",
|
|
Short: "Ping the server.",
|
|
RunE: pingFunc,
|
|
}
|
|
|
|
cmd.PersistentFlags().StringVar(&logLevel, "log-level", luxlog.Info.String(), "log level")
|
|
cmd.PersistentFlags().StringVar(&endpoint, "endpoint", "0.0.0.0:8080", "server endpoint")
|
|
cmd.PersistentFlags().DurationVar(&dialTimeout, "dial-timeout", 10*time.Second, "server dial timeout")
|
|
cmd.PersistentFlags().DurationVar(&requestTimeout, "request-timeout", 10*time.Second, "client request timeout")
|
|
|
|
return cmd
|
|
}
|
|
|
|
func pingFunc(*cobra.Command, []string) error {
|
|
lvl, err := luxlog.ToLevel(logLevel)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
lcfg := luxlog.Config{
|
|
DisplayLevel: lvl,
|
|
LogLevel: luxlog.Off,
|
|
}
|
|
logFactory := luxlog.NewFactory(lcfg)
|
|
log, err := logFactory.Make(constants.LogNameControl)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cli, err := client.New(client.Config{
|
|
Endpoint: endpoint,
|
|
DialTimeout: dialTimeout,
|
|
}, log)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer cli.Close()
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), requestTimeout)
|
|
resp, err := cli.Ping(ctx)
|
|
cancel()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
logString := "ping response: " + luxlog.Green.Wrap("%s")
|
|
ux.Print(log, logString, resp)
|
|
return nil
|
|
}
|