From 8d19fbd54912524f277603632b818473ccb0b68d Mon Sep 17 00:00:00 2001 From: Paul Payne Date: Tue, 14 Oct 2025 18:55:43 +0000 Subject: [PATCH] Fix dashboard token command. --- BUILDING_WILD_CLI.md | 49 ++++++++++++++++++++++++++++++++++++++++++++ cmd/utility.go | 6 +++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/BUILDING_WILD_CLI.md b/BUILDING_WILD_CLI.md index 44f3eb6..5022ecf 100644 --- a/BUILDING_WILD_CLI.md +++ b/BUILDING_WILD_CLI.md @@ -2,3 +2,52 @@ - Go 1.21+ - GNU Make (for build automation) + +## Patterns + +### Instance-scoped Commands + +CLI commands that operate on a specific Wild Cloud instance should follow this pattern: + +```go +// In cmd/utility.go +var dashboardTokenCmd = &cobra.Command{ + Use: "token", + Short: "Get dashboard token", + RunE: func(cmd *cobra.Command, args []string) error { + // 1. Get instance from CLI context + instanceName, err := getInstanceName() + if err != nil { + return err + } + + // 2. Call instance-scoped API endpoint with instance in URL + resp, err := apiClient.Get(fmt.Sprintf("/api/v1/instances/%s/utilities/dashboard/token", instanceName)) + if err != nil { + return err + } + + // 3. Process response + data := resp.GetMap("data") + if data == nil { + return fmt.Errorf("no data in response") + } + + token, ok := data["token"].(string) + if !ok { + return fmt.Errorf("no token in response") + } + + // 4. Display result + fmt.Println(token) + return nil + }, +} +``` + +#### Key Principles + +1. **Get instance from context**: Use `getInstanceName()` to get the current instance from CLI context +2. **Instance in URL path**: Include the instance name in the API endpoint URL path +3. **Stateless API calls**: Don't rely on server-side session state - pass instance explicitly +4. **Handle errors gracefully**: Return clear error messages if instance is not set or API call fails diff --git a/cmd/utility.go b/cmd/utility.go index d81862c..e07aad5 100644 --- a/cmd/utility.go +++ b/cmd/utility.go @@ -38,7 +38,11 @@ var dashboardTokenCmd = &cobra.Command{ Use: "token", Short: "Get dashboard token", RunE: func(cmd *cobra.Command, args []string) error { - resp, err := apiClient.Get("/api/v1/utilities/dashboard/token") + instanceName, err := getInstanceName() + if err != nil { + return err + } + resp, err := apiClient.Get(fmt.Sprintf("/api/v1/instances/%s/utilities/dashboard/token", instanceName)) if err != nil { return err }