Documentation
¶
Overview ¶
Package cmdutil provides utilities for command implementation
Index ¶
- Constants
- Variables
- func AddFormatFlag(cmd *cobra.Command, target *string)
- func AddJSONFlag(cmd *cobra.Command, target *bool)
- func AddTemplateFlag(cmd *cobra.Command, target *string)
- func AddTimeFormatFlag(cmd *cobra.Command, target *string)
- func AuthenticatedClient(httpClient *http.Client) (*api.Client, error)
- func AuthenticatedClientFromFactory(httpClient func() (*http.Client, error)) (*api.Client, error)
- func ConfirmOrAbort(opts ConfirmOptions) error
- func ConfirmTokenDisclosure(ioStreams *iostreams.IOStreams, hostname string) error
- func DecodeUserText(content []byte) string
- func DefaultToken() string
- func ExitCode(err error) int
- func FormatAPIID(id interface{}) string
- func NewAuthError(message string) error
- func NewCLIError(code int, message string, cause error) error
- func NewConflictError(message string) error
- func NewNotFoundError(message string, cause error) error
- func NewUsageError(message string) error
- func NormalizeLabels(labels []string) []string
- func ParseRepo(repo string) (string, string, error)
- func ReadBody(body, bodyFile string, stdin io.Reader) (string, error)
- func ReadText(r io.Reader) (string, error)
- func ReadTextFile(path string) (string, error)
- func ReadTextFromFlag(r io.Reader, flagName string) (string, error)
- func ResolvePRURL(htmlURL, owner, repo string, number int) string
- func ResolveRepo(repo string, baseRepo func() (string, error)) (string, error)
- func ScanContentForSecrets(content string) error
- func SetFlagEnum(cmd *cobra.Command, name string, values ...string) error
- func SetFlagEnumOrWarn(cmd *cobra.Command, name string, values ...string)
- func SetTopicAnnotation(cmd *cobra.Command, topic string)
- func WrapNotFound(err error, format string, args ...interface{}) error
- func WriteJSON(w io.Writer, value interface{}) error
- type CLIError
- type ConfirmOptions
- type Factory
Constants ¶
const ( ExitSuccess = 0 ExitError = 1 ExitUsage = 2 ExitNotFound = 3 ExitAuth = 4 ExitConflict = 5 )
const FlagEnumAnnotation = "gc.enum"
const TopicAnnotation = "gc.topic"
Variables ¶
ErrLossyPowerShellStdin is returned when Windows PowerShell appears to have replaced non-ASCII stdin text with question marks before the CLI could read it.
ErrSecretDetected is returned when ScanContentForSecrets detects a secret.
var StandardTopics = []string{
"auth",
"commits",
"issues",
"labels",
"milestones",
"precommit",
"pull-requests",
"releases",
"repo",
}
StandardTopics is the list of standard topic categories.
Functions ¶
func AddFormatFlag ¶ added in v0.3.10
AddFormatFlag adds a consistent format flag to a command.
func AddJSONFlag ¶ added in v0.3.7
AddJSONFlag adds a consistent JSON output flag to a command.
func AddTemplateFlag ¶ added in v0.3.10
AddTemplateFlag adds a consistent template flag to a command.
func AddTimeFormatFlag ¶ added in v0.3.10
AddTimeFormatFlag adds a consistent time-format flag to a command.
func AuthenticatedClient ¶ added in v0.4.0
AuthenticatedClient creates an API client using the configured default host and active token. Environment tokens keep their existing priority.
func AuthenticatedClientFromFactory ¶ added in v0.4.0
AuthenticatedClientFromFactory creates an authenticated API client from the command factory dependencies.
func ConfirmOrAbort ¶ added in v0.3.7
func ConfirmOrAbort(opts ConfirmOptions) error
ConfirmOrAbort enforces a shared confirmation flow.
func ConfirmTokenDisclosure ¶ added in v0.7.0
ConfirmTokenDisclosure gates commands that print a full authentication token.
func DecodeUserText ¶ added in v0.5.8
DecodeUserText decodes text accepted from user-facing files or stdin.
func DefaultToken ¶ added in v0.7.0
func DefaultToken() string
DefaultToken returns the active token, checking environment variables first, then falling back to the configured token file.
func ExitCode ¶ added in v0.3.7
ExitCode maps a command error to a stable process exit code.
func FormatAPIID ¶ added in v0.3.7
func FormatAPIID(id interface{}) string
FormatAPIID normalizes API IDs that may arrive as strings or JSON numbers.
func NewCLIError ¶ added in v0.3.7
NewCLIError creates a CLIError with a stable exit code.
func NormalizeLabels ¶ added in v0.11.0
NormalizeLabels parses a slice of label names (typically from a comma-separated --add/--labels flag or cobra StringSlice) into a clean list: splits on comma, trims whitespace, drops empty entries, and de-duplicates. Splitting is idempotent for already-split input, so it is safe whether cobra pre-split the value or not. Order of first occurrence is preserved.
func ParseRepo ¶ added in v0.3.7
ParseRepo parses a repository reference and returns the owner and repository name. It supports owner/repo, HTTPS URLs, and SSH URLs.
func ReadBody ¶ added in v0.7.0
ReadBody resolves the body text from --body and --body-file flags. body and bodyFile are the raw flag values; stdin is used when bodyFile == "-". It returns an error when both body and bodyFile are set.
func ReadText ¶ added in v0.5.8
ReadText reads user-provided text from a stream.
func ReadTextFile ¶ added in v0.5.3
ReadTextFile reads a user-provided text file and strips a UTF-8 BOM when present.
func ReadTextFromFlag ¶ added in v0.5.9
ReadTextFromFlag reads stdin text for an explicit file flag such as --body-file - or --comment-file - and rejects input that Windows PowerShell appears to have already corrupted before the CLI could decode it.
func ResolvePRURL ¶ added in v0.5.0
ResolvePRURL returns the PR URL with fallback logic. If the API-provided URL is empty, it constructs one from owner/repo/number.
func ResolveRepo ¶ added in v0.3.7
ResolveRepo returns the explicit repository when provided, otherwise tries to infer it from the current git repository.
func ScanContentForSecrets ¶ added in v0.7.0
ScanContentForSecrets scans content for the current GC_TOKEN/GITCODE_TOKEN environment variable value. It is called before submitting user-provided body/comment content to the GitCode API to prevent AI agents (or humans) from accidentally leaking the current GitCode credential into issues, PRs, comments, or releases.
GitCode tokens have no fixed prefix (unlike GitHub's ghp_), so pattern matching is not viable; the only reliable check is whether the current token value appears in the content. This is the highest-value defense against the common AI mistake of pasting $GC_TOKEN into an issue body.
On detection, returns an error. The content is NOT modified (no redaction); submission is refused so the caller can surface the error before any API call is made.
func SetFlagEnum ¶ added in v0.4.0
SetFlagEnum records a stable enum set for schema/export consumers. It returns an error (e.g. when the flag is not registered on the command) instead of panicking, so callers can decide how to handle it.
func SetFlagEnumOrWarn ¶ added in v0.11.0
SetFlagEnumOrWarn records a stable enum set for schema/export consumers and degrades an annotation failure to a stderr warning instead of crashing the process. Use this from command constructors (NewCmd*) whose signature (*cobra.Command) cannot propagate an error; the annotation is non-functional metadata for schema/export, so a missing enum does not break the command.
func SetTopicAnnotation ¶ added in v0.4.0
SetTopicAnnotation adds a topic annotation to a command.
func WrapNotFound ¶ added in v0.5.0
WrapNotFound wraps an error as NotFoundError if it's a 404 API error. Returns the original error if it's not a 404. Usage: return cmdutil.WrapNotFound(err, "issue #%d not found in %s/%s", number, owner, repo)
func WriteJSON ¶ added in v0.3.7
WriteJSON writes indented JSON to the target writer.
A nil slice is normalized to an empty slice so list commands emit `[]` instead of `null` when there are no results, keeping --json output stable and consumable by scripts and agents (see spec/foundations/agent-friendly-cli.md).
Only the top-level value is normalized. A nil value with no concrete type, a pointer to a nil slice (e.g. *[]T), and nil slice fields nested inside a struct are left untouched and still encode as `null`; commands that rely on such fields (e.g. pr view --json emitting "comments": null) are unaffected.
Types ¶
type CLIError ¶ added in v0.3.7
CLIError represents a stable CLI-facing error with an exit code.
type ConfirmOptions ¶ added in v0.3.7
ConfirmOptions controls destructive-action confirmation prompts.
Source Files
¶
- auth.go
- confirm.go
- errors.go
- factory.go
- flag_annotations.go
- format.go
- output.go
- repo.go
- secret_scan.go
- text_file.go
- token_confirm.go