From 0c10bbb5698f10dba268531dd563af0f602fb07c Mon Sep 17 00:00:00 2001 From: Maidul Islam Date: Tue, 14 Feb 2023 13:43:40 -0800 Subject: [PATCH] send error message to standard error out --- cli/packages/cmd/init.go | 2 +- cli/packages/cmd/secrets.go | 22 +++++----------------- cli/packages/util/helper.go | 12 ++++++------ cli/packages/util/log.go | 6 +++--- 4 files changed, 15 insertions(+), 27 deletions(-) diff --git a/cli/packages/cmd/init.go b/cli/packages/cmd/init.go index 413f61440c..d5d9164bad 100644 --- a/cli/packages/cmd/init.go +++ b/cli/packages/cmd/init.go @@ -56,7 +56,7 @@ var initCmd = &cobra.Command{ workspaces := workspaceResponse.Workspaces if len(workspaces) == 0 { message := fmt.Sprintf("You don't have any projects created in Infisical. You must first create a project at %s", util.INFISICAL_TOKEN_NAME) - util.PrintMessageAndExit(message) + util.PrintErrorMessageAndExit(message) } var workspaceNames []string diff --git a/cli/packages/cmd/secrets.go b/cli/packages/cmd/secrets.go index 823505d7db..dadb699242 100644 --- a/cli/packages/cmd/secrets.go +++ b/cli/packages/cmd/secrets.go @@ -92,15 +92,13 @@ var secretsSetCmd = &cobra.Command{ PreRun: toggleDebug, Args: cobra.MinimumNArgs(1), Run: func(cmd *cobra.Command, args []string) { + util.RequireLocalWorkspaceFile() + environmentName, err := cmd.Flags().GetString("env") if err != nil { util.HandleError(err, "Unable to parse flag") } - if !util.IsSecretEnvironmentValid(environmentName) { - util.PrintMessageAndExit("You have entered a invalid environment name", "Environment names can only be prod, dev, test or staging") - } - workspaceFile, err := util.GetWorkSpaceFromFile() if err != nil { util.HandleError(err, "Unable to get your local config details") @@ -153,11 +151,11 @@ var secretsSetCmd = &cobra.Command{ for _, arg := range args { splitKeyValueFromArg := strings.SplitN(arg, "=", 2) if splitKeyValueFromArg[0] == "" || splitKeyValueFromArg[1] == "" { - util.PrintMessageAndExit("ensure that each secret has a none empty key and value. Modify the input and try again") + util.PrintErrorMessageAndExit("ensure that each secret has a none empty key and value. Modify the input and try again") } if unicode.IsNumber(rune(splitKeyValueFromArg[0][0])) { - util.PrintMessageAndExit("keys of secrets cannot start with a number. Modify the key name(s) and try again") + util.PrintErrorMessageAndExit("keys of secrets cannot start with a number. Modify the key name(s) and try again") } // Key and value from argument @@ -308,7 +306,7 @@ var secretsDeleteCmd = &cobra.Command{ if len(invalidSecretNamesThatDoNotExist) != 0 { message := fmt.Sprintf("secret name(s) [%v] does not exist in your project. To see which secrets exist run [infisical secrets]", strings.Join(invalidSecretNamesThatDoNotExist, ", ")) - util.PrintMessageAndExit(message) + util.PrintErrorMessageAndExit(message) } request := api.BatchDeleteSecretsBySecretIdsRequest{ @@ -337,11 +335,6 @@ func getSecretsByNames(cmd *cobra.Command, args []string) { util.HandleError(err, "Unable to parse flag") } - workspaceFileExists := util.WorkspaceConfigFileExistsInCurrentPath() - if !workspaceFileExists { - util.HandleError(err, "Unable to parse flag") - } - infisicalToken, err := cmd.Flags().GetString("token") if err != nil { util.HandleError(err, "Unable to parse flag") @@ -385,11 +378,6 @@ func generateExampleEnv(cmd *cobra.Command, args []string) { util.HandleError(err, "Unable to parse flag") } - workspaceFileExists := util.WorkspaceConfigFileExistsInCurrentPath() - if !workspaceFileExists { - util.HandleError(err, "Unable to parse flag") - } - infisicalToken, err := cmd.Flags().GetString("token") if err != nil { util.HandleError(err, "Unable to parse flag") diff --git a/cli/packages/util/helper.go b/cli/packages/util/helper.go index 3fd8853c4a..2a7ac7317a 100644 --- a/cli/packages/util/helper.go +++ b/cli/packages/util/helper.go @@ -65,29 +65,29 @@ func RequireLogin() { } if !currentUserDetails.IsUserLoggedIn { - PrintMessageAndExit("You must be logged in to run this command. To login, run [infisical login]") + PrintErrorMessageAndExit("You must be logged in to run this command. To login, run [infisical login]") } if currentUserDetails.LoginExpired { - PrintMessageAndExit("Your login expired, please login in again. To login, run [infisical login]") + PrintErrorMessageAndExit("Your login expired, please login in again. To login, run [infisical login]") } if currentUserDetails.UserCredentials.Email == "" && currentUserDetails.UserCredentials.JTWToken == "" && currentUserDetails.UserCredentials.PrivateKey == "" { - PrintMessageAndExit("One or more of your login details is empty. Please try logging in again via by running [infisical login]") + PrintErrorMessageAndExit("One or more of your login details is empty. Please try logging in again via by running [infisical login]") } } func RequireServiceToken() { serviceToken := os.Getenv(INFISICAL_TOKEN_NAME) if serviceToken == "" { - PrintMessageAndExit("No service token is found in your terminal") + PrintErrorMessageAndExit("No service token is found in your terminal") } } func RequireLocalWorkspaceFile() { workspaceFileExists := WorkspaceConfigFileExistsInCurrentPath() if !workspaceFileExists { - PrintMessageAndExit("It looks you have not yet connected this project to Infisical", "To do so, run [infisical init] then run your command again") + PrintErrorMessageAndExit("It looks you have not yet connected this project to Infisical", "To do so, run [infisical init] then run your command again") } workspaceFile, err := GetWorkSpaceFromFile() @@ -96,7 +96,7 @@ func RequireLocalWorkspaceFile() { } if workspaceFile.WorkspaceId == "" { - PrintMessageAndExit("Your project id is missing in your local config file. Please add it or run again [infisical init]") + PrintErrorMessageAndExit("Your project id is missing in your local config file. Please add it or run again [infisical init]") } } diff --git a/cli/packages/util/log.go b/cli/packages/util/log.go index 68144e0748..4c11322c24 100644 --- a/cli/packages/util/log.go +++ b/cli/packages/util/log.go @@ -31,10 +31,10 @@ func PrintSuccessMessage(message string) { color.New(color.FgGreen).Println(message) } -func PrintMessageAndExit(messages ...string) { +func PrintErrorMessageAndExit(messages ...string) { if len(messages) > 0 { for _, message := range messages { - fmt.Println(message) + fmt.Fprintln(os.Stderr, message) } } @@ -42,5 +42,5 @@ func PrintMessageAndExit(messages ...string) { } func printError(e error) { - color.Red("Hmm, we ran into an error: %v", e) + color.New(color.FgRed).Fprintf(os.Stderr, "Hmm, we ran into an error: %v", e) }