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
19 changes: 17 additions & 2 deletions internal/register/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/equinix/cli/internal/parser"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"gopkg.in/yaml.v3"
)

// APIClientInterface represents any API client that can be used for command registration
Expand Down Expand Up @@ -479,12 +480,26 @@ func executeMethod(cmd *cobra.Command, serviceName string, _ reflect.Method, bui

// Format and display the result (first return value)
if !result[0].IsNil() {
var pretty []byte
response := result[0].Interface()
jsonBytes, err := json.MarshalIndent(response, "", " ")

// TODO: move formatting elsewhere, possibly replicating
// output package from metal CLI
format, err := cmd.InheritedFlags().GetString("format")
if err != nil {
return fmt.Errorf("failed to get format flag: %w", err)
}

switch format {
case "json":
pretty, err = json.MarshalIndent(response, "", " ")
case "yaml":
pretty, err = yaml.Marshal(response)
}
if err != nil {
return fmt.Errorf("failed to format response: %w", err)
}
fmt.Println(string(jsonBytes))
fmt.Println(string(pretty))
} else {
// Some endpoints might return no content (e.g., DELETE operations)
if httpResp != nil && httpResp.StatusCode >= 200 && httpResp.StatusCode < 300 {
Expand Down
3 changes: 3 additions & 0 deletions templates/cmd/service.go.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ func init() {
// Add common debug flag that will be inherited by all subcommands for {{SERVICE}}
{{SERVICE}}Cmd.PersistentFlags().BoolVar(&{{SERVICE}}Debug, "debug", false, "Enable debug logging for HTTP requests")

// Add common format flag that will be inherited by all subcommands for {{SERVICE}}
{{SERVICE}}Cmd.PersistentFlags().StringP("format", "f", "json", "Format to use for output (json or yaml)")

// Load SDK descriptions for better command documentation
if err := register.LoadDescriptions({{SERVICE}}Descriptions); err != nil {
fmt.Fprintf(os.Stderr, "Warning: Could not load {{SERVICE}} descriptions: %v\n", err)
Expand Down