cmdutil

package
v0.1.1-beta Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 7, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package cmdutil provides shared helpers for CLI command implementations.

Index

Constants

View Source
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

func AccountIDFlag(cmd *cobra.Command) string

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

func BuildClient(apiBaseURL, accountIDOverride string) (*api.Client, string, error)

BuildClient returns an authenticated JSON API client.

func BuildXMLClient

func BuildXMLClient(apiBaseURL, accountIDOverride string) (*api.Client, string, error)

BuildXMLClient returns an authenticated XML-mode client for the Dashboard API.

func DashboardClient

func DashboardClient(accountIDOverride string) (*api.Client, string, error)

DashboardClient returns an XML-mode client for the Bandwidth Dashboard API v2.

func ExitCodeForError

func ExitCodeForError(err error) int

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 IsInteractive

func IsInteractive() bool

IsInteractive reports whether stdin is a terminal.

func MessagingClient

func MessagingClient(accountIDOverride string) (*api.Client, string, error)

MessagingClient returns a client for the Bandwidth Messaging API v2.

func NewFeatureLimit

func NewFeatureLimit(msg string, cause error) error

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

func NormalizeNumber(number string) string

NormalizeNumber ensures a phone number has the + prefix for E.164 format.

func OutputFlags

func OutputFlags(cmd *cobra.Command) (format string, plain bool)

OutputFlags extracts the common --plain and --format flags from a command's root.

func PlatformClient

func PlatformClient(accountIDOverride string) (*api.Client, string, error)

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

func ReadPassword() ([]byte, error)

ReadPassword reads a password from stdin without echoing.

func ValidateID

func ValidateID(id string) error

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

func VoiceClient(accountIDOverride string) (*api.Client, string, error)

VoiceClient returns a client for the Bandwidth Voice API v2.

func Wrap403

func Wrap403(err error, feature, role string) error

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL