send error message to standard error out

This commit is contained in:
Maidul Islam
2023-02-14 13:43:40 -08:00
parent fba54ae0c6
commit 0c10bbb569
4 changed files with 15 additions and 27 deletions
+1 -1
View File
@@ -56,7 +56,7 @@ var initCmd = &cobra.Command{
workspaces := workspaceResponse.Workspaces workspaces := workspaceResponse.Workspaces
if len(workspaces) == 0 { 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) 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 var workspaceNames []string
+5 -17
View File
@@ -92,15 +92,13 @@ var secretsSetCmd = &cobra.Command{
PreRun: toggleDebug, PreRun: toggleDebug,
Args: cobra.MinimumNArgs(1), Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
util.RequireLocalWorkspaceFile()
environmentName, err := cmd.Flags().GetString("env") environmentName, err := cmd.Flags().GetString("env")
if err != nil { if err != nil {
util.HandleError(err, "Unable to parse flag") 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() workspaceFile, err := util.GetWorkSpaceFromFile()
if err != nil { if err != nil {
util.HandleError(err, "Unable to get your local config details") util.HandleError(err, "Unable to get your local config details")
@@ -153,11 +151,11 @@ var secretsSetCmd = &cobra.Command{
for _, arg := range args { for _, arg := range args {
splitKeyValueFromArg := strings.SplitN(arg, "=", 2) splitKeyValueFromArg := strings.SplitN(arg, "=", 2)
if splitKeyValueFromArg[0] == "" || splitKeyValueFromArg[1] == "" { 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])) { 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 // Key and value from argument
@@ -308,7 +306,7 @@ var secretsDeleteCmd = &cobra.Command{
if len(invalidSecretNamesThatDoNotExist) != 0 { 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, ", ")) 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{ request := api.BatchDeleteSecretsBySecretIdsRequest{
@@ -337,11 +335,6 @@ func getSecretsByNames(cmd *cobra.Command, args []string) {
util.HandleError(err, "Unable to parse flag") 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") infisicalToken, err := cmd.Flags().GetString("token")
if err != nil { if err != nil {
util.HandleError(err, "Unable to parse flag") 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") 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") infisicalToken, err := cmd.Flags().GetString("token")
if err != nil { if err != nil {
util.HandleError(err, "Unable to parse flag") util.HandleError(err, "Unable to parse flag")
+6 -6
View File
@@ -65,29 +65,29 @@ func RequireLogin() {
} }
if !currentUserDetails.IsUserLoggedIn { 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 { 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 == "" { 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() { func RequireServiceToken() {
serviceToken := os.Getenv(INFISICAL_TOKEN_NAME) serviceToken := os.Getenv(INFISICAL_TOKEN_NAME)
if serviceToken == "" { if serviceToken == "" {
PrintMessageAndExit("No service token is found in your terminal") PrintErrorMessageAndExit("No service token is found in your terminal")
} }
} }
func RequireLocalWorkspaceFile() { func RequireLocalWorkspaceFile() {
workspaceFileExists := WorkspaceConfigFileExistsInCurrentPath() workspaceFileExists := WorkspaceConfigFileExistsInCurrentPath()
if !workspaceFileExists { 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() workspaceFile, err := GetWorkSpaceFromFile()
@@ -96,7 +96,7 @@ func RequireLocalWorkspaceFile() {
} }
if workspaceFile.WorkspaceId == "" { 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]")
} }
} }
+3 -3
View File
@@ -31,10 +31,10 @@ func PrintSuccessMessage(message string) {
color.New(color.FgGreen).Println(message) color.New(color.FgGreen).Println(message)
} }
func PrintMessageAndExit(messages ...string) { func PrintErrorMessageAndExit(messages ...string) {
if len(messages) > 0 { if len(messages) > 0 {
for _, message := range messages { for _, message := range messages {
fmt.Println(message) fmt.Fprintln(os.Stderr, message)
} }
} }
@@ -42,5 +42,5 @@ func PrintMessageAndExit(messages ...string) {
} }
func printError(e error) { 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)
} }