diff --git a/cmd/onecli/version.go b/cmd/onecli/version.go index 8db0ef1..834a111 100644 --- a/cmd/onecli/version.go +++ b/cmd/onecli/version.go @@ -1,17 +1,42 @@ package main -import "github.com/onecli/onecli-cli/pkg/output" +import ( + "context" + "time" + + "github.com/onecli/onecli-cli/pkg/output" +) // VersionCmd prints version information as JSON. type VersionCmd struct{} // VersionResponse is the JSON output of the version command. type VersionResponse struct { - Version string `json:"version"` + Version string `json:"version"` + ServerVersion string `json:"server_version"` + ServerStatus string `json:"server_status"` } func (cmd *VersionCmd) Run(out *output.Writer) error { - return out.Write(VersionResponse{ - Version: version, - }) + resp := VersionResponse{Version: version} + + client, err := newClient() + if err != nil { + resp.ServerVersion = "unknown" + resp.ServerStatus = "not_configured" + return out.Write(resp) + } + + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second) + defer cancel() + health, err := client.GetHealth(ctx) + if err != nil { + resp.ServerVersion = "unknown" + resp.ServerStatus = "unreachable" + return out.Write(resp) + } + + resp.ServerVersion = health.Version + resp.ServerStatus = health.Status + return out.Write(resp) } diff --git a/internal/api/health.go b/internal/api/health.go new file mode 100644 index 0000000..8abfa40 --- /dev/null +++ b/internal/api/health.go @@ -0,0 +1,18 @@ +package api + +import "context" + +// HealthResponse is the response from /api/health. +type HealthResponse struct { + Status string `json:"status"` + Version string `json:"version"` +} + +// GetHealth calls the /api/health endpoint. +func (c *Client) GetHealth(ctx context.Context) (*HealthResponse, error) { + var resp HealthResponse + if err := c.do(ctx, "GET", "/api/health", nil, &resp); err != nil { + return nil, err + } + return &resp, nil +} diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 7834c5f..2296e64 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -29,6 +29,8 @@ func TestEnvUnknownValueDefaultsToProduction(t *testing.T) { } func TestAPIHostDefault(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + t.Setenv("ONECLI_ENV", "") t.Setenv("ONECLI_API_HOST", "") // Env var not set and no config file → default got := APIHost()