Files
reg/cmd/ping.go
2026-11-05 06:40:24 +03:00

42 lines
1.1 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package cmd
import (
"context"
"flag"
"io"
"time"
reg "reg/internal/reg"
)
// RunPing реализует команду `reg ping [-v]`.
// Проверяет доступность реестра (GET /v2/) и выводит API-версию.
// С -v добавляет время ответа.
func RunPing(ctx context.Context, client *reg.RegistryClient, args []string) int {
cmdFlags := flag.NewFlagSet("ping", flag.ContinueOnError)
cmdFlags.SetOutput(io.Discard)
verboseFlag := cmdFlags.Bool("v", false, "print response time")
cmdFlags.Usage = func() {}
if code, ok := parseFlags(cmdFlags, args); !ok {
return code
}
start := time.Now()
apiVersion, err := client.Ping(ctx)
elapsed := time.Since(start)
if err != nil {
reg.PrintError(err.Error())
return 1
}
out := map[string]interface{}{
"ok": true,
"apiVersion": apiVersion,
}
if *verboseFlag {
out["duration_ms"] = float64(elapsed.Microseconds()) / 1000.0
}
reg.PrintJSON(out)
return 0
}