Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 60 additions & 36 deletions cmd/check.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package cmd

import (
"context"
"fmt"
"time"

"github.com/spf13/cobra"
"github.com/windsorcli/cli/pkg/constants"
"github.com/windsorcli/cli/pkg/context"
"github.com/windsorcli/cli/pkg/di"
"github.com/windsorcli/cli/pkg/pipelines"
"github.com/windsorcli/cli/pkg/provisioner"
)

var (
Expand All @@ -25,27 +25,31 @@ var checkCmd = &cobra.Command{
Long: "Check the tool versions required by the project",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
// Get shared dependency injector from context
injector := cmd.Context().Value(injectorKey).(di.Injector)

// Create output function
outputFunc := func(output string) {
fmt.Fprintln(cmd.OutOrStdout(), output)
execCtx := &context.ExecutionContext{
Injector: injector,
}

// Create execution context with operation and output function
ctx := context.WithValue(cmd.Context(), "operation", "tools")
ctx = context.WithValue(ctx, "output", outputFunc)

// Set up the check pipeline
pipeline, err := pipelines.WithPipeline(injector, ctx, "checkPipeline")
execCtx, err := context.NewContext(execCtx)
if err != nil {
return fmt.Errorf("failed to set up check pipeline: %w", err)
return fmt.Errorf("failed to initialize context: %w", err)
}

if err := execCtx.CheckTrustedDirectory(); err != nil {
return fmt.Errorf("not in a trusted directory. If you are in a Windsor project, run 'windsor init' to approve")
}

if err := execCtx.LoadConfig(); err != nil {
return err
}

// Execute the pipeline
if err := pipeline.Execute(ctx); err != nil {
return fmt.Errorf("Error executing check pipeline: %w", err)
if !execCtx.ConfigHandler.IsLoaded() {
return fmt.Errorf("Nothing to check. Have you run \033[1mwindsor init\033[0m?")
}

if err := execCtx.CheckTools(); err != nil {
return err
}

return nil
Expand All @@ -58,43 +62,63 @@ var checkNodeHealthCmd = &cobra.Command{
Long: "Check the health status of specified cluster nodes",
SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error {
// Get shared dependency injector from context
injector := cmd.Context().Value(injectorKey).(di.Injector)

// Require at least one health check type to be specified
if len(nodeHealthNodes) == 0 && k8sEndpoint == "" {
return fmt.Errorf("No health checks specified. Use --nodes and/or --k8s-endpoint flags to specify health checks to perform")
}

// If timeout is not set via flag, use default
if !cmd.Flags().Changed("timeout") {
nodeHealthTimeout = constants.DefaultNodeHealthCheckTimeout
}

// Create output function
execCtx := &context.ExecutionContext{
Injector: injector,
}

execCtx, err := context.NewContext(execCtx)
if err != nil {
return fmt.Errorf("failed to initialize context: %w", err)
}

if err := execCtx.CheckTrustedDirectory(); err != nil {
return fmt.Errorf("not in a trusted directory. If you are in a Windsor project, run 'windsor init' to approve")
}

if err := execCtx.LoadConfig(); err != nil {
return err
}

if !execCtx.ConfigHandler.IsLoaded() {
return fmt.Errorf("Nothing to check. Have you run \033[1mwindsor init\033[0m?")
}

provisionerCtx := &provisioner.ProvisionerExecutionContext{
ExecutionContext: *execCtx,
}

prov := provisioner.NewProvisioner(provisionerCtx)

outputFunc := func(output string) {
fmt.Fprintln(cmd.OutOrStdout(), output)
}

// Create execution context with operation, nodes, timeout, version, and output function
ctx := context.WithValue(cmd.Context(), "operation", "node-health")
ctx = context.WithValue(ctx, "nodes", nodeHealthNodes)
ctx = context.WithValue(ctx, "timeout", nodeHealthTimeout)
ctx = context.WithValue(ctx, "version", nodeHealthVersion)
ctx = context.WithValue(ctx, "k8s-endpoint", k8sEndpoint)
ctx = context.WithValue(ctx, "k8s-endpoint-provided", k8sEndpoint != "" || checkNodeReady)
ctx = context.WithValue(ctx, "check-node-ready", checkNodeReady)
ctx = context.WithValue(ctx, "output", outputFunc)
k8sEndpointStr := k8sEndpoint
if k8sEndpointStr == "" && checkNodeReady {
k8sEndpointStr = "true"
}

// Set up the check pipeline
pipeline, err := pipelines.WithPipeline(injector, ctx, "checkPipeline")
if err != nil {
return fmt.Errorf("failed to set up check pipeline: %w", err)
options := provisioner.NodeHealthCheckOptions{
Nodes: nodeHealthNodes,
Timeout: nodeHealthTimeout,
Version: nodeHealthVersion,
K8SEndpoint: k8sEndpointStr,
K8SEndpointProvided: k8sEndpoint != "" || checkNodeReady,
CheckNodeReady: checkNodeReady,
}

// Execute the pipeline
if err := pipeline.Execute(ctx); err != nil {
return fmt.Errorf("Error executing check pipeline: %w", err)
if err := prov.CheckNodeHealth(cmd.Context(), options, outputFunc); err != nil {
return fmt.Errorf("error checking node health: %w", err)
}

return nil
Expand Down
Loading
Loading