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
2 changes: 1 addition & 1 deletion pkg/cmd/create/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ var projectCmd = &cobra.Command{
var response apis.CreateProjectResponse
response, err = apis.CreateProjectRequest(projectName, credentials)
if err != nil {
utils.Red.Printf("❌ Error creating project: %v\n", err)
utils.PrintFormattedError("Error creating project", err)
} else {
fmt.Printf("Response: %+v\n", response)
projectID := response.Data.ID
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/describe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ var probeCmd = &cobra.Command{
}
_, option, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
utils.PrintFormattedError("Prompt failed", err)
return
}
probeMode = option
Expand All @@ -90,7 +90,7 @@ var probeCmd = &cobra.Command{
getProbeYAMLRequest.Mode = model.Mode(probeMode)
getProbeYAML, err := apis.GetProbeYAMLRequest(pid, getProbeYAMLRequest, credentials)
if err != nil {
utils.Red.Println(err)
utils.PrintFormattedError("Failed to fetch probe YAML", err)
os.Exit(1)
}
getProbeYAMLData := getProbeYAML.Data.GetProbeYAML
Expand All @@ -104,7 +104,7 @@ var probeCmd = &cobra.Command{
var prettyJSON bytes.Buffer
err = json.Indent(&prettyJSON, jsonData, "", " ")
if err != nil {
utils.Red.Println("❌ Error formatting JSON: " + err.Error())
utils.PrintFormattedError("Error formatting JSON", err)
os.Exit(1)
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/get/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ var GetChaosEnvironmentsCmd = &cobra.Command{
for _, environment := range environmentListData[start:end] {
intTime, err := strconv.ParseInt(environment.CreatedAt, 10, 64)
if err != nil {
fmt.Println("Error converting CreatedAt to int64:", err)
utils.PrintFormattedError("Error converting CreatedAt to int64", err)
continue
}
humanTime := time.Unix(intTime, 0)
Expand Down Expand Up @@ -132,12 +132,12 @@ var GetChaosEnvironmentsCmd = &cobra.Command{
writer.Flush()
intUpdateTime, err := strconv.ParseInt(environmentGetData.UpdatedAt, 10, 64)
if err != nil {
utils.Red.Println("Error converting UpdatedAt to int64:", err)
utils.PrintFormattedError("Error converting UpdatedAt to int64", err)
}
updatedTime := time.Unix(intUpdateTime, 0).String()
intCreatedTime, err := strconv.ParseInt(environmentGetData.CreatedAt, 10, 64)
if err != nil {
utils.Red.Println("Error converting CreatedAt to int64:", err)
utils.PrintFormattedError("Error converting CreatedAt to int64", err)
}
createdTime := time.Unix(intCreatedTime, 0).String()
writer.Flush()
Expand Down
10 changes: 5 additions & 5 deletions pkg/cmd/get/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func getProbeList(projectID string, cmd *cobra.Command, credentials types.Creden
}
_, option, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
utils.PrintFormattedError("Prompt failed", err)
return
}
fmt.Printf("You chose %q\n", option)
Expand All @@ -106,7 +106,7 @@ func getProbeList(projectID string, cmd *cobra.Command, credentials types.Creden

selectedIndex, result, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
utils.PrintFormattedError("Prompt failed", err)
os.Exit(1)
}

Expand Down Expand Up @@ -156,7 +156,7 @@ func getProbeList(projectID string, cmd *cobra.Command, credentials types.Creden
for _, probe := range probes_data[start:end] {
intTime, err := strconv.ParseInt(probe.CreatedAt, 10, 64)
if err != nil {
fmt.Println("Error converting CreatedAt to int64:", err)
utils.PrintFormattedError("Error converting CreatedAt to int64", err)
continue
}
humanTime := time.Unix(intTime, 0)
Expand Down Expand Up @@ -201,12 +201,12 @@ func getProbeDetails(projectID, ProbeID string, credentials types.Credentials) {
writer := tabwriter.NewWriter(os.Stdout, 30, 8, 2, '\t', tabwriter.AlignRight)
intUpdateTime, err := strconv.ParseInt(probeGetData.UpdatedAt, 10, 64)
if err != nil {
utils.Red.Println("Error converting UpdatedAt to int64:", err)
utils.PrintFormattedError("Error converting UpdatedAt to int64", err)
}
updatedTime := time.Unix(intUpdateTime, 0).String()
intCreatedTime, err := strconv.ParseInt(probeGetData.CreatedAt, 10, 64)
if err != nil {
utils.Red.Println("Error converting CreatedAt to int64:", err)
utils.PrintFormattedError("Error converting CreatedAt to int64", err)
}
createdTime := time.Unix(intCreatedTime, 0).String()
utils.White_B.Fprintln(writer, "PROBE DETAILS")
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/save/experiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ var experimentCmd = &cobra.Command{
// Parse experiment manifest and populate chaosExperimentInput
err = utils.ParseExperimentManifest(experimentManifest, &chaosExperimentRequest)
if err != nil {
utils.Red.Println("❌ Error parsing Chaos Experiment manifest: " + err.Error())
utils.PrintFormattedError("Error parsing Chaos Experiment manifest", err)
os.Exit(1)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/update/password.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ var PasswordCmd = &cobra.Command{
} else {
err := errors.New("Unmatched status code: " + string(bodyBytes))
if err != nil {
utils.Red.Println("\nError updating password: ", err)
utils.PrintFormattedError("Error updating password", err)
os.Exit(1)
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/infra_ops/ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func ValidateSAPermissions(namespace string, mode string, kubeconfig *string) {
for i, resource := range resources {
pems[i], err = k8s.CheckSAPermissions(k8s.CheckSAPermissionsParams{Verb: "create", Resource: resource, Print: true, Namespace: namespace}, kubeconfig)
if err != nil {
utils.Red.Println(err)
utils.PrintFormattedError("Permission check failed", err)
}
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/k8s/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ limitations under the License.
package k8s

import (
"fmt"
"os"
"path/filepath"

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

// create the config
config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
if err != nil {
utils.Red.Println("ERROR: ", err.Error())
utils.PrintFormattedError("Failed to build kubernetes config", err)
os.Exit(1)
}

// create the clientset
clientset, err := kubernetes.NewForConfig(config)
if err != nil {
utils.Red.Println("ERROR: ", err.Error())
utils.PrintFormattedError("Failed to create kubernetes clientset", err)
os.Exit(1)
}
return clientset, err
Expand Down
30 changes: 30 additions & 0 deletions pkg/utils/cliutil.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright © 2021 The LitmusChaos Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils

import (
"fmt"
)

// FormatError formats error messages consistently with appropriate emoji and styling
func FormatError(message string, err error) string {
return fmt.Sprintf("❌ %s: %v", message, err)
}

// PrintFormattedError prints a formatted error message to stderr in red color
func PrintFormattedError(message string, err error) {
Red.Println(FormatError(message, err))
}
31 changes: 31 additions & 0 deletions pkg/utils/cliutil_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
Copyright © 2021 The LitmusChaos Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package utils

import (
"errors"
"testing"
)

func TestFormatError(t *testing.T) {
err := errors.New("connection failed")
result := FormatError("Error creating project", err)
expected := "❌ Error creating project: connection failed"

if result != expected {
t.Errorf("FormatError() = %v, want %v", result, expected)
}
}
4 changes: 2 additions & 2 deletions pkg/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ func Scanner() string {
return scanner.Text()
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
PrintFormattedError("Error reading standard input", err)
}
return ""
}
func PrintError(err error) {
if err != nil {
Red.Println(err)
PrintFormattedError("Error", err)
os.Exit(1)
}
}
Expand Down
Loading