From 8bfa459ddec610ad6e4ce5eb50440e9b02e61db4 Mon Sep 17 00:00:00 2001 From: Shivam Purohit Date: Thu, 15 Feb 2024 16:21:03 +0530 Subject: [PATCH 1/2] fix : output flag for get projects (#199) * add get project output flag Signed-off-by: Shivam Purohit * fix:fmt Signed-off-by: Shivam Purohit * remove unused import Signed-off-by: Shivam Purohit --------- Signed-off-by: Shivam Purohit --- pkg/cmd/get/projects.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pkg/cmd/get/projects.go b/pkg/cmd/get/projects.go index 7b3bd2a6..07691714 100644 --- a/pkg/cmd/get/projects.go +++ b/pkg/cmd/get/projects.go @@ -34,8 +34,7 @@ var projectsCmd = &cobra.Command{ credentials, err := utils.GetCredentials(cmd) utils.PrintError(err) - //promptui to ask the user for the output format - outputFormat := "" + outputFormat, _ := cmd.Flags().GetString("output") projects, err := apis.ListProject(credentials) utils.PrintError(err) From 8a139b00fba338127fa8b0aa851421df80d3f9dd Mon Sep 17 00:00:00 2001 From: Kartikay <120778728+kartikaysaxena@users.noreply.github.com> Date: Thu, 15 Feb 2024 16:48:02 +0530 Subject: [PATCH 2/2] fix staticcheck warnings (#191) * fix dependencies Signed-off-by: Kartikay * go mod tidy Signed-off-by: Kartikay * some more fixes Signed-off-by: Kartikay * revert deleted function Signed-off-by: Kartikay * fixed lint Signed-off-by: Kartikay * fix Signed-off-by: Kartikay --------- Signed-off-by: Kartikay --- pkg/apis/auth.go | 4 ++-- pkg/apis/environment/environment.go | 13 +++++++++---- pkg/apis/experiment/experiment.go | 16 ++++++++-------- pkg/apis/infrastructure/infra.go | 15 +++++++++------ pkg/apis/project.go | 8 ++++---- pkg/apis/upgrade.go | 9 +++++---- pkg/cmd/config/view.go | 3 +-- pkg/cmd/connect/infra.go | 4 ++-- pkg/cmd/disconnect/infra.go | 3 ++- pkg/cmd/get/environment.go | 7 ++++--- pkg/cmd/get/infra.go | 2 +- pkg/cmd/get/projects.go | 7 ++++--- pkg/cmd/list/environment.go | 9 +++++---- pkg/cmd/root/root.go | 3 +-- pkg/cmd/run/experiment.go | 5 +++-- pkg/cmd/save/experiment.go | 6 ++++-- pkg/config/ops.go | 9 ++++----- pkg/infra_ops/ops.go | 9 +++++---- pkg/k8s/operations.go | 6 +++--- pkg/utils/experiment.go | 19 +++++-------------- pkg/utils/reader.go | 4 ++-- 21 files changed, 83 insertions(+), 78 deletions(-) diff --git a/pkg/apis/auth.go b/pkg/apis/auth.go index 5b2c3b42..027cb7ed 100644 --- a/pkg/apis/auth.go +++ b/pkg/apis/auth.go @@ -18,7 +18,7 @@ package apis import ( "encoding/json" "errors" - "io/ioutil" + "io" "net/http" "github.com/litmuschaos/litmusctl/pkg/utils" @@ -47,7 +47,7 @@ func Auth(input types.AuthInput) (types.AuthResponse, error) { return types.AuthResponse{}, err } - bodyBytes, err := ioutil.ReadAll(resp.Body) + bodyBytes, err := io.ReadAll(resp.Body) if err != nil { return types.AuthResponse{}, err } diff --git a/pkg/apis/environment/environment.go b/pkg/apis/environment/environment.go index 4721324c..2f07cd1b 100644 --- a/pkg/apis/environment/environment.go +++ b/pkg/apis/environment/environment.go @@ -3,12 +3,13 @@ package environment import ( "encoding/json" "errors" + "io" + "net/http" + models "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" "github.com/litmuschaos/litmusctl/pkg/apis" "github.com/litmuschaos/litmusctl/pkg/types" "github.com/litmuschaos/litmusctl/pkg/utils" - "io/ioutil" - "net/http" ) // CreateEnvironment connects the Infra with the given details @@ -19,12 +20,16 @@ func CreateEnvironment(pid string, request models.CreateEnvironmentRequest, cred gqlReq.Variables.Request = request query, err := json.Marshal(gqlReq) + if err != nil { + return CreateEnvironmentResponse{}, errors.New("Error in Creating Chaos Infrastructure: " + err.Error()) + } + resp, err := apis.SendRequest(apis.SendRequestParams{Endpoint: cred.ServerEndpoint + utils.GQLAPIPath, Token: cred.Token}, query, string(types.Post)) if err != nil { return CreateEnvironmentResponse{}, errors.New("Error in Creating Chaos Infrastructure: " + err.Error()) } - bodyBytes, err := ioutil.ReadAll(resp.Body) + bodyBytes, err := io.ReadAll(resp.Body) defer resp.Body.Close() if err != nil { return CreateEnvironmentResponse{}, errors.New("Error in Creating Chaos Environment: " + err.Error()) @@ -68,7 +73,7 @@ func GetEnvironmentList(pid string, cred types.Credentials) (ListEnvironmentData if err != nil { return ListEnvironmentData{}, errors.New("Error in Getting Chaos Environment List: " + err.Error()) } - bodyBytes, err := ioutil.ReadAll(resp.Body) + bodyBytes, err := io.ReadAll(resp.Body) defer resp.Body.Close() if err != nil { return ListEnvironmentData{}, errors.New("Error in Getting Chaos Environment List: " + err.Error()) diff --git a/pkg/apis/experiment/experiment.go b/pkg/apis/experiment/experiment.go index 3b866b04..57352408 100644 --- a/pkg/apis/experiment/experiment.go +++ b/pkg/apis/experiment/experiment.go @@ -18,7 +18,7 @@ package experiment import ( "encoding/json" "errors" - "io/ioutil" + "io" "net/http" "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" @@ -54,7 +54,7 @@ func CreateExperiment(pid string, requestData model.SaveChaosExperimentRequest, return RunExperimentResponse{}, err } - bodyBytes, err := ioutil.ReadAll(resp.Body) + bodyBytes, err := io.ReadAll(resp.Body) defer resp.Body.Close() if err != nil { @@ -87,7 +87,7 @@ func CreateExperiment(pid string, requestData model.SaveChaosExperimentRequest, return RunExperimentResponse{}, errors.New("Error in Running Chaos Experiment: " + err.Error()) } - bodyBytes, err = ioutil.ReadAll(resp.Body) + bodyBytes, err = io.ReadAll(resp.Body) defer resp.Body.Close() if err != nil { return RunExperimentResponse{}, errors.New("Error in Running Chaos Experiment: " + err.Error()) @@ -135,7 +135,7 @@ func SaveExperiment(pid string, requestData model.SaveChaosExperimentRequest, cr return SaveExperimentData{}, err } - bodyBytes, err := ioutil.ReadAll(resp.Body) + bodyBytes, err := io.ReadAll(resp.Body) defer resp.Body.Close() if err != nil { @@ -173,7 +173,7 @@ func RunExperiment(pid string, eid string, cred types.Credentials) (RunExperimen return RunExperimentResponse{}, errors.New("Error in Running Chaos Experiment: " + err.Error()) } - bodyBytes, err := ioutil.ReadAll(resp.Body) + bodyBytes, err := io.ReadAll(resp.Body) defer resp.Body.Close() if err != nil { return RunExperimentResponse{}, errors.New("Error in Running Chaos Experiment: " + err.Error()) @@ -222,7 +222,7 @@ func GetExperimentList(pid string, in model.ListExperimentRequest, cred types.Cr return ExperimentListData{}, err } - bodyBytes, err := ioutil.ReadAll(resp.Body) + bodyBytes, err := io.ReadAll(resp.Body) defer resp.Body.Close() if err != nil { return ExperimentListData{}, err @@ -272,7 +272,7 @@ func GetExperimentRunsList(pid string, in model.ListExperimentRunRequest, cred t return ExperimentRunListData{}, err } - bodyBytes, err := ioutil.ReadAll(resp.Body) + bodyBytes, err := io.ReadAll(resp.Body) defer resp.Body.Close() if err != nil { return ExperimentRunListData{}, err @@ -324,7 +324,7 @@ func DeleteChaosExperiment(projectID string, experimentID *string, cred types.Cr return DeleteChaosExperimentData{}, err } - bodyBytes, err := ioutil.ReadAll(resp.Body) + bodyBytes, err := io.ReadAll(resp.Body) defer resp.Body.Close() if err != nil { return DeleteChaosExperimentData{}, err diff --git a/pkg/apis/infrastructure/infra.go b/pkg/apis/infrastructure/infra.go index 195a3108..43ffc85e 100644 --- a/pkg/apis/infrastructure/infra.go +++ b/pkg/apis/infrastructure/infra.go @@ -19,7 +19,7 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/http" "github.com/litmuschaos/litmusctl/pkg/apis" @@ -45,7 +45,7 @@ func GetInfraList(c types.Credentials, pid string, request models.ListInfraReque return InfraData{}, err } - bodyBytes, err := ioutil.ReadAll(resp.Body) + bodyBytes, err := io.ReadAll(resp.Body) defer resp.Body.Close() if err != nil { @@ -97,12 +97,16 @@ func ConnectInfra(infra types.Infra, cred types.Credentials) (InfraConnectionDat } query, err := json.Marshal(gqlReq) + if err != nil { + return InfraConnectionData{}, errors.New("Error in registering Chaos Infrastructure: " + err.Error()) + } + resp, err := apis.SendRequest(apis.SendRequestParams{Endpoint: cred.ServerEndpoint + utils.GQLAPIPath, Token: cred.Token}, query, string(types.Post)) if err != nil { return InfraConnectionData{}, errors.New("Error in registering Chaos Infrastructure: " + err.Error()) } - bodyBytes, err := ioutil.ReadAll(resp.Body) + bodyBytes, err := io.ReadAll(resp.Body) defer resp.Body.Close() if err != nil { return InfraConnectionData{}, errors.New("Error in registering Chaos Infrastructure: " + err.Error()) @@ -167,7 +171,7 @@ func DisconnectInfra(projectID string, infraID string, cred types.Credentials) ( return DisconnectInfraData{}, err } - bodyBytes, err := ioutil.ReadAll(resp.Body) + bodyBytes, err := io.ReadAll(resp.Body) defer resp.Body.Close() if err != nil { return DisconnectInfraData{}, err @@ -190,7 +194,6 @@ func DisconnectInfra(projectID string, infraID string, cred types.Credentials) ( } } -// GetServerVersion fetches the GQL server version func GetServerVersion(endpoint string) (ServerVersionResponse, error) { var gqlReq ServerVersionRequest var err error @@ -210,7 +213,7 @@ func GetServerVersion(endpoint string) (ServerVersionResponse, error) { if err != nil { return ServerVersionResponse{}, err } - bodyBytes, err := ioutil.ReadAll(resp.Body) + bodyBytes, err := io.ReadAll(resp.Body) defer resp.Body.Close() if err != nil { return ServerVersionResponse{}, err diff --git a/pkg/apis/project.go b/pkg/apis/project.go index ba0fb996..58b85ee7 100644 --- a/pkg/apis/project.go +++ b/pkg/apis/project.go @@ -18,7 +18,7 @@ package apis import ( "encoding/json" "errors" - "io/ioutil" + "io" "net/http" "github.com/golang-jwt/jwt" @@ -56,7 +56,7 @@ func CreateProjectRequest(projectName string, cred types.Credentials) (CreatePro return CreateProjectResponse{}, err } - bodyBytes, err := ioutil.ReadAll(resp.Body) + bodyBytes, err := io.ReadAll(resp.Body) if err != nil { return CreateProjectResponse{}, err } @@ -100,7 +100,7 @@ func ListProject(cred types.Credentials) (listProjectResponse, error) { return listProjectResponse{}, err } - bodyBytes, err := ioutil.ReadAll(resp.Body) + bodyBytes, err := io.ReadAll(resp.Body) if err != nil { return listProjectResponse{}, err } @@ -162,7 +162,7 @@ func GetProjectDetails(c types.Credentials) (ProjectDetails, error) { return ProjectDetails{}, err } - bodyBytes, err := ioutil.ReadAll(resp.Body) + bodyBytes, err := io.ReadAll(resp.Body) defer resp.Body.Close() if err != nil { return ProjectDetails{}, err diff --git a/pkg/apis/upgrade.go b/pkg/apis/upgrade.go index 2a1e4bc8..46c590cc 100644 --- a/pkg/apis/upgrade.go +++ b/pkg/apis/upgrade.go @@ -6,8 +6,9 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" + "io" "net/http" + "os" "github.com/litmuschaos/litmusctl/pkg/k8s" "github.com/litmuschaos/litmusctl/pkg/types" @@ -54,7 +55,7 @@ func UpgradeInfra(c context.Context, cred types.Credentials, projectID string, i return "", err } - bodyBytes, err := ioutil.ReadAll(resp.Body) + bodyBytes, err := io.ReadAll(resp.Body) if err != nil { return "", err } @@ -81,7 +82,7 @@ func UpgradeInfra(c context.Context, cred types.Credentials, projectID string, i return "", err } - bodyBytes, err = ioutil.ReadAll(resp.Body) + bodyBytes, err = io.ReadAll(resp.Body) if err != nil { return "", err } @@ -136,7 +137,7 @@ func UpgradeInfra(c context.Context, cred types.Credentials, projectID string, i cobra.CheckErr(err) configMapString = metadata.String() + configMapString - err = ioutil.WriteFile(home+"/backupSubscriberConfig.yaml", []byte(configMapString), 0644) + err = os.WriteFile(home+"/backupSubscriberConfig.yaml", []byte(configMapString), 0644) if err != nil { return "Error creating backup for subscriber config: ", err } diff --git a/pkg/cmd/config/view.go b/pkg/cmd/config/view.go index 6c585825..2893b196 100644 --- a/pkg/cmd/config/view.go +++ b/pkg/cmd/config/view.go @@ -17,7 +17,6 @@ package config import ( "fmt" - "io/ioutil" "github.com/litmuschaos/litmusctl/pkg/utils" @@ -41,7 +40,7 @@ var viewCmd = &cobra.Command{ os.Exit(1) } - data, err := ioutil.ReadFile(configFilePath) + data, err := os.ReadFile(configFilePath) utils.PrintError(err) //Printing the config map diff --git a/pkg/cmd/connect/infra.go b/pkg/cmd/connect/infra.go index 32750352..fa247e32 100644 --- a/pkg/cmd/connect/infra.go +++ b/pkg/cmd/connect/infra.go @@ -105,7 +105,7 @@ var infraCmd = &cobra.Command{ os.Exit(1) } - newInfra.EnvironmentID, err = cmd.Flags().GetString("environmentID") + newInfra.EnvironmentID, _ = cmd.Flags().GetString("environmentID") if newInfra.EnvironmentID == "" { utils.Red.Print("Error: --environment flag is empty") os.Exit(1) @@ -230,7 +230,7 @@ var infraCmd = &cobra.Command{ } if infra.Data.RegisterInfraDetails.Token == "" { - utils.Red.Println("\n❌ failed to get the Infra registration token: " + "\n") + utils.Red.Println("\n❌ failed to get the Infra registration token: ") os.Exit(1) } diff --git a/pkg/cmd/disconnect/infra.go b/pkg/cmd/disconnect/infra.go index e6ee9eba..83be1ef1 100644 --- a/pkg/cmd/disconnect/infra.go +++ b/pkg/cmd/disconnect/infra.go @@ -17,10 +17,11 @@ package disconnect import ( "fmt" - "github.com/litmuschaos/litmusctl/pkg/apis/infrastructure" "os" "strings" + "github.com/litmuschaos/litmusctl/pkg/apis/infrastructure" + "github.com/litmuschaos/litmusctl/pkg/apis" "github.com/litmuschaos/litmusctl/pkg/utils" diff --git a/pkg/cmd/get/environment.go b/pkg/cmd/get/environment.go index f20278d4..f2ba53a7 100644 --- a/pkg/cmd/get/environment.go +++ b/pkg/cmd/get/environment.go @@ -18,14 +18,15 @@ package get import ( "fmt" - "github.com/litmuschaos/litmusctl/pkg/apis/environment" - "github.com/litmuschaos/litmusctl/pkg/utils" - "github.com/spf13/cobra" "os" "strconv" "strings" "text/tabwriter" "time" + + "github.com/litmuschaos/litmusctl/pkg/apis/environment" + "github.com/litmuschaos/litmusctl/pkg/utils" + "github.com/spf13/cobra" ) var ChaosEnvironmentCmd = &cobra.Command{ diff --git a/pkg/cmd/get/infra.go b/pkg/cmd/get/infra.go index f29992d7..7291b4fb 100644 --- a/pkg/cmd/get/infra.go +++ b/pkg/cmd/get/infra.go @@ -61,7 +61,7 @@ var InfraCmd = &cobra.Command{ } } - output, err := cmd.Flags().GetString("output") + output, _ := cmd.Flags().GetString("output") switch output { case "json": diff --git a/pkg/cmd/get/projects.go b/pkg/cmd/get/projects.go index 07691714..6f98353c 100644 --- a/pkg/cmd/get/projects.go +++ b/pkg/cmd/get/projects.go @@ -16,13 +16,14 @@ limitations under the License. package get import ( + "os" + "text/tabwriter" + "time" + "github.com/litmuschaos/litmusctl/pkg/apis" "github.com/litmuschaos/litmusctl/pkg/utils" "github.com/manifoldco/promptui" "github.com/spf13/cobra" - "os" - "text/tabwriter" - "time" ) // projectCmd represents the project command diff --git a/pkg/cmd/list/environment.go b/pkg/cmd/list/environment.go index 0daec8fa..5acf1538 100644 --- a/pkg/cmd/list/environment.go +++ b/pkg/cmd/list/environment.go @@ -18,15 +18,16 @@ package list import ( "fmt" - "github.com/litmuschaos/litmusctl/pkg/apis/environment" - "github.com/litmuschaos/litmusctl/pkg/utils" - "github.com/manifoldco/promptui" - "github.com/spf13/cobra" "os" "strconv" "strings" "text/tabwriter" "time" + + "github.com/litmuschaos/litmusctl/pkg/apis/environment" + "github.com/litmuschaos/litmusctl/pkg/utils" + "github.com/manifoldco/promptui" + "github.com/spf13/cobra" ) var ListChaosEnvironmentCmd = &cobra.Command{ diff --git a/pkg/cmd/root/root.go b/pkg/cmd/root/root.go index 03c8abad..d4a69282 100644 --- a/pkg/cmd/root/root.go +++ b/pkg/cmd/root/root.go @@ -19,7 +19,6 @@ import ( "crypto/tls" "crypto/x509" "fmt" - "io/ioutil" "net/http" "os" @@ -110,7 +109,7 @@ func initConfig() { if config2.SkipSSLVerify { http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true} } else if config2.CACert != "" { - caCert, err := ioutil.ReadFile(config2.CACert) + caCert, err := os.ReadFile(config2.CACert) cobra.CheckErr(err) caCertPool := x509.NewCertPool() caCertPool.AppendCertsFromPEM(caCert) diff --git a/pkg/cmd/run/experiment.go b/pkg/cmd/run/experiment.go index 179e5d8e..b683caf9 100644 --- a/pkg/cmd/run/experiment.go +++ b/pkg/cmd/run/experiment.go @@ -17,12 +17,13 @@ package run import ( "fmt" + "os" + "strings" + "github.com/litmuschaos/litmusctl/pkg/apis" "github.com/litmuschaos/litmusctl/pkg/apis/experiment" "github.com/litmuschaos/litmusctl/pkg/utils" "github.com/spf13/cobra" - "os" - "strings" ) // experimentCmd represents the project command diff --git a/pkg/cmd/save/experiment.go b/pkg/cmd/save/experiment.go index 8ce9e248..0b239a87 100644 --- a/pkg/cmd/save/experiment.go +++ b/pkg/cmd/save/experiment.go @@ -17,10 +17,12 @@ package save import ( "fmt" - models "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" - "github.com/litmuschaos/litmusctl/pkg/apis/experiment" "os" "strings" + + models "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" + "github.com/litmuschaos/litmusctl/pkg/apis/experiment" + //"time" //"github.com/gorhill/cronexpr" diff --git a/pkg/config/ops.go b/pkg/config/ops.go index b1a97674..7f02131c 100644 --- a/pkg/config/ops.go +++ b/pkg/config/ops.go @@ -17,7 +17,6 @@ package config import ( "errors" - "io/ioutil" "os" "github.com/litmuschaos/litmusctl/pkg/types" @@ -41,7 +40,7 @@ func CreateNewLitmusCtlConfig(filename string, config types.LitmuCtlConfig) erro return err } - err = ioutil.WriteFile(filename, configByte, 0644) + err = os.WriteFile(filename, configByte, 0644) if err != nil { return err } @@ -59,7 +58,7 @@ func FileExists(filename string) bool { } func GetFileLength(filename string) (int, error) { - data, err := ioutil.ReadFile(filename) + data, err := os.ReadFile(filename) if err != nil { return -1, err } @@ -68,7 +67,7 @@ func GetFileLength(filename string) (int, error) { } func YamltoObject(filename string) (types.LitmuCtlConfig, error) { - data, err := ioutil.ReadFile(filename) + data, err := os.ReadFile(filename) if err != nil { return types.LitmuCtlConfig{}, errors.New("File reading error " + err.Error()) } @@ -166,7 +165,7 @@ func writeObjToFile(obj types.LitmuCtlConfig, filename string) error { return err } - err = ioutil.WriteFile(filename, byteObj, 0644) + err = os.WriteFile(filename, byteObj, 0644) if err != nil { return err } diff --git a/pkg/infra_ops/ops.go b/pkg/infra_ops/ops.go index 6e15f5c1..5f4c5384 100644 --- a/pkg/infra_ops/ops.go +++ b/pkg/infra_ops/ops.go @@ -17,13 +17,14 @@ package infra_ops import ( "fmt" - "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" - "github.com/litmuschaos/litmusctl/pkg/apis/environment" - "github.com/litmuschaos/litmusctl/pkg/apis/infrastructure" "os" "strconv" "strings" + "github.com/litmuschaos/litmus/chaoscenter/graphql/server/graph/model" + "github.com/litmuschaos/litmusctl/pkg/apis/environment" + "github.com/litmuschaos/litmusctl/pkg/apis/infrastructure" + "github.com/litmuschaos/litmusctl/pkg/apis" "github.com/litmuschaos/litmusctl/pkg/k8s" "github.com/litmuschaos/litmusctl/pkg/types" @@ -116,7 +117,7 @@ INFRA_NAME: } // Check if Chaos Infra with the given name already exists - isInfraExist, err, infra := ValidateInfraNameExists(newInfra.InfraName, pid, c) + isInfraExist, _, infra := ValidateInfraNameExists(newInfra.InfraName, pid, c) if isInfraExist { PrintExistingInfra(infra) diff --git a/pkg/k8s/operations.go b/pkg/k8s/operations.go index 0417d755..ae163fdb 100644 --- a/pkg/k8s/operations.go +++ b/pkg/k8s/operations.go @@ -21,7 +21,7 @@ import ( "errors" "flag" "fmt" - "io/ioutil" + "io" "log" "net/http" "os" @@ -304,11 +304,11 @@ func ApplyYaml(params ApplyYamlParams, kubeconfig string, isLocal bool) (output return "", err } defer resp.Body.Close() - resp_body, err := ioutil.ReadAll(resp.Body) + resp_body, err := io.ReadAll(resp.Body) if err != nil { return "", err } - err = ioutil.WriteFile("chaos-infra-manifest.yaml", resp_body, 0644) + err = os.WriteFile("chaos-infra-manifest.yaml", resp_body, 0644) if err != nil { return "", err } diff --git a/pkg/utils/experiment.go b/pkg/utils/experiment.go index d65d04e5..09d38c75 100644 --- a/pkg/utils/experiment.go +++ b/pkg/utils/experiment.go @@ -19,9 +19,9 @@ import ( "encoding/json" "errors" "fmt" - "io/ioutil" "math/rand" "net/url" + "os" "regexp" "strconv" "strings" @@ -44,7 +44,7 @@ func ParseExperimentManifest(file string, chaosWorkFlowRequest *model.SaveChaosE // Read the manifest file. parsedURL, ok := url.ParseRequestURI(file) if ok != nil || !(parsedURL.Scheme == "http" || parsedURL.Scheme == "https") { - body, err = ioutil.ReadFile(file) + body, err = os.ReadFile(file) } else { body, err = ReadRemoteFile(file) } @@ -133,23 +133,14 @@ func ParseExperimentManifest(file string, chaosWorkFlowRequest *model.SaveChaosE return nil } -// Helper function to check the presence of a string in a slice -func sliceContains(s []string, e string) bool { - for _, a := range s { - if a == e { - return true - } - } - return false -} - // Helper function to generate a random 8 char string - used for workflow name postfix func generateRandomString() string { - rand.Seed(time.Now().UnixNano()) + source := rand.NewSource(time.Now().UnixNano()) + randomGenerator := rand.New(source) var letters = []rune("abcdefghijklmnopqrstuvxyz0123456789") b := make([]rune, 5) for i := range b { - b[i] = letters[rand.Intn(len(letters))] + b[i] = letters[randomGenerator.Intn(len(letters))] } return string(b) } diff --git a/pkg/utils/reader.go b/pkg/utils/reader.go index 2740a0d8..84b8b373 100644 --- a/pkg/utils/reader.go +++ b/pkg/utils/reader.go @@ -17,7 +17,7 @@ package utils import ( "encoding/json" - "io/ioutil" + "io" "net/http" "sigs.k8s.io/yaml" @@ -45,7 +45,7 @@ func ReadRemoteFile(url string) ([]byte, error) { resp, err := http.Get(url) var body []byte if err == nil { - body, err = ioutil.ReadAll(resp.Body) + body, err = io.ReadAll(resp.Body) resp.Body.Close() } return body, err