Skip to content

Commit

Permalink
Improve error handlingg for shell commands
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Feb 1, 2024
1 parent c6088d7 commit cc185a0
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 15 deletions.
17 changes: 15 additions & 2 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,30 @@ func createCluster() {
cmd_tpl := "%v create cluster --config %v%v"
cmd := fmt.Sprintf(cmd_tpl, internal.Kind, internal.KindConfigFile, optName)

ExecCmd(cmd, false)
_, _, err = ExecCmd(cmd, false)
if err != nil {
slog.Error("kind create cluster failed", "error", err)
os.Exit(1)
}

if c.Calico {
slog.Info("Install Calico CNI")
cmd = `kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.24.5/manifests/tigera-operator.yaml &&
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.24.5/manifests/custom-resources.yaml`
ExecCmd(cmd, false)
_, _, err = ExecCmd(cmd, false)
if err != nil {
slog.Error("calico installation failed", "error", err)
os.Exit(1)
}

}

slog.Info("Wait for Kubernetes nodes to be up and running")
cmd = "kubectl wait --timeout=180s --for=condition=Ready node --all"
ExecCmd(cmd, false)
_, _, err = ExecCmd(cmd, false)
if err != nil {
slog.Error("kubectl wait failed", "error", err)
os.Exit(1)
}
}
8 changes: 6 additions & 2 deletions cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package cmd
import (
"fmt"
"log/slog"
"os"

"github.com/k8s-school/ktbx/internal"
"github.com/spf13/cobra"
Expand All @@ -22,8 +23,11 @@ func deleteCluster() {

cmd := fmt.Sprintf(cmd_tpl, internal.Kind, optName)

ExecCmd(cmd, false)

_, _, err := ExecCmd(cmd, false)
if err != nil {
slog.Error("Error while deleting cluster", "error", err)
os.Exit(1)
}
}

// deleteCmd represents the delete command
Expand Down
14 changes: 12 additions & 2 deletions cmd/desk.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package cmd

import (
"fmt"
"log/slog"
"os"

"github.com/k8s-school/ktbx/resources"
"github.com/spf13/cobra"
Expand All @@ -23,10 +25,18 @@ var deskCmd = &cobra.Command{
if showDockerCmd {
// TODO escape ' in command
script := "SHOWDOCKERCMD=true\n" + resources.DeskRunScript
ExecCmd(script, false)
_, _, err := ExecCmd(script, false)
if err != nil {
slog.Error("Error while executing desk script", "error", err)
os.Exit(1)
}
} else {
fmt.Println("Launch interactive desk")
ExecCmd(resources.DeskRunScript, true)
_, _, err := ExecCmd(resources.DeskRunScript, true)
if err != nil {
slog.Error("Error while launching interactive desk", "error", err)
os.Exit(1)
}
}

},
Expand Down
7 changes: 6 additions & 1 deletion cmd/install_argocd.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cmd

import (
"log/slog"
"os"

"github.com/k8s-school/ktbx/resources"
"github.com/spf13/cobra"
Expand All @@ -18,7 +19,11 @@ var argocdCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
slog.Info("Install ArgoCD")

ExecCmd(resources.ArgoCDInstallScript, false)
_, _, err := ExecCmd(resources.ArgoCDInstallScript, false)
if err != nil {
slog.Error("Error while installing ArgoCD", "error", err)
os.Exit(1)
}
},
}

Expand Down
7 changes: 6 additions & 1 deletion cmd/install_argoworkflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cmd

import (
"log/slog"
"os"

"github.com/k8s-school/ktbx/resources"
"github.com/spf13/cobra"
Expand All @@ -18,7 +19,11 @@ var argoWorkflowsCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
slog.Info("Install Argo-workflows")

ExecCmd(resources.ArgoWorkflowInstallScript, false)
_, _, err := ExecCmd(resources.ArgoWorkflowInstallScript, false)
if err != nil {
slog.Error("Error while installing Argo-workflows", "error", err)
os.Exit(1)
}
},
}

Expand Down
8 changes: 7 additions & 1 deletion cmd/install_helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package cmd

import (
"fmt"
"log/slog"
"os"

"github.com/k8s-school/ktbx/resources"
"github.com/spf13/cobra"
Expand All @@ -18,7 +20,11 @@ var helmCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Install helm")

ExecCmd(resources.HelmInstallScript, false)
_, _, err := ExecCmd(resources.HelmInstallScript, false)
if err != nil {
slog.Error("Error while installing helm", "error", err)
os.Exit(1)
}
},
}

Expand Down
8 changes: 7 additions & 1 deletion cmd/install_kind.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package cmd

import (
"fmt"
"log/slog"
"os"

"github.com/k8s-school/ktbx/internal"
"github.com/k8s-school/ktbx/resources"
Expand All @@ -31,7 +33,11 @@ var kindCmd = &cobra.Command{

script := internal.FormatTemplate(resources.KindInstallScript, k)

ExecCmd(script, false)
_, _, err := ExecCmd(script, false)
if err != nil {
slog.Error("Error while installing kind", "error", err)
os.Exit(1)
}
},
}

Expand Down
8 changes: 7 additions & 1 deletion cmd/install_kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package cmd

import (
"fmt"
"log/slog"
"os"

"github.com/k8s-school/ktbx/resources"
"github.com/spf13/cobra"
Expand All @@ -18,7 +20,11 @@ var kubectlCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Install kubectl")

ExecCmd(resources.KubectlInstallScript, false)
_, _, err := ExecCmd(resources.KubectlInstallScript, false)
if err != nil {
slog.Error("Error while installing kubectl", "error", err)
os.Exit(1)
}
},
}

Expand Down
7 changes: 6 additions & 1 deletion cmd/install_olm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package cmd

import (
"log/slog"
"os"

"github.com/k8s-school/ktbx/resources"
"github.com/spf13/cobra"
Expand All @@ -18,7 +19,11 @@ var olmCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
slog.Info("Install OLM")

ExecCmd(resources.OlmInstallScript, false)
_, _, err := ExecCmd(resources.OlmInstallScript, false)
if err != nil {
slog.Error("Error while installing OLM", "error", err)
os.Exit(1)
}
},
}

Expand Down
8 changes: 7 additions & 1 deletion cmd/install_telepresence.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package cmd

import (
"fmt"
"log/slog"
"os"

"github.com/k8s-school/ktbx/resources"
"github.com/spf13/cobra"
Expand All @@ -17,7 +19,11 @@ var telepresenceCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Install telepresence")

ExecCmd(resources.TelepresenceInstallScript, false)
_, _, err := ExecCmd(resources.TelepresenceInstallScript, false)
if err != nil {
slog.Error("Error while installing telepresence", "error", err)
os.Exit(1)
}
},
}

Expand Down
5 changes: 3 additions & 2 deletions cmd/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

const ShellToUse = "bash"

func ExecCmd(command string, interactive bool) (string, string) {
func ExecCmd(command string, interactive bool) (string, string, error) {

var stdoutBuf, stderrBuf bytes.Buffer
if !dryRun {
Expand All @@ -27,12 +27,13 @@ func ExecCmd(command string, interactive bool) (string, string) {
err := cmd.Run()
if err != nil {
slog.Error("cmd.Run() failed", "error", err)
return stdoutBuf.String(), stderrBuf.String(), err
}
// logger.Infof("stdout %v", stdoutBuf)
// logger.Infof("stderr %v", stderrBuf)

} else {
slog.Info("Dry run", "command", command)
}
return stdoutBuf.String(), stderrBuf.String()
return stdoutBuf.String(), stderrBuf.String(), nil
}

0 comments on commit cc185a0

Please sign in to comment.