Documentation
¶
Overview ¶
Package cmd provides reusable helpers for command wiring and execution.
This package contains shared utilities that simplify the construction and execution of Cobra commands. Key functionality includes:
- Kubeconfig path resolution with home directory expansion
- Docker client lifecycle management with automatic cleanup
- Lifecycle command helpers for cluster operations (start, stop, delete, etc.)
- Command runner utilities for executing commands with output capture
The utilities in this package follow dependency injection patterns and integrate with the KSail runtime container for testability and flexibility.
Index ¶
- Constants
- Variables
- func ExtractClusterNameFromContext(context string, distribution v1alpha1.Distribution) string
- func GetClusterNameFromConfig(clusterCfg *v1alpha1.Cluster, factory clusterprovisioner.Factory) (string, error)
- func GetDefaultKubeconfigPath() string
- func GetKubeconfigPathFromConfig(cfg *v1alpha1.Cluster) (string, error)
- func GetKubeconfigPathSilently() string
- func HandleLifecycleRunE(cmd *cobra.Command, cfgManager *ksailconfigmanager.ConfigManager, ...) error
- func IsTimingEnabled(cmd *cobra.Command) (bool, error)
- func MaybeTimer(cmd *cobra.Command, tmr timer.Timer) timer.Timer
- func NewStandardLifecycleRunE(runtimeContainer *runtime.Runtime, ...) func(*cobra.Command, []string) error
- func RunLifecycleWithConfig(cmd *cobra.Command, deps LifecycleDeps, config LifecycleConfig, ...) error
- 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
- func WrapLifecycleHandler(runtimeContainer *runtime.Runtime, ...) func(*cobra.Command, []string) error
- type EditorResolver
- type LifecycleAction
- type LifecycleConfig
- type LifecycleDeps
Constants ¶
const TimingFlagName = "timing"
TimingFlagName is the global/root persistent flag that enables timing output.
Variables ¶
var ErrClusterConfigRequired = errors.New("cluster configuration is required")
ErrClusterConfigRequired indicates that a nil cluster configuration was provided.
var ErrMissingClusterProvisionerDependency = errors.New("missing cluster provisioner dependency")
ErrMissingClusterProvisionerDependency indicates that a lifecycle command resolved a nil provisioner.
Functions ¶
func ExtractClusterNameFromContext ¶
func ExtractClusterNameFromContext(context string, distribution v1alpha1.Distribution) string
ExtractClusterNameFromContext extracts the cluster name from a context string. For kind clusters, contexts follow the pattern "kind-<cluster-name>". For k3d clusters, contexts follow the pattern "k3d-<cluster-name>". Returns empty string if the context doesn't match the expected pattern.
func GetClusterNameFromConfig ¶
func GetClusterNameFromConfig( clusterCfg *v1alpha1.Cluster, factory clusterprovisioner.Factory, ) (string, error)
GetClusterNameFromConfig extracts the cluster name from the KSail cluster configuration. When a context is explicitly set, it derives the cluster name from it. Otherwise, it loads the distribution config and extracts the name from there. This function is exported for use in command handlers that need the cluster name for operations beyond the standard lifecycle actions.
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 HandleLifecycleRunE ¶
func HandleLifecycleRunE( cmd *cobra.Command, cfgManager *ksailconfigmanager.ConfigManager, deps LifecycleDeps, config LifecycleConfig, ) error
HandleLifecycleRunE orchestrates the standard lifecycle workflow. It performs the following steps in order:
- Start the timer
- Load the cluster configuration
- Create a new timer stage
- Execute the lifecycle action via RunLifecycleWithConfig
This function provides the complete workflow for standard lifecycle commands.
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 NewStandardLifecycleRunE ¶
func NewStandardLifecycleRunE( runtimeContainer *runtime.Runtime, cfgManager *ksailconfigmanager.ConfigManager, config LifecycleConfig, ) func(*cobra.Command, []string) error
NewStandardLifecycleRunE creates a standard RunE handler for simple lifecycle commands. It handles dependency injection from the runtime container and delegates to HandleLifecycleRunE with the provided lifecycle configuration.
This is the recommended way to create lifecycle command handlers for standard operations like start, stop, and delete. The returned function can be assigned directly to a cobra.Command's RunE field.
func RunLifecycleWithConfig ¶
func RunLifecycleWithConfig( cmd *cobra.Command, deps LifecycleDeps, config LifecycleConfig, clusterCfg *v1alpha1.Cluster, ) error
RunLifecycleWithConfig executes a lifecycle command using a pre-loaded cluster configuration. This function is useful when the cluster configuration has already been loaded, avoiding the need to reload it.
It performs the following steps:
- Create the cluster provisioner using the factory
- Extract the cluster name from the distribution config or context
- Execute the lifecycle action
- Display success message with timing information
Returns an error if provisioner creation, cluster name extraction, or the action itself fails.
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.
func WrapLifecycleHandler ¶
func WrapLifecycleHandler( runtimeContainer *runtime.Runtime, cfgManager *ksailconfigmanager.ConfigManager, handler func(*cobra.Command, *ksailconfigmanager.ConfigManager, LifecycleDeps) error, ) func(*cobra.Command, []string) error
WrapLifecycleHandler resolves lifecycle dependencies from the runtime container and invokes the provided handler function with those dependencies.
This function is used internally by NewStandardLifecycleRunE but can also be used directly for custom lifecycle handlers that need dependency injection but require custom logic beyond the standard HandleLifecycleRunE flow.
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) ResolveEditor ¶
func (r *EditorResolver) ResolveEditor() string
ResolveEditor 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) SetEditorEnvVars ¶
func (r *EditorResolver) SetEditorEnvVars(editor string, forCommand string) func()
SetEditorEnvVars sets the appropriate environment variables for the resolved editor. It returns a cleanup function that restores the original environment.
type LifecycleAction ¶
type LifecycleAction func( ctx context.Context, provisioner clusterprovisioner.ClusterProvisioner, clusterName string, ) error
LifecycleAction represents a lifecycle operation executed against a cluster provisioner. The action receives a context for cancellation, the provisioner instance, and the cluster name. It returns an error if the lifecycle operation fails.
type LifecycleConfig ¶
type LifecycleConfig struct {
TitleEmoji string
TitleContent string
ActivityContent string
SuccessContent string
ErrorMessagePrefix string
Action LifecycleAction
}
LifecycleConfig describes the messaging and action behavior for a lifecycle command. It configures the user-facing messages displayed during command execution and specifies the action to perform on the cluster provisioner.
type LifecycleDeps ¶
type LifecycleDeps struct {
Timer timer.Timer
Factory clusterprovisioner.Factory
}
LifecycleDeps groups the injectable collaborators required by lifecycle commands.