Documentation
¶
Overview ¶
Package cmd builds the freshbooks CLI's cobra command tree: the data-driven registry of 169 resource commands (registry.go, commands_*.go), the auth/config/api non-registry commands, and the global flag plumbing (scope resolution, output formatting, dry-run, exit codes) every command shares.
Index ¶
- Variables
- func BuildTree(root *cobra.Command, state *runtimeState)
- func NewRootCmd(version string) *cobra.Command
- func Run(args []string, stdin io.Reader, stdout, stderr io.Writer, version string) int
- type Class
- type Command
- type Invocation
- func (inv *Invocation) All() bool
- func (inv *Invocation) DecodeBody(out any) error
- func (inv *Invocation) DecodeBodyOptional(out any) (bool, error)
- func (inv *Invocation) Extra(i int) string
- func (inv *Invocation) Include() []string
- func (inv *Invocation) IncludeOpt() []freshbooks.RequestOption
- func (inv *Invocation) IntID() int64
- func (inv *Invocation) OpenUpload() (io.ReadCloser, string, error)
- func (inv *Invocation) Page() int
- func (inv *Invocation) PerPage() int
- func (inv *Invocation) ReqOpts() []freshbooks.RequestOption
- func (inv *Invocation) RequiredString(name string) string
- func (inv *Invocation) Search() freshbooks.Search
- func (inv *Invocation) SortOpt() []freshbooks.RequestOption
- func (inv *Invocation) StrID() string
- type RunFunc
- type Scope
- type ScopeFamily
Constants ¶
This section is empty.
Variables ¶
var All = buildRegistry()
All is the full command registry: one Command per freshbooks client-library method the phase 4 work order covers (169 entries, docs/phases/4/commands.md). Declared as a function so it evaluates after every commandsXxx slice below has initialized, regardless of Go's package-level var initialization order across files.
Functions ¶
func BuildTree ¶
BuildTree adds every registry command to root, grouped under one cobra parent command per resource (e.g. "clients"), bound to state.
func NewRootCmd ¶
NewRootCmd builds the freshbooks root command: every global flag, the non-registry commands (auth, config, api, version, plus any build-tag gated extras registered in extraCommands -- the hidden docs command under -tags docsgen), and the full 169-command registry tree. version is embedded so `freshbooks version` reports the binary that was actually built.
func Run ¶
Run builds the root command, executes it against args, and returns the process exit code main should pass to os.Exit. It is the seam main.go's single os.Exit(cmd.Run(...)) statement calls into, kept separate so it is testable without exercising os.Exit itself. stdin feeds --file - bodies and the `auth login --no-browser` paste prompt.
Types ¶
type Class ¶
type Class string
Class is a registry command's annotation class, matching the RO/D/I/W columns in docs/phases/4/commands.md and mcp/internal/tools/registry.go.
type Command ¶
type Command struct {
// Group is the cobra parent command name, e.g. "clients". Verb is the
// leaf command name, e.g. "list". Together they are the
// "freshbooks <group> <verb>" path docs/phases/4/commands.md lists.
Group, Verb string
// Short is the one-line help text.
Short string
// Service and Method are the *freshbooks.Client field name and its
// exported method this command wraps, e.g. "Clients", "List".
Service, Method string
// Keys are the lib method's // inventory: keys, stacked exactly as
// freshbooks carries them.
Keys []string
// Class is the RO/D/I/W annotation.
Class Class
// Scope says which scope flag(s) this command resolves.
Scope ScopeFamily
// HasID marks a positional <id> argument. IDKind is "int64" (the
// default, parsed and validated) or "string" (passed through as-is).
// IDName is the positional's display name in Use and error messages;
// "id" when empty.
HasID bool
IDKind string
IDName string
// ExtraPositional names positional arguments beyond <id>, in order,
// e.g. {"query"} for time-entries search or {"download-token"} for
// reports download-invoice-details-csv.
ExtraPositional []string
// List registers --page/--per-page (unless NoPaging) and --search.
// HasInclude additionally registers --include; HasAll additionally
// registers --all (rejecting --page/--per-page alongside it).
// NoPaging is set for the handful of List methods whose lib options
// struct carries no Page/PerPage field at all (only Search) --
// registering the flags would silently do nothing.
List bool
HasInclude bool
HasAll bool
NoPaging bool
// HasSort registers --sort field[:asc|desc] on a List command whose
// lib method takes extra ...RequestOption (spec section 7's command
// surface names this flag explicitly, not as a heuristic -- 21 of the
// 24 List methods qualify; the other three take no options at all).
// Invocation.SortOpt() renders it as freshbooks.Sort(field, dir),
// appended to the same extra variadic every List/All call already
// takes.
HasSort bool
// Body registers -f/--file for a JSON request body (a path, or - for
// stdin), decoded by the Run closure via Invocation.DecodeBody.
// BodyOptional (the 13 report commands with a filter-options struct
// FreshBooks itself defaults sensibly) lets the command run with no
// --file at all; the Run closure reads it back via
// Invocation.DecodeBodyOptional instead.
Body bool
BodyOptional bool
// Upload registers --file for a local file to upload, opened by the
// Run closure via Invocation.OpenUpload.
Upload bool
// Binary marks a command whose result is raw bytes, written to
// -o/--output (a local -o that shadows the global output-format
// flag on this one command, since a binary result has no format to
// select) instead of going through the normal formatter.
Binary bool
// ExtraFlags registers any command-specific flags beyond the above
// (--verifier, --message, --rate, --project-id, --payment-method),
// read back by the Run closure via Invocation.Flags.
ExtraFlags func(fs *pflag.FlagSet)
// RequiredFlags names ExtraFlags-registered string flags that must be
// non-empty, checked in execute() before buildClient (F13/review A1):
// a missing --verifier or --message is a usage error (exit 2)
// regardless of whether the machine has any stored credentials at
// all, not an auth error discovered only after a client was built for
// a call that was never going to happen. Each Run closure still reads
// the flag itself (via Invocation.Flags or a RequiredString helper);
// this list only decides how early the "is it there" check runs.
// RequiredInt64Flags is the same idea for an ExtraFlags-registered
// int64 flag that must be non-zero (service-rates update-project-rate's
// --project-id).
RequiredFlags []string
RequiredInt64Flags []string
// Run is the one-line closure that calls the wrapped lib method.
Run RunFunc
}
Command is one registry entry: everything BuildTree needs to generate a cobra command, and everything the parity test needs to check it against the lib and docs/phases/4/commands.md.
type Invocation ¶
type Invocation struct {
// Scope carries the resolved account/business/business-uuid
// identifiers for this call.
Scope Scope
// Flags is this command's own FlagSet, for ExtraFlags-registered
// flags a Run closure reads directly (e.g. cmd.Flags().GetString
// via inv.Flags.GetString("verifier")).
Flags *pflag.FlagSet
// contains filtered or unexported fields
}
Invocation carries one command invocation's parsed request context: the resolved scope, positional arguments, list/body/upload flag values, and the command's own FlagSet for any command-specific flag a Run closure needs (--verifier, --message, --rate, ...). registry.go's Command.execute populates it before calling Run; Run closures never parse cobra flags directly except through the accessors here or via Flags for a command-specific one.
func (*Invocation) DecodeBody ¶
func (inv *Invocation) DecodeBody(out any) error
DecodeBody decodes the -f/--file JSON body into out, rejecting unknown fields. Returns a usageError (exit 2) if no body was supplied or the JSON is malformed or carries a field out does not have -- both are the caller's mistake, never the API's.
func (*Invocation) DecodeBodyOptional ¶
func (inv *Invocation) DecodeBodyOptional(out any) (bool, error)
DecodeBodyOptional is DecodeBody for a command whose -f/--file body is optional (the 13 report commands that take a filter-options struct FreshBooks itself defaults sensibly when omitted): it reports whether a body was supplied, decoding into out only when one was.
func (*Invocation) Extra ¶
func (inv *Invocation) Extra(i int) string
Extra returns the i'th ExtraPositional argument (0-based, after <id> when present), or "" if there is none at that index.
func (*Invocation) Include ¶
func (inv *Invocation) Include() []string
Include returns the --include values, in the order given.
func (*Invocation) IncludeOpt ¶
func (inv *Invocation) IncludeOpt() []freshbooks.RequestOption
IncludeOpt renders --include as a single-element []freshbooks.RequestOption (freshbooks.Include(...)), or nil when --include was not given. Like SortOpt, returning a slice lets every call site append it directly to a variadic opts ...RequestOption argument with `...` regardless of whether it is empty (F9/review B7 -- invoices get/create/update, invoice-profiles get/create, and the other single-resource get commands whose lib method takes opts ...RequestOption).
func (*Invocation) IntID ¶
func (inv *Invocation) IntID() int64
IntID returns the positional <id> argument as an int64. Valid only for a command with HasID and IDKind != "string"; registry.go's Command.execute has already validated and parsed it.
func (*Invocation) OpenUpload ¶
func (inv *Invocation) OpenUpload() (io.ReadCloser, string, error)
OpenUpload opens the --file path for an Upload command, returning the open file and its base name (directory components stripped, matching what the lib's own multipart builder does defensively on its side too). The caller must close the returned reader.
func (*Invocation) Page ¶
func (inv *Invocation) Page() int
Page returns the --page value (0 when unset, which every ListOptions.opts() in the lib already treats as "omit").
func (*Invocation) PerPage ¶
func (inv *Invocation) PerPage() int
PerPage returns the --per-page value (0 when unset, same as Page).
func (*Invocation) ReqOpts ¶
func (inv *Invocation) ReqOpts() []freshbooks.RequestOption
ReqOpts renders Search/Page/PerPage/Sort as a []RequestOption, for the handful of lib methods that take raw variadic RequestOptions instead of a *XListOptions struct (JournalEntries.Details, JournalEntryAccounts.List, TimeEntries.ListWithTotals).
func (*Invocation) RequiredString ¶
func (inv *Invocation) RequiredString(name string) string
RequiredString reads an ExtraFlags-registered string flag. Every command that declares name in its RequiredFlags has already had this checked non-empty by execute() before Run ever ran (F13), so this never itself returns the "is required" error -- it exists so five call sites do not each retype the same GetString call.
func (*Invocation) Search ¶
func (inv *Invocation) Search() freshbooks.Search
Search returns the --search filter as the lib's Search type, nil when no --search flag was given (so a Call never sends an empty search[] query parameter).
func (*Invocation) SortOpt ¶
func (inv *Invocation) SortOpt() []freshbooks.RequestOption
SortOpt renders --sort as a single-element []freshbooks.RequestOption (freshbooks.Sort(field, dir)), or nil when --sort was not given. Returning a slice rather than a (RequestOption, bool) pair lets every call site append it directly to a variadic extra ...RequestOption argument with `...` regardless of whether it is empty.
func (*Invocation) StrID ¶
func (inv *Invocation) StrID() string
StrID returns the positional <id> argument as-is. Valid only for a command with HasID and IDKind == "string".
type RunFunc ¶
type RunFunc func(ctx context.Context, c *freshbooks.Client, inv *Invocation) (any, error)
RunFunc is one command's implementation: given the resolved client and invocation, it calls exactly one freshbooks client-library method and returns its result (or a []byte for a Binary command). RunFunc implementations never format or print the result themselves and never build error messages by interpolating a raw request body -- registry.go and invocation.go's generic plumbing handle output formatting, exit codes, and redaction.
type Scope ¶
type Scope struct {
AccountID freshbooks.AccountID
BusinessID freshbooks.BusinessID
BusinessUUID freshbooks.BusinessUUID
}
Scope carries the resolved account/business identifiers a Run closure calls the lib with.
type ScopeFamily ¶
type ScopeFamily int
ScopeFamily says which scope flag(s) a command's lib method needs. The family comes from the lib method's own signature, never converted (CLAUDE.md's "two ID families" gotcha) -- ScopeAccountAndBusiness exists for the one method (SystemsService.Get) that genuinely needs both at once, not as a shortcut for anything else.
const ( ScopeNone ScopeFamily = iota ScopeAccount ScopeBusiness ScopeBusinessUUID ScopeAccountAndBusiness )
The scope families a Command declares.
Source Files
¶
- api_cmd.go
- auth_cmd.go
- commands_attachments.go
- commands_bill_payments.go
- commands_bill_vendors.go
- commands_bills.go
- commands_callbacks.go
- commands_clients.go
- commands_contacts.go
- commands_credit_notes.go
- commands_estimates.go
- commands_expense_categories.go
- commands_expenses.go
- commands_gateways.go
- commands_identity.go
- commands_images.go
- commands_invoice_profiles.go
- commands_invoices.go
- commands_items.go
- commands_journal.go
- commands_ledger_accounts.go
- commands_other_income.go
- commands_payment_options.go
- commands_payments.go
- commands_projects.go
- commands_reports.go
- commands_retainers.go
- commands_service_rates.go
- commands_services.go
- commands_staff.go
- commands_systems.go
- commands_tasks.go
- commands_taxes.go
- commands_team_members.go
- commands_time_entries.go
- config_cmd.go
- errors.go
- hooks.go
- invocation.go
- registry.go
- root.go
- state.go
- term.go
- version.go