Skip to content

Commit 066056d

Browse files
committed
refactor: reuse PrintFormattedError helper for consistent error logging
Signed-off-by: ritoban23 <[email protected]>
1 parent 6726388 commit 066056d

File tree

7 files changed

+32
-31
lines changed

7 files changed

+32
-31
lines changed

pkg/cmd/describe/probe.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,18 @@ var probeCmd = &cobra.Command{
7979
Label: "Please select the probe mode ?",
8080
Items: []string{"SOT", "EOT", "Edge", "Continuous", "OnChaos"},
8181
}
82-
_, option, err := prompt.Run()
83-
if err != nil {
84-
fmt.Printf("Prompt failed %v\n", err)
85-
return
86-
}
82+
_, option, err := prompt.Run()
83+
if err != nil {
84+
utils.PrintFormattedError("Prompt failed", err)
85+
return
86+
}
8787
probeMode = option
8888
fmt.Printf("You chose %q\n", option)
8989
}
9090
getProbeYAMLRequest.Mode = model.Mode(probeMode)
9191
getProbeYAML, err := apis.GetProbeYAMLRequest(pid, getProbeYAMLRequest, credentials)
9292
if err != nil {
93-
utils.Red.Println(err)
93+
utils.PrintFormattedError("Failed to fetch probe YAML", err)
9494
os.Exit(1)
9595
}
9696
getProbeYAMLData := getProbeYAML.Data.GetProbeYAML
@@ -104,7 +104,7 @@ var probeCmd = &cobra.Command{
104104
var prettyJSON bytes.Buffer
105105
err = json.Indent(&prettyJSON, jsonData, "", " ")
106106
if err != nil {
107-
utils.Red.Println("❌ Error formatting JSON: " + err.Error())
107+
utils.PrintFormattedError("Error formatting JSON", err)
108108
os.Exit(1)
109109
}
110110

pkg/cmd/get/environments.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ var GetChaosEnvironmentsCmd = &cobra.Command{
9090
for _, environment := range environmentListData[start:end] {
9191
intTime, err := strconv.ParseInt(environment.CreatedAt, 10, 64)
9292
if err != nil {
93-
fmt.Println("Error converting CreatedAt to int64:", err)
93+
utils.PrintFormattedError("Error converting CreatedAt to int64", err)
9494
continue
9595
}
9696
humanTime := time.Unix(intTime, 0)
@@ -132,12 +132,12 @@ var GetChaosEnvironmentsCmd = &cobra.Command{
132132
writer.Flush()
133133
intUpdateTime, err := strconv.ParseInt(environmentGetData.UpdatedAt, 10, 64)
134134
if err != nil {
135-
utils.Red.Println("Error converting UpdatedAt to int64:", err)
135+
utils.PrintFormattedError("Error converting UpdatedAt to int64", err)
136136
}
137137
updatedTime := time.Unix(intUpdateTime, 0).String()
138138
intCreatedTime, err := strconv.ParseInt(environmentGetData.CreatedAt, 10, 64)
139139
if err != nil {
140-
utils.Red.Println("Error converting CreatedAt to int64:", err)
140+
utils.PrintFormattedError("Error converting CreatedAt to int64", err)
141141
}
142142
createdTime := time.Unix(intCreatedTime, 0).String()
143143
writer.Flush()

pkg/cmd/get/probe.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ func getProbeList(projectID string, cmd *cobra.Command, credentials types.Creden
8484
Label: "Do you want to enable advance filter probes?",
8585
Items: []string{"Yes", "No"},
8686
}
87-
_, option, err := prompt.Run()
88-
if err != nil {
89-
fmt.Printf("Prompt failed %v\n", err)
90-
return
91-
}
87+
_, option, err := prompt.Run()
88+
if err != nil {
89+
utils.PrintFormattedError("Prompt failed", err)
90+
return
91+
}
9292
fmt.Printf("You chose %q\n", option)
9393

9494
if option == "Yes" {
@@ -106,7 +106,7 @@ func getProbeList(projectID string, cmd *cobra.Command, credentials types.Creden
106106

107107
selectedIndex, result, err := prompt.Run()
108108
if err != nil {
109-
fmt.Printf("Prompt failed %v\n", err)
109+
utils.PrintFormattedError("Prompt failed", err)
110110
os.Exit(1)
111111
}
112112

@@ -153,12 +153,12 @@ func getProbeList(projectID string, cmd *cobra.Command, credentials types.Creden
153153
if end > totalProbes {
154154
end = totalProbes
155155
}
156-
for _, probe := range probes_data[start:end] {
157-
intTime, err := strconv.ParseInt(probe.CreatedAt, 10, 64)
158-
if err != nil {
159-
fmt.Println("Error converting CreatedAt to int64:", err)
160-
continue
161-
}
156+
for _, probe := range probes_data[start:end] {
157+
intTime, err := strconv.ParseInt(probe.CreatedAt, 10, 64)
158+
if err != nil {
159+
utils.PrintFormattedError("Error converting CreatedAt to int64", err)
160+
continue
161+
}
162162
humanTime := time.Unix(intTime, 0)
163163
var probeReferencedBy int
164164
if probe.ReferencedBy != nil {
@@ -201,12 +201,12 @@ func getProbeDetails(projectID, ProbeID string, credentials types.Credentials) {
201201
writer := tabwriter.NewWriter(os.Stdout, 30, 8, 2, '\t', tabwriter.AlignRight)
202202
intUpdateTime, err := strconv.ParseInt(probeGetData.UpdatedAt, 10, 64)
203203
if err != nil {
204-
utils.Red.Println("Error converting UpdatedAt to int64:", err)
204+
utils.PrintFormattedError("Error converting UpdatedAt to int64", err)
205205
}
206206
updatedTime := time.Unix(intUpdateTime, 0).String()
207207
intCreatedTime, err := strconv.ParseInt(probeGetData.CreatedAt, 10, 64)
208208
if err != nil {
209-
utils.Red.Println("Error converting CreatedAt to int64:", err)
209+
utils.PrintFormattedError("Error converting CreatedAt to int64", err)
210210
}
211211
createdTime := time.Unix(intCreatedTime, 0).String()
212212
utils.White_B.Fprintln(writer, "PROBE DETAILS")

pkg/cmd/update/password.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ var PasswordCmd = &cobra.Command{
9999
} else {
100100
err := errors.New("Unmatched status code: " + string(bodyBytes))
101101
if err != nil {
102-
utils.Red.Println("\nError updating password: ", err)
102+
utils.PrintFormattedError("Error updating password", err)
103103
os.Exit(1)
104104
}
105105
}

pkg/infra_ops/ops.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func ValidateSAPermissions(namespace string, mode string, kubeconfig *string) {
269269
for i, resource := range resources {
270270
pems[i], err = k8s.CheckSAPermissions(k8s.CheckSAPermissionsParams{Verb: "create", Resource: resource, Print: true, Namespace: namespace}, kubeconfig)
271271
if err != nil {
272-
utils.Red.Println(err)
272+
utils.PrintFormattedError("Permission check failed", err)
273273
}
274274
}
275275

pkg/k8s/client.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616
package k8s
1717

1818
import (
19+
"fmt"
1920
"os"
2021
"path/filepath"
2122

@@ -34,22 +35,22 @@ func ClientSet(kubeconfig *string) (*kubernetes.Clientset, error) {
3435
kcfg := filepath.Join(home, ".kube", "config")
3536
kubeconfig = &kcfg
3637
} else {
37-
utils.Red.Println("ERROR: Clientset generation failed!")
38+
utils.PrintFormattedError("Clientset generation failed", fmt.Errorf("home directory not found"))
3839
os.Exit(1)
3940
}
4041
}
4142

4243
// create the config
4344
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
4445
if err != nil {
45-
utils.Red.Println("ERROR: ", err.Error())
46+
utils.PrintFormattedError("Failed to build kubernetes config", err)
4647
os.Exit(1)
4748
}
4849

4950
// create the clientset
5051
clientset, err := kubernetes.NewForConfig(config)
5152
if err != nil {
52-
utils.Red.Println("ERROR: ", err.Error())
53+
utils.PrintFormattedError("Failed to create kubernetes clientset", err)
5354
os.Exit(1)
5455
}
5556
return clientset, err

pkg/utils/common.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ func Scanner() string {
4747
return scanner.Text()
4848
}
4949
if err := scanner.Err(); err != nil {
50-
fmt.Fprintln(os.Stderr, "reading standard input:", err)
50+
PrintFormattedError("Error reading standard input", err)
5151
}
5252
return ""
5353
}
5454
func PrintError(err error) {
5555
if err != nil {
56-
Red.Println(err)
56+
PrintFormattedError("Error", err)
5757
os.Exit(1)
5858
}
5959
}

0 commit comments

Comments
 (0)