Skip to content
Merged
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
35 changes: 30 additions & 5 deletions cmd/onecli/version.go
Original file line number Diff line number Diff line change
@@ -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)
}
18 changes: 18 additions & 0 deletions internal/api/health.go
Original file line number Diff line number Diff line change
@@ -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
}
2 changes: 2 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading