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
27 changes: 10 additions & 17 deletions pkg/deploy/deploy.go
Original file line number Diff line number Diff line change
@@ -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 {

Expand All @@ -22,18 +9,24 @@ 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
}

executor := Executor{
DryRun: dryRun,
}

fmt.Println("Starting deployment...")

return executor.Run(command.Name, command.Args...)
}
62 changes: 62 additions & 0 deletions pkg/deploy/preflight.go
Original file line number Diff line number Diff line change
@@ -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
}