Documentation
¶
Overview ¶
Package sonarinternal holds helpers shared by `krci sonar` verbs (validation, enum sets, render/format dispatch, error-envelope plumbing). Placed under an `internal/` directory so Go enforces package visibility: only packages below `pkg/cmd/sonar/` may import it.
Index ¶
- Constants
- Variables
- func HandleError(ios *iostreams.IOStreams, outputFormat string, err error) error
- func PageCount(total, pageSize int) int
- func PrintTable(w io.Writer, isTTY bool, headers []string, rows [][]string) error
- func Render[T any](ios *iostreams.IOStreams, outputFormat string, data T, ...) error
- func ValidateEnumCSV(flagName string, values []string, allowed EnumSet) ([]string, error)
- func ValidateOutputFormat(format string) error
- func ValidateProjectCommand(cmd *cobra.Command, outputFormat, project, pullRequest string) error
- func ValidateProjectKey(project string) error
- func ValidatePullRequest(changed bool, value string) error
- type EnumSet
- type TableRenderer
Constants ¶
const MaxPageSize = 500
MaxPageSize is the upper bound on `--page-size` for every paginated sonar verb, mirroring the shared Zod schema cap on portal-side.
const SchemaVersion = "1"
SchemaVersion is the shared JSON envelope version for every `krci sonar *` verb. Bump only when the envelope shape changes in a way scripting consumers must detect (e.g. removal of a previously-required key).
Variables ¶
var ( IssueTypes = newEnumSet("BUG", "VULNERABILITY", "CODE_SMELL") IssueSeverities = newEnumSet("BLOCKER", "CRITICAL", "MAJOR", "MINOR", "INFO") IssueStatuses = newEnumSet("OPEN", "CONFIRMED", "REOPENED", "RESOLVED", "CLOSED") )
Canonical SonarQube enum sets. Keep in sync with the Portal's shared SonarQube schema.
Functions ¶
func HandleError ¶
HandleError promotes portal.ErrUnauthorized to the shared "run krci auth login" message, and — when `-o json` is selected — also writes the `{schemaVersion, error: { message }}` envelope to stdout so scripting consumers get a structured error alongside the exit-1 signal. Callers still return the returned error so root-level main.go emits the plain-text form on stderr and exits 1.
func PageCount ¶
PageCount returns the number of pages of pageSize required to hold total items. Always returns at least 1 so footers never read "page 1 of 0".
func PrintTable ¶
PrintTable is the shared TTY/non-TTY dispatcher for sonar verbs that show a plain 2D table. Styled output for interactive terminals, plain tabwriter output when stdout is piped.
func Render ¶
func Render[T any](ios *iostreams.IOStreams, outputFormat string, data T, renderTable TableRenderer) error
Render dispatches on the `-o` flag: emit the shared `{schemaVersion, data}` JSON envelope for `-o json`, invoke the supplied TableRenderer for `-o table` (the default), or return a validation error for anything else.
func ValidateEnumCSV ¶
ValidateEnumCSV checks every comma-separated/repeated value in values against allowed. flagName is used in error messages. Returns the flattened list of validated items in input order with duplicates collapsed (empty when values is empty).
func ValidateOutputFormat ¶
ValidateOutputFormat rejects `-o` values other than "", "table", or "json". An empty string means "use default" (table) and is always valid.
func ValidateProjectCommand ¶
ValidateProjectCommand runs the standard validator triple used by the three project-scoped verbs (get, gate, issues): output format → DNS-1123 project key → non-empty --pull-request (when supplied).
func ValidateProjectKey ¶
ValidateProjectKey returns an error when project does not match DNS-1123. Codebase names (by Portal convention, also the SonarQube projectKey) follow the same shape as Kubernetes namespaces — see internal/cmdutil/validate.go.
func ValidatePullRequest ¶
ValidatePullRequest rejects an explicitly supplied empty --pull-request value. changed is true when the flag was set on the command line (regardless of value).