diff --git a/pkg/deploy/deploy.go b/pkg/deploy/deploy.go index aaef95c..c08910c 100644 --- a/pkg/deploy/deploy.go +++ b/pkg/deploy/deploy.go @@ -1,19 +1,6 @@ package deploy -import ( - "fmt" - "os/exec" -) - -func checkDependency(name string) error { - - _, err := exec.LookPath(name) - if err != nil { - return fmt.Errorf("%s not found in PATH. please install it to continue", name) - } - - return nil -} +import "fmt" func Run(envName string, dryRun bool) error { @@ -22,12 +9,16 @@ func Run(envName string, dryRun bool) error { return err } - command, _, err := BuildCommand(environment) - if err != nil { + //////////////////////////////////////////////////// + // PREFLIGHT FIRST + //////////////////////////////////////////////////// + + if err := Preflight(environment); err != nil { return err } - if err := checkDependency(command.Name); err != nil { + command, _, err := BuildCommand(environment) + if err != nil { return err } @@ -35,5 +26,7 @@ func Run(envName string, dryRun bool) error { DryRun: dryRun, } + fmt.Println("Starting deployment...") + return executor.Run(command.Name, command.Args...) } diff --git a/pkg/deploy/preflight.go b/pkg/deploy/preflight.go new file mode 100644 index 0000000..d454217 --- /dev/null +++ b/pkg/deploy/preflight.go @@ -0,0 +1,62 @@ +package deploy + +import ( + "fmt" + "os/exec" + + "github.com/aryansharma9917/codewise-cli/pkg/env" +) + +func runCheck(name string, args ...string) error { + + cmd := exec.Command(name, args...) + + if err := cmd.Run(); err != nil { + return err + } + + return nil +} + +func Preflight(environment *env.Env) error { + + fmt.Println("Running preflight checks...") + + strategy := ResolveStrategy(environment) + + //////////////////////////////////////////////////// + // Check binary availability + //////////////////////////////////////////////////// + + switch strategy { + + case StrategyHelm: + + if err := runCheck("helm", "version"); err != nil { + return fmt.Errorf("helm not available or not functioning") + } + + case StrategyKubectl: + + if err := runCheck("kubectl", "version", "--client"); err != nil { + return fmt.Errorf("kubectl not available or not functioning") + } + } + + //////////////////////////////////////////////////// + // Cluster connectivity check + //////////////////////////////////////////////////// + + args := []string{"cluster-info"} + + if environment.K8s.Context != "" { + args = append(args, "--context", environment.K8s.Context) + } + + if err := runCheck("kubectl", args...); err != nil { + return fmt.Errorf("cannot reach kubernetes cluster. check kube-context") + } + + fmt.Println("Cluster reachable") + return nil +}