Documentation
¶
Overview ¶
Package htcli is the machinery the generated commands and the hand-written ones share: the operation descriptor, the flag surface built from it, the request runner on top of the Go SDK, and the exit codes.
Index ¶
- Constants
- func AddGroups(root *cobra.Command, opts *Options)
- func BuildBody(jsonSource string, sets []string, stdin io.Reader) ([]byte, error)
- func Count() int
- func ExitCode(err error) int
- func GroupCommand(group Group, opts *Options) *cobra.Command
- func OperationCommand(op Operation, opts *Options) *cobra.Command
- func PrintError(w io.Writer, err error, format output.Format)
- func ReadJSONSource(source string, stdin io.Reader) ([]byte, error)
- func Register(op Operation)
- func RegisterGroup(g Group)
- func Usagef(format string, args ...any) error
- type Answer
- type Arg
- type Group
- type Operation
- type Options
- func (o *Options) Bind(fs *pflag.FlagSet)
- func (o *Options) Client() (*hosttracker.Client, error)
- func (o *Options) Do(ctx context.Context, req Request) (*Answer, error)
- func (o *Options) Emit(answer *Answer) error
- func (o *Options) File() *config.File
- func (o *Options) Format() output.Format
- func (o *Options) Printer() output.Printer
- func (o *Options) ProfileName() string
- func (o *Options) Resolve() error
- func (o *Options) ResolvedBaseURL() string
- func (o *Options) ResolvedProfile() config.Profile
- func (o *Options) ResolvedToken() string
- func (o *Options) Streams()
- type Param
- type Request
- type UsageError
Constants ¶
const ( FlagJSON = "json" FlagSet = "set" FlagQuery = "query" FlagQueryFile = "query-file" FlagIdempotencyKey = "idempotency-key" )
Flag names the command layer owns. A generated parameter that would collide with one of these is renamed by the generator, so the two sets never overlap.
const ( // ExitOK is a command that did what it was asked. ExitOK = 0 // ExitError is a failure with no more specific code. ExitError = 1 // ExitUsage is a malformed command line: an unknown flag, a missing // argument, a bad value. ExitUsage = 2 // ExitAuth is a missing, rejected or under-scoped credential. ExitAuth = 3 // ExitNotFound is an address that names nothing. ExitNotFound = 4 // ExitInvalid is a request the API refused on its merits: a // validation problem, a conflict, a precondition. ExitInvalid = 5 // ExitRateLimit is a throttle or an exhausted quota. ExitRateLimit = 6 // ExitNetwork is a failure to reach the API, or a fault on its side. ExitNetwork = 7 )
Exit codes. A command that succeeded exits 0; everything else picks the code that says what kind of failure it was, so a script can branch without parsing the message.
const ( EnvToken = "HT_TOKEN" EnvBaseURL = "HT_BASE_URL" EnvProfile = "HT_PROFILE" EnvOutput = "HT_OUTPUT" )
Environment variables the global options fall back to.
Variables ¶
This section is empty.
Functions ¶
func AddGroups ¶
AddGroups builds a command for every registered group and operation and hangs them off root.
func BuildBody ¶
BuildBody assembles a request body from --json and --set.
--json supplies the document; each --set then writes one value into it at a dotted path, so a small change needs no file:
--set name=api --set settings.interval=5 --set tags.0=prod
A value is read as JSON when it parses as one (a number, true, false, null, an object, an array) and as a plain string otherwise. Wrapping it in quotes forces the string reading: --set name='"5"'.
func GroupCommand ¶
GroupCommand builds one group command with its operations under it.
func OperationCommand ¶
OperationCommand builds the command for one operation: its positional arguments from the path, its flags from the parameters and the body.
func PrintError ¶
PrintError writes a failure to w. An API failure is reported as the RFC 9457 problem it is: the machine code, the human detail, the offending members, and the request id to quote in a support request. Under --output json the whole problem document is printed instead, so a script can read it.
func ReadJSONSource ¶
ReadJSONSource reads one of the three spellings a JSON argument takes:
'{"name":"api"}' inline
@body.json a file
- standard input
func RegisterGroup ¶
func RegisterGroup(g Group)
RegisterGroup declares a command group. The generated files call it from their init functions.
Types ¶
type Arg ¶
type Arg struct {
// Name is the wire name, e.g. "monitorId".
Name string
// Help is what the argument identifies.
Help string
}
Arg is one path parameter, taken as a positional argument.
type Group ¶
type Group struct {
// Name is the group command, e.g. "status-pages".
Name string
// Tag is the tag it was built from, e.g. "StatusPages".
Tag string
// Short is the one-line group help.
Short string
// Aliases are alternative spellings of the group name.
Aliases []string
}
Group is a command group, one per tag of the document.
type Operation ¶
type Operation struct {
// Group is the command group, e.g. "monitors".
Group string
// Name is the command name inside the group, e.g. "bulk-create".
Name string
// ID is the operationId, so a command can be traced to the document.
ID string
// Method and Path address the operation.
Method string
Path string
// Summary is the one-line help.
Summary string
// Description is the long help.
Description string
// Args are the path parameters, in path order.
Args []Arg
// Params are the query and header parameters.
Params []Param
// Body reports that the operation takes a JSON request body.
Body bool
// BodyRequired reports that the body is not optional.
BodyRequired bool
// QueryPath is the POST <path>/q body-query twin, folded into this
// command as --query and --query-file. Empty when there is none.
QueryPath string
// Paged reports a collection that --all may walk to exhaustion.
Paged bool
// Binary reports an answer that is not JSON (a report download).
Binary bool
// Deprecated carries the replacement when the operation is going away.
Deprecated string
}
Operation is one API operation, as a command.
func OperationsOf ¶
OperationsOf lists a group's operations in alphabetical order.
type Options ¶
type Options struct {
Profile string
Token string
BaseURL string
OutputFmt string
Timeout time.Duration
NoRetry bool
Verbose bool
All bool
// Streams. Tests replace them; main leaves them at the process ones.
Out io.Writer
Err io.Writer
In io.Reader
// UserAgent is appended to the SDK's own product token.
UserAgent string
// Started reports that a command body was reached. A failure raised
// before that came from the command line, not from the API.
Started bool
// contains filtered or unexported fields
}
Options is the global flag set, plus everything a command needs to run: the resolved profile, the streams, and the SDK client built from them.
func (*Options) Client ¶
func (o *Options) Client() (*hosttracker.Client, error)
Client builds the SDK client the command runs on, once per command. Everything the CLI does on the wire goes through it, so auth, retries, idempotency keys and error mapping are the SDK's, not the CLI's.
func (*Options) Do ¶
Do sends one request through the SDK. Everything on the wire, the bearer token, the idempotency key, the retry ladder and the mapping of a >= 400 answer onto *hosttracker.Error, is the SDK's doing.
func (*Options) Emit ¶
Emit prints one answer: the JSON document through the chosen format, a binary payload straight through, an empty answer not at all.
func (*Options) ProfileName ¶
ProfileName is the profile the command is running under.
func (*Options) Resolve ¶
Resolve loads the configuration file and settles every setting, in the order flag, environment, profile, default. It runs once per command.
func (*Options) ResolvedBaseURL ¶
ResolvedBaseURL is the API root in force.
func (*Options) ResolvedProfile ¶
ResolvedProfile is the profile's stored settings.
func (*Options) ResolvedToken ¶
ResolvedToken is the credential in force, from --token, the environment or the profile.
type Param ¶
type Param struct {
// Name is the wire name, e.g. "monitorId".
Name string
// Flag is the command-line spelling, e.g. "monitor-id".
Flag string
// In is "query" or "header".
In string
// Type is "string", "integer", "number", "boolean" or "array".
Type string
// Items is the element type when Type is "array".
Items string
// Enum lists the accepted values, when the parameter has a closed set.
Enum []string
// Explode reports the array serialisation the document asked for:
// true repeats the key once per element, false joins the elements
// with commas into a single key.
Explode bool
// Help is the one-line flag description.
Help string
// Required reports a parameter the API refuses the call without.
Required bool
}
Param is one query or header parameter of an operation.
type UsageError ¶
type UsageError struct {
Err error
}
UsageError marks a failure caused by the command line itself, so it exits 2 and the usage text is worth showing.