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
- 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 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 EditorResolver
Constants ¶
const TimingFlagName = "timing"
TimingFlagName is the global/root persistent flag that enables timing output.
Variables ¶
This section is empty.
Functions ¶
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 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 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.