From 3fd9cbf7bd2b468e426b53b55551d15df05f8ed9 Mon Sep 17 00:00:00 2001 From: Jaesang Lee Date: Fri, 1 Jul 2022 06:24:04 +0000 Subject: [PATCH] Remove os.Exit code --- cmd/cluster_create.go | 3 +-- cmd/cluster_delete.go | 3 +-- cmd/cluster_list.go | 3 +-- cmd/cluster_show.go | 3 +-- cmd/service_create.go | 3 +-- cmd/service_delete.go | 3 +-- cmd/service_list.go | 3 +-- cmd/show_byoh_agent_guide.go | 14 +++++--------- 8 files changed, 12 insertions(+), 23 deletions(-) diff --git a/cmd/cluster_create.go b/cmd/cluster_create.go index 51b5b8d..8dead39 100644 --- a/cmd/cluster_create.go +++ b/cmd/cluster_create.go @@ -93,8 +93,7 @@ tks cluster create `, r, err := client.CreateCluster(ctx, &data) fmt.Println("Response:\n", r) if err != nil { - fmt.Println("Error:", err) - os.Exit(1) + return fmt.Errorf("Error: %s", err) } else { fmt.Println("Success: The request to create cluster ", args[0], " was accepted.") } diff --git a/cmd/cluster_delete.go b/cmd/cluster_delete.go index cc02afe..0c8293a 100644 --- a/cmd/cluster_delete.go +++ b/cmd/cluster_delete.go @@ -75,8 +75,7 @@ tks cluster delete `, r, err := client.DeleteCluster(ctx, &data) fmt.Println(r) if err != nil { - fmt.Println(err) - os.Exit(1) + return fmt.Errorf("Error: %s", err) } else { fmt.Println("The request to delete cluster ", args[0], " was accepted.") } diff --git a/cmd/cluster_list.go b/cmd/cluster_list.go index 663f9ee..43ea8fd 100644 --- a/cmd/cluster_list.go +++ b/cmd/cluster_list.go @@ -72,8 +72,7 @@ tks cluster list (--long)`, } r, err := client.GetClusters(ctx, &data) if err != nil { - fmt.Println(err) - os.Exit(1) + return fmt.Errorf("Error: %s", err) } else { if len(r.Clusters) == 0 { fmt.Println("No cluster exists for specified contract!") diff --git a/cmd/cluster_show.go b/cmd/cluster_show.go index 7517988..bb2d210 100644 --- a/cmd/cluster_show.go +++ b/cmd/cluster_show.go @@ -73,8 +73,7 @@ tks cluster show `, } r, err := client.GetCluster(ctx, &data) if err != nil { - fmt.Println(err) - os.Exit(1) + return fmt.Errorf("Error: %s", err) } else { printCluster(r) } diff --git a/cmd/service_create.go b/cmd/service_create.go index 222b436..f9d6100 100644 --- a/cmd/service_create.go +++ b/cmd/service_create.go @@ -98,8 +98,7 @@ tks service create --cluster-id --service-name `, } r, err := client.UninstallAppGroups(ctx, &data) if err != nil { - fmt.Println(err) - os.Exit(1) + return fmt.Errorf("Error: %s", err) } else { fmt.Println(r) } diff --git a/cmd/service_list.go b/cmd/service_list.go index 73f24dd..ecc5187 100644 --- a/cmd/service_list.go +++ b/cmd/service_list.go @@ -77,8 +77,7 @@ tks service list (--long)`, } r, err := client.GetAppGroupsByClusterID(ctx, &data) if err != nil { - fmt.Println(err) - os.Exit(1) + return fmt.Errorf("Error: %s", err) } else { long, _ := cmd.Flags().GetBool("long") printAppGroups(r, long) diff --git a/cmd/show_byoh_agent_guide.go b/cmd/show_byoh_agent_guide.go index c284b6a..f3be9aa 100644 --- a/cmd/show_byoh_agent_guide.go +++ b/cmd/show_byoh_agent_guide.go @@ -17,8 +17,6 @@ package cmd import ( "fmt" - "log" - "os" "os/exec" "github.com/spf13/cobra" @@ -41,17 +39,15 @@ Standard reference architecture is as follows. Among these types, the 'worker' nodes might needs to be scaled out based on your application size. `, - Run: func(cmd *cobra.Command, args []string) { + RunE: func(cmd *cobra.Command, args []string) error { nodeType, _ := cmd.Flags().GetString("type") if nodeType == "" { - fmt.Printf("Usage: tks cluster show-byoh-node-agent-guide --type=$NODE_TYPE\n") - os.Exit(1) + return fmt.Errorf("Usage: tks cluster show-byoh-node-agent-guide --type=$NODE_TYPE\n") } if nodeType != "controlplane" && nodeType != "tks" && nodeType != "worker" { - fmt.Printf("Wrong node type '%s': please refer to help message.\n", nodeType) - os.Exit(1) + return fmt.Errorf("Wrong node type '%s': please refer to help message.\n", nodeType) } fmt.Printf("*************************************************\n") @@ -63,7 +59,7 @@ Among these types, the 'worker' nodes might needs to be scaled out based on your cmdStr := "cat ~/.kube/config | base64" out, err := exec.Command("bash", "-c", cmdStr).Output() if err != nil { - log.Fatal(err) + return fmt.Errorf("Error: %s", err) } fmt.Printf("Encoded kubeconfig for MGMT cluster:\n%s\n", string(out)) @@ -98,7 +94,7 @@ That's it! Enjoy BYOH provider! ******************************* ` fmt.Printf(guide_str, nodeType) - + return nil }, }