Documentation
¶
Overview ¶
Package helpers provides common CLI utilities for command handling.
This package consolidates various CLI helper functions that were previously scattered across multiple small packages (docker, editor, flags, kubeconfig). By grouping these related utilities together, we achieve better package cohesion and reduce fragmentation.
Key functionality:
- Docker client lifecycle management (WithClient, WithClientInstance)
- Editor configuration resolution with proper precedence
- Flag handling utilities including timing detection
- Kubeconfig path resolution with home directory expansion
Index ¶
- Constants
- Variables
- func DetectGitOpsEngine(ctx context.Context) (v1alpha1.GitOpsEngine, error)
- func DetectLocalRegistryPort(ctx context.Context) (int32, error)
- func GetDefaultKubeconfigPath() string
- func GetKubeconfigPathFromConfig(cfg *v1alpha1.Cluster) (string, error)
- func GetKubeconfigPathSilently() string
- func IsTimingEnabled(cmd *cobra.Command) (bool, error)
- func MaybeTimer(cmd *cobra.Command, tmr timer.Timer) timer.Timer
- func NewStandardIOStreams() genericiooptions.IOStreams
- func SetupEditorEnv(editorFlag, forCommand string) func()
- func WithDockerClient(cmd *cobra.Command, operation func(client.APIClient) error) error
- func WithDockerClientInstance(cmd *cobra.Command, dockerClient client.APIClient, ...) error
- type ClusterEnvironment
- type EditorResolver
Constants ¶
const TimingFlagName = "timing"
TimingFlagName is the global/root persistent flag that enables timing output.
Variables ¶
var ErrNoGitOpsEngine = errors.New(
"no GitOps engine detected in cluster; " +
"create a cluster with '--gitops-engine Flux|ArgoCD' during cluster init",
)
ErrNoGitOpsEngine is returned when no GitOps engine is detected.
var ErrNoLocalRegistry = errors.New(
"no running local registry found; " +
"create a cluster with '--local-registry Enabled' during cluster init",
)
ErrNoLocalRegistry is returned when no local registry container is found.
Functions ¶
func DetectGitOpsEngine ¶ added in v5.13.0
func DetectGitOpsEngine(ctx context.Context) (v1alpha1.GitOpsEngine, error)
DetectGitOpsEngine checks if Flux or ArgoCD is deployed in the cluster.
func DetectLocalRegistryPort ¶ added in v5.13.0
DetectLocalRegistryPort finds the host port of a running local-registry container. It checks for cluster-prefixed registries (e.g., "kind-local-registry", "k3d-default-local-registry") by searching for containers matching the "*-local-registry" pattern. Both KSail-managed registries (with labels) and K3d-managed registries are detected.
func GetDefaultKubeconfigPath ¶
func GetDefaultKubeconfigPath() string
GetDefaultKubeconfigPath returns the default kubeconfig path for the current user. The path is constructed as ~/.kube/config using the user's home directory.
func GetKubeconfigPathFromConfig ¶
GetKubeconfigPathFromConfig extracts and expands the kubeconfig path from a loaded cluster config. If the config doesn't specify a kubeconfig path, it returns the default path from GetDefaultKubeconfigPath.
The function always expands tilde (~) characters in the path to the user's home directory, regardless of whether the path came from the config or is the default.
Returns an error if path expansion fails.
func GetKubeconfigPathSilently ¶
func GetKubeconfigPathSilently() string
GetKubeconfigPathSilently attempts to load the KSail config and extract the kubeconfig path without producing any output. All config loading output is suppressed using io.Discard.
If config loading fails for any reason, this function returns the default kubeconfig path rather than propagating the error. This makes it suitable for scenarios where a best-effort path is acceptable.
func IsTimingEnabled ¶
IsTimingEnabled reports whether the current command invocation has timing enabled.
The flag is defined as a root persistent flag and inherited by subcommands.
func MaybeTimer ¶
MaybeTimer returns the provided timer when timing output is enabled.
When timing is disabled (or the flag is unavailable), it returns nil.
func NewStandardIOStreams ¶ added in v5.13.0
func NewStandardIOStreams() genericiooptions.IOStreams
NewStandardIOStreams creates IOStreams for standard input/output/error. This is used by kubectl-based commands across the CLI.
func SetupEditorEnv ¶
func SetupEditorEnv(editorFlag, forCommand string) func()
SetupEditorEnv sets up the editor environment variables based on flag and config. It returns a cleanup function that should be called to restore the original environment.
func WithDockerClient ¶
WithDockerClient creates a Docker client, executes the given operation function, and ensures cleanup. The Docker client is automatically closed after the operation completes, regardless of success or failure.
This function is suitable for production use. For testing with mock clients, use WithDockerClientInstance instead.
Returns an error if client creation fails or if the operation function returns an error.
func WithDockerClientInstance ¶
func WithDockerClientInstance( cmd *cobra.Command, dockerClient client.APIClient, operation func(client.APIClient) error, ) error
WithDockerClientInstance executes an operation with a provided Docker client and handles cleanup. The client will be closed after the operation completes, even if the operation returns an error.
This function is particularly useful for testing with mock clients, as it allows you to provide a pre-configured client instance. Any error during client cleanup is logged but does not cause the function to return an error if the operation itself succeeded.
Types ¶
type ClusterEnvironment ¶ added in v5.13.0
type ClusterEnvironment struct {
RegistryPort int32
GitOpsEngine v1alpha1.GitOpsEngine
}
ClusterEnvironment holds auto-detected cluster configuration from the running environment.
func DetectClusterEnvironment ¶ added in v5.13.0
func DetectClusterEnvironment(ctx context.Context) (*ClusterEnvironment, error)
DetectClusterEnvironment auto-detects the registry port and GitOps engine from the running Docker containers and Kubernetes cluster.
type EditorResolver ¶
type EditorResolver struct {
// contains filtered or unexported fields
}
EditorResolver handles editor configuration resolution with proper precedence.
func NewEditorResolver ¶
func NewEditorResolver(flagEditor string, cfg *v1alpha1.Cluster) *EditorResolver
NewEditorResolver creates a new editor resolver.
func (*EditorResolver) Resolve ¶
func (r *EditorResolver) Resolve() string
Resolve resolves the editor command based on precedence: 1. --editor flag 2. spec.editor from config 3. Environment variables (SOPS_EDITOR, KUBE_EDITOR, EDITOR, VISUAL) 4. Fallback to vim, nano, vi.
func (*EditorResolver) SetEnvVars ¶
func (r *EditorResolver) SetEnvVars(editorCmd string, forCommand string) func()
SetEnvVars sets the appropriate environment variables for the resolved editor. It returns a cleanup function that restores the original environment.