Documentation
¶
Overview ¶
Package cmdutil provides shared helpers for CLI command implementations.
Index ¶
- Constants
- func AccountIDFlag(cmd *cobra.Command) string
- func ActiveBuild() bool
- func BuildClient(apiBaseURL, accountIDOverride string) (*api.Client, string, error)
- func BuildXMLClient(apiBaseURL, accountIDOverride string) (*api.Client, string, error)
- func DashboardClient(accountIDOverride string) (*api.Client, string, error)
- func ExitCodeForError(err error) int
- func IsInteractive() bool
- func MessagingClient(accountIDOverride string) (*api.Client, string, error)
- func NewFeatureLimit(msg string, cause error) error
- func NormalizeNumber(number string) string
- func OutputFlags(cmd *cobra.Command) (format string, plain bool)
- func PlatformClient(accountIDOverride string) (*api.Client, string, error)
- func Poll(cfg PollConfig) (interface{}, error)
- func ReadPassword() ([]byte, error)
- func ValidateID(id string) error
- func VoiceClient(accountIDOverride string) (*api.Client, string, error)
- func Wrap403(err error, feature, role string) error
- type FeatureLimitError
- type NumberType
- type PollConfig
Constants ¶
const ( ExitOK = 0 ExitGeneral = 1 ExitAuth = 2 ExitNotFound = 3 ExitConflict = 4 ExitTimeout = 5 ExitFlagError = 6 ExitRateLimit = 7 )
Exit code constants for the bw CLI.
Variables ¶
This section is empty.
Functions ¶
func AccountIDFlag ¶
AccountIDFlag extracts the --account-id override from a command's root.
func ActiveBuild ¶
func ActiveBuild() bool
ActiveBuild reports whether the active profile is a Bandwidth Build account. Returns false on any config-load failure — best-effort, used only to shape error messages.
func BuildClient ¶
BuildClient returns an authenticated JSON API client.
func BuildXMLClient ¶
BuildXMLClient returns an authenticated XML-mode client for the Dashboard API.
func DashboardClient ¶
DashboardClient returns an XML-mode client for the Bandwidth Dashboard API v2.
func ExitCodeForError ¶
ExitCodeForError maps an error to the appropriate exit code. FeatureLimitError takes precedence over the raw API status code so a 403 caused by a plan/role limit maps to ExitConflict (4) rather than ExitAuth (2) — agents can then distinguish "stop, escalate" from "re-auth or retry." All other errors fall back to status-code mapping, then ExitGeneral.
func MessagingClient ¶
MessagingClient returns a client for the Bandwidth Messaging API v2.
func NewFeatureLimit ¶
NewFeatureLimit wraps a richer, endpoint-specific message in the typed error. Use this from existing wrappers that already produce tailored guidance for a 403 (e.g. tendlc, tfv, shortcode) so the exit code becomes 4 without changing the displayed text.
func NormalizeNumber ¶
NormalizeNumber ensures a phone number has the + prefix for E.164 format.
func OutputFlags ¶
OutputFlags extracts the common --plain and --format flags from a command's root.
func PlatformClient ¶
PlatformClient creates a JSON API client for Universal Platform v2 endpoints (e.g. VCP).
func Poll ¶
func Poll(cfg PollConfig) (interface{}, error)
Poll runs cfg.Check repeatedly at cfg.Interval until it returns done=true or cfg.Timeout is exceeded. On success it returns the result from Check. On timeout it returns ErrPollTimeout.
func ReadPassword ¶
ReadPassword reads a password from stdin without echoing.
func ValidateID ¶
ValidateID checks that a user-supplied ID does not contain characters that could inject path segments or query parameters into a URL. The forbidden set covers the most common path-traversal and query-injection characters: slash, question mark, ampersand, hash, percent, and any ASCII whitespace.
func VoiceClient ¶
VoiceClient returns a client for the Bandwidth Voice API v2.
func Wrap403 ¶
Wrap403 inspects err and, when it is a 403, returns a FeatureLimitError shaped to the active profile.
On Bandwidth Build accounts, the message points at the plan limit and the upgrade path. On other accounts, it tells the user which role to request from their Bandwidth account manager.
`feature` is a human noun phrase describing what the user was trying to do (e.g. "VCPs", "phone number search"). `role` is the role to suggest for non-Build users; pass "" if unknown.
Non-403 errors pass through as `<feature>: <err>` so they retain their original status code for ExitCodeForError to interpret.
Types ¶
type FeatureLimitError ¶
type FeatureLimitError struct {
// contains filtered or unexported fields
}
FeatureLimitError indicates that a command failed because the active account or credential lacks the role/feature needed to complete it. It is mapped to ExitConflict (4) by ExitCodeForError, so an agent can branch on "stop and tell the user" without conflating these failures with true auth errors (exit 2).
Wraps the underlying error so errors.As(err, &apiErr) still finds the original *api.APIError when callers want the raw status code or body.
func (*FeatureLimitError) Error ¶
func (e *FeatureLimitError) Error() string
func (*FeatureLimitError) Unwrap ¶
func (e *FeatureLimitError) Unwrap() error
type NumberType ¶
type NumberType int
NumberType represents the type of a phone number for messaging purposes.
const ( NumberTypeUnknown NumberType = iota NumberType10DLC // Local 10-digit US/CA number NumberTypeTollFree // US/CA toll-free (800, 888, 877, 866, 855, 844, 833) NumberTypeShortCode // 5-6 digit short code )
func ClassifyNumber ¶
func ClassifyNumber(number string) NumberType
ClassifyNumber determines the number type from an E.164-formatted phone number.
func (NumberType) String ¶
func (t NumberType) String() string
type PollConfig ¶
type PollConfig struct {
Interval time.Duration
Timeout time.Duration
// Check performs one poll attempt. It should return done=true when the
// desired condition is met, along with the final result. Return an error
// only for hard failures (not for "not ready yet").
Check func() (done bool, result interface{}, err error)
}
PollConfig configures a polling loop.