Documentation
¶
Overview ¶
Package cmdutil provides shared CLI utilities, including the Factory dependency container.
Index ¶
- Constants
- Variables
- func ConfigNotSetError(fieldName, prefix string, opts ConfigOption, hint string) error
- func ExactArgs(n int, usage string, hint ...string) cobra.PositionalArgs
- func FinalizeTree(root *cobra.Command)
- func IsValidDNS1123Label(s string) bool
- func Rewritef(cause error, format string, args ...any) error
- func ValidateK8sName(placeholder, value string) error
- func ValidateK8sSubdomain(placeholder, value string) error
- type ConfigOption
- type Factory
Constants ¶
const DNS1123SubdomainMaxLength = 253
const DefaultHTTPTimeout = 45 * time.Second
DefaultHTTPTimeout caps every portal REST request. Must exceed the portal's 30s upstream client timeout so its 504 arrives before the client aborts as a transport error.
const K8sNamePattern = `^` + k8sLabelPattern + `$`
K8sNamePattern is the portal's tektonInputSchemas.k8sName shape: lowercase alphanumerics and '-', no dots, start/end alphanumeric. Length is bounded separately by DNS1123SubdomainMaxLength.
const LoginHint = "Or run: krci auth login --portal-url <url>"
LoginHint suggests running "krci auth login" to configure the CLI.
Variables ¶
var ClusterNameOption = ConfigOption{
EnvVar: "KRCI_CLUSTER_NAME",
ConfigKey: "cluster-name in ~/.config/krci/config.yaml",
}
ClusterNameOption describes how to set the cluster name.
var IssuerURLOption = ConfigOption{
EnvVar: "KRCI_ISSUER_URL",
ConfigKey: "issuer-url in ~/.config/krci/config.yaml",
}
IssuerURLOption describes how to set the OIDC issuer URL.
var NamespaceOption = ConfigOption{
EnvVar: "KRCI_NAMESPACE",
ConfigKey: "namespace in ~/.config/krci/config.yaml",
}
NamespaceOption describes how to set the namespace.
var PortalURLOption = ConfigOption{
Flag: "--portal-url",
EnvVar: "KRCI_PORTAL_URL",
ConfigKey: "portal-url in ~/.config/krci/config.yaml",
}
PortalURLOption describes how to set the portal URL.
Functions ¶
func ConfigNotSetError ¶ added in v0.3.0
func ConfigNotSetError(fieldName, prefix string, opts ConfigOption, hint string) error
ConfigNotSetError returns a formatted error for a missing configuration field. When prefix is empty, the error opens with "<fieldName> not configured". When prefix is non-empty, it is used verbatim as the opening line. hint, when non-empty, is appended after the options list.
func ExactArgs ¶ added in v0.4.0
func ExactArgs(n int, usage string, hint ...string) cobra.PositionalArgs
ExactArgs returns a cobra.PositionalArgs that requires exactly n arguments, producing a user-friendly error message that names the missing argument. An optional hint is appended as a "Hint:" line when provided.
func FinalizeTree ¶ added in v0.20.0
FinalizeTree sets the argument contract of every command under root:
- Args run validateStringFlags first.
- nil Args means cobra.NoArgs; cobra's help command, which takes a command path, gets cobra.ArbitraryArgs.
- A group without Run or RunE prints its help: a bare group exits 0, a mistyped subcommand fails with unknown command.
Root keeps cobra's own unknown-command check. Call once, after every AddCommand and after InitDefaultHelpCmd and InitDefaultCompletionCmd on the root; cobra otherwise adds those two commands inside Execute, unfinalized.
func IsValidDNS1123Label ¶ added in v0.8.0
func Rewritef ¶ added in v0.20.0
Rewritef returns an error whose message is the formatted text, without the text of cause. errors.Is and errors.As still match cause.
func ValidateK8sName ¶ added in v0.15.0
ValidateK8sName validates a name the user types for an object the platform names: project, pipeline, deployment, env, cluster. A pod name goes through ValidateK8sSubdomain. placeholder names the argument in the error, e.g. "<project>" or "--cluster".
func ValidateK8sSubdomain ¶ added in v0.20.0
ValidateK8sSubdomain validates the name of an object the platform does not name, such as a pod: Kubernetes accepts dots in it, ValidateK8sName does not.
Types ¶
type ConfigOption ¶ added in v0.3.0
type ConfigOption struct {
Flag string // e.g. "--portal-url" (empty if not available)
EnvVar string // e.g. "KRCI_PORTAL_URL"
ConfigKey string // e.g. "portal-url in ~/.config/krci/config.yaml"
}
ConfigOption describes one way to set a configuration value.
type Factory ¶
type Factory struct {
IOStreams *iostreams.IOStreams
Config func() (*config.Config, error)
TokenProvider func() (auth.TokenProvider, error)
RestClient func() (*restapi.ClientWithResponses, error)
// Services returns the portal services of the configured target over
// RestClient, so it passes the same configuration gate.
Services func() (*portal.Services, error)
// Now is the clock for running pipeline-run durations. Default: time.Now.
Now func() time.Time
// contains filtered or unexported fields
}
Factory holds lazy-func dependencies shared across all CLI commands. Config memoizes the first successful result and retries after a failure; TokenProvider, RestClient and Services memoize their first result, including an error. Set every field before NewCmdRoot builds the tree. Commands copy the func values when the tree is built; Services reads RestClient, Config and Now on its first call and keeps the result. Set the config source through SetConfigResolver.