Documentation
¶
Index ¶
- Constants
- Variables
- func ApplyLimitAndPrint[T any](items []T, limit int, outputFormat string, noColor bool, emptyMessage string, ...) error
- func BuyConfirmPrompt(resourceType string, details []BuyConfirmDetail, noColor bool) bool
- func ConfirmPrompt(question string, noColor bool) bool
- func ContextFromCmd(cmd *cobra.Command) (context.Context, context.CancelFunc)
- func ContextFromCmdWithDefault(cmd *cobra.Command, defaultTimeout time.Duration) (context.Context, context.CancelFunc)
- func Filter[T any](items []T, predicate func(T) bool) []T
- func GetBuyConfirmPrompt() func(string, []BuyConfirmDetail, bool) bool
- func GetConfirmPrompt() func(string, bool) bool
- func GetCurrentEnv() string
- func GetPrompt() func(string, bool) (string, error)
- func GetResourcePrompt() func(string, string, bool) (string, error)
- func GetResourceTagsPrompt() func(bool) (map[string]string, error)
- func GetUpdateResourceTagsPrompt() func(map[string]string, bool) (map[string]string, error)
- func ListResourceTags(resourceType, uid string, noColor bool, outputFormat string, ...) error
- func ParseResourceTagsInput(cmd *cobra.Command) (map[string]string, error)
- func Prompt(msg string, noColor bool) (string, error)
- func ResolveInput[T any](cfg InputConfig[T]) (T, error)
- func ResourcePrompt(resourceType string, msg string, noColor bool) (string, error)
- func ResourceTagsPrompt(noColor bool) (map[string]string, error)
- func RetryWithBackoff(ctx context.Context, opts RetryOpts, fn func(ctx context.Context) error) error
- func SetBuyConfirmPrompt(fn func(string, []BuyConfirmDetail, bool) bool)
- func SetConfirmPrompt(fn func(string, bool) bool)
- func SetPrompt(fn func(string, bool) (string, error))
- func SetResourcePrompt(fn func(string, string, bool) (string, error))
- func SetResourceTagsPrompt(fn func(bool) (map[string]string, error))
- func SetUpdateResourceTagsPrompt(fn func(map[string]string, bool) (map[string]string, error))
- func ShouldDisableColors() bool
- func UpdateResourceTags(opts UpdateTagsOptions) error
- func UpdateResourceTagsPrompt(existingTags map[string]string, noColor bool) (map[string]string, error)
- func WatchLoop(ctx context.Context, cfg WatchConfig, ...) error
- func WithIdempotentRetry(ctx context.Context, fn func(ctx context.Context) error) error
- func WithRetry(ctx context.Context, fn func(ctx context.Context) error) error
- func WrapAPIError(err error, resourceType, resourceUID string) error
- func WrapColorAwareRunE(fn func(cmd *cobra.Command, args []string, noColor bool) error) func(cmd *cobra.Command, args []string) error
- func WrapOutputFormatRunE(fn func(cmd *cobra.Command, args []string, noColor bool, format string) error) func(cmd *cobra.Command, args []string) error
- func WrapRunE(runE func(cmd *cobra.Command, args []string) error) func(cmd *cobra.Command, args []string) error
- type BuyConfirmDetail
- type InputConfig
- type RetryOpts
- type TagListerFunc
- type TagUpdaterFunc
- type UpdateTagsOptions
- type WatchConfig
Constants ¶
const ( // DefaultProvisionTimeout is the default wait time for provisioning operations // (buy, update commands that wait for the resource to become ready). DefaultProvisionTimeout = 10 * time.Minute // DefaultMutationTimeout is the default context timeout for buy/update/delete // commands that may take longer than the standard request timeout. DefaultMutationTimeout = 15 * time.Minute )
const ( FormatTable = "table" FormatJSON = "json" FormatCSV = "csv" FormatXML = "xml" // StatusDecommissioning is used for filtering inactive resources. The SDK // exports STATUS_CANCELLED and STATUS_DECOMMISSIONED but not this one. StatusDecommissioning = "DECOMMISSIONING" )
Variables ¶
var ( // Env is the target environment (prod, dev, staging). Set once via flag // binding before command execution; read during login. Not protected by // a mutex because cobra flag parsing and command execution are sequential // on the main goroutine. Env string // ProfileOverride is the config profile name. Same set-once semantics as Env. ProfileOverride string // NoRetry disables automatic retry on transient API failures. Set via --no-retry flag. NoRetry bool // MaxRetries overrides the default retry count. Set via --max-retries flag. MaxRetries int ValidFormats = []string{FormatTable, FormatJSON, FormatCSV, FormatXML} )
Functions ¶
func ApplyLimitAndPrint ¶ added in v0.5.9
func ApplyLimitAndPrint[T any]( items []T, limit int, outputFormat string, noColor bool, emptyMessage string, printFunc func([]T, string, bool) error, ) error
ApplyLimitAndPrint handles the common post-filter pipeline for all List commands: validate and apply --limit, check for empty results, and print.
Returns nil (no error) when items is empty. If outputFormat is table, it also prints an informational message.
func BuyConfirmPrompt ¶ added in v0.5.4
func BuyConfirmPrompt(resourceType string, details []BuyConfirmDetail, noColor bool) bool
BuyConfirmPrompt displays a purchase summary and asks for confirmation.
func ConfirmPrompt ¶
ConfirmPrompt asks the user a yes/no question.
func ContextFromCmd ¶ added in v0.5.8
ContextFromCmd creates a context with timeout from the command's --timeout flag. If no timeout flag is set or the duration is zero or negative, defaults to 90 seconds.
func ContextFromCmdWithDefault ¶ added in v0.5.8
func ContextFromCmdWithDefault(cmd *cobra.Command, defaultTimeout time.Duration) (context.Context, context.CancelFunc)
ContextFromCmdWithDefault creates a context with timeout from the command's --timeout flag, falling back to defaultTimeout when the flag is not set or is zero or negative. Use this for long-running operations (e.g. provisioning) where the operation-appropriate default differs from the global 90-second default.
func Filter ¶ added in v0.5.8
Filter returns a new slice containing only the elements of items for which predicate returns true. Returns nil (not an empty slice) when no elements match, consistent with the existing filter functions across this codebase. Callers need not special-case a nil result: len(nil) == 0 in Go.
func GetBuyConfirmPrompt ¶ added in v0.6.0
func GetBuyConfirmPrompt() func(string, []BuyConfirmDetail, bool) bool
func GetConfirmPrompt ¶ added in v0.6.0
func GetCurrentEnv ¶
func GetCurrentEnv() string
func GetResourcePrompt ¶ added in v0.6.0
func GetResourceTagsPrompt ¶ added in v0.6.0
func GetUpdateResourceTagsPrompt ¶ added in v0.6.0
func ListResourceTags ¶ added in v0.5.1
func ListResourceTags(resourceType, uid string, noColor bool, outputFormat string, listFunc TagListerFunc) error
ListResourceTags handles the common pattern of listing resource tags: setting output format, calling the list function, converting to ResourceTag slice, sorting, and printing output.
func ParseResourceTagsInput ¶ added in v0.5.1
ParseResourceTagsInput reads resource tags from --json or --json-file flags.
func ResolveInput ¶ added in v0.5.9
func ResolveInput[T any](cfg InputConfig[T]) (T, error)
ResolveInput determines which input mode the user chose (JSON, flags, or interactive) and delegates to the appropriate builder function.
Precedence: JSON > flags > interactive > error.
func ResourcePrompt ¶ added in v0.4.0
ResourcePrompt asks the user for resource-specific input.
func ResourceTagsPrompt ¶ added in v0.4.3
ResourceTagsPrompt asks the user to enter resource tags.
func RetryWithBackoff ¶ added in v0.6.0
func RetryWithBackoff(ctx context.Context, opts RetryOpts, fn func(ctx context.Context) error) error
RetryWithBackoff calls fn up to opts.MaxRetries+1 times with exponential backoff and jitter. It respects the Retry-After header from 429 responses and only retries on transient/server errors.
func SetBuyConfirmPrompt ¶ added in v0.6.0
func SetBuyConfirmPrompt(fn func(string, []BuyConfirmDetail, bool) bool)
func SetConfirmPrompt ¶ added in v0.6.0
func SetResourcePrompt ¶ added in v0.6.0
func SetResourceTagsPrompt ¶ added in v0.6.0
func SetUpdateResourceTagsPrompt ¶ added in v0.6.0
func ShouldDisableColors ¶ added in v0.3.2
func ShouldDisableColors() bool
func UpdateResourceTags ¶ added in v0.5.1
func UpdateResourceTags(opts UpdateTagsOptions) error
UpdateResourceTags handles the common pattern of updating resource tags: fetching existing tags, parsing input (interactive/JSON/JSON file), calling the update function, and printing results.
func UpdateResourceTagsPrompt ¶ added in v0.4.3
func UpdateResourceTagsPrompt(existingTags map[string]string, noColor bool) (map[string]string, error)
UpdateResourceTagsPrompt asks the user to update existing resource tags.
func WatchLoop ¶ added in v0.5.8
func WatchLoop(ctx context.Context, cfg WatchConfig, pollFn func(ctx context.Context) (string, error)) error
WatchLoop runs pollFn repeatedly at the configured interval until interrupted. pollFn should fetch and print its output, returning the current status string for transition tracking.
func WithIdempotentRetry ¶ added in v0.6.0
WithIdempotentRetry wraps fn with the default retry policy including network-level error retries. Use this for read-only or idempotent operations where retrying after an ambiguous network failure is safe. If --no-retry was set globally, fn is called exactly once.
func WithRetry ¶ added in v0.6.0
WithRetry wraps fn with the default retry policy, only retrying on server-confirmed errors (HTTP status codes like 429, 502, 503, 504). Network-level errors (connection reset, EOF, timeout) are NOT retried because it is unsafe for non-idempotent operations — the server may have processed the request despite the client-side failure. Use WithIdempotentRetry for read-only or otherwise idempotent operations where retrying network errors is safe. If --no-retry was set globally, fn is called exactly once.
func WrapAPIError ¶ added in v0.5.5
WrapAPIError wraps a megaport SDK error with an actionable message based on the HTTP status code. Falls back to the original error if the type cannot be extracted.
func WrapColorAwareRunE ¶
func WrapColorAwareRunE(fn func(cmd *cobra.Command, args []string, noColor bool) error) func(cmd *cobra.Command, args []string) error
WrapColorAwareRunE combines WrapRunE and WrapCommandFunc to handle both error formatting and passing the noColor flag to command functions.
func WrapOutputFormatRunE ¶
func WrapOutputFormatRunE(fn func(cmd *cobra.Command, args []string, noColor bool, format string) error) func(cmd *cobra.Command, args []string) error
WrapOutputFormatRunE handles both output format and color settings in command functions. This wrapper takes a function that needs both output format and noColor parameters.
Types ¶
type BuyConfirmDetail ¶ added in v0.5.4
BuyConfirmDetail represents a key-value detail line in the purchase summary.
type InputConfig ¶ added in v0.5.9
type InputConfig[T any] struct { // ResourceName is used in the fallback error message (e.g. "port", "MCR"). ResourceName string // Cmd is the cobra command whose flags are inspected. Cmd *cobra.Command // NoColor disables colored output. NoColor bool // FlagsProvided should return true when any resource-specific CLI flags // have been explicitly set by the user. FlagsProvided func() bool // FromJSON builds the request from --json or --json-file input. FromJSON func(jsonStr, jsonFile string) (T, error) // FromFlags builds the request from CLI flags. FromFlags func() (T, error) // FromPrompt builds the request via interactive prompts. // May be nil if the command does not support interactive mode. FromPrompt func() (T, error) }
InputConfig configures how ResolveInput resolves the request object from one of the three input modes (JSON, flags, interactive prompts).
type RetryOpts ¶ added in v0.6.0
type RetryOpts struct {
MaxRetries int
InitialDelay time.Duration
MaxDelay time.Duration
BackoffMultiplier float64
// RetryNetworkErrors enables retrying on ambiguous network failures
// (connection reset, EOF, timeout). Only set this for idempotent operations.
RetryNetworkErrors bool
}
RetryOpts configures the retry behaviour of RetryWithBackoff.
func DefaultRetryOpts ¶ added in v0.6.0
func DefaultRetryOpts() RetryOpts
DefaultRetryOpts returns sensible defaults for API retry behaviour. MaxRetries is taken from the --max-retries flag (default 3, set by cobra).
type TagListerFunc ¶ added in v0.5.1
TagListerFunc is a function that lists resource tags for a given UID.
type TagUpdaterFunc ¶ added in v0.5.1
TagUpdaterFunc is a function that updates resource tags for a given UID.
type UpdateTagsOptions ¶ added in v0.5.1
type UpdateTagsOptions struct {
ResourceType string
UID string
NoColor bool
Cmd *cobra.Command
ListFunc TagListerFunc
UpdateFunc TagUpdaterFunc
ExtraTagFlags bool // When true, also check --tags, --tags-file, --resource-tags flags
}
UpdateTagsOptions contains the options for UpdateResourceTags.