2
0
Эх сурвалжийг харах

add porter apply validate command

Mohammed Nafees 3 жил өмнө
parent
commit
f83c3961bf
1 өөрчлөгдсөн 45 нэмэгдсэн , 12 устгасан
  1. 45 12
      cli/cmd/apply.go

+ 45 - 12
cli/cmd/apply.go

@@ -72,32 +72,43 @@ applying a configuration:
 	},
 }
 
+// applyValidateCmd represents the "porter apply validate" command when called
+// with a porter.yaml file as an argument
+var applyValidateCmd = &cobra.Command{
+	Use:   "validate",
+	Short: "Validates a porter.yaml",
+	Run: func(*cobra.Command, []string) {
+		err := applyValidate()
+
+		if err != nil {
+			color.New(color.FgRed).Printf("Error: %s\n", err.Error())
+			os.Exit(1)
+		}
+	},
+}
+
 var porterYAML string
 
 func init() {
 	rootCmd.AddCommand(applyCmd)
 
-	applyCmd.Flags().StringVarP(&porterYAML, "file", "f", "", "path to porter.yaml")
+	applyCmd.AddCommand(applyValidateCmd)
+
+	applyCmd.PersistentFlags().StringVarP(&porterYAML, "file", "f", "", "path to porter.yaml")
 	applyCmd.MarkFlagRequired("file")
 }
 
 func apply(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string) error {
-	fileBytes, err := ioutil.ReadFile(porterYAML)
+	err := applyValidate()
 
 	if err != nil {
-		return fmt.Errorf("error reading porter.yaml: %w", err)
+		return err
 	}
 
-	validationErrors := previewInt.Validate(string(fileBytes))
-
-	if len(validationErrors) > 0 {
-		errString := "the following error(s) were found while validating the porter.yaml file:"
-
-		for _, err := range validationErrors {
-			errString += "\n- " + strings.ReplaceAll(err.Error(), "\n\n*", "\n  *")
-		}
+	fileBytes, err := ioutil.ReadFile(porterYAML)
 
-		return fmt.Errorf(errString)
+	if err != nil {
+		return fmt.Errorf("error reading porter.yaml: %w", err)
 	}
 
 	resGroup, err := parser.ParseRawBytes(fileBytes)
@@ -147,6 +158,28 @@ func apply(_ *types.GetAuthenticatedUserResponse, client *api.Client, _ []string
 	})
 }
 
+func applyValidate() error {
+	fileBytes, err := ioutil.ReadFile(porterYAML)
+
+	if err != nil {
+		return fmt.Errorf("error reading porter.yaml: %w", err)
+	}
+
+	validationErrors := previewInt.Validate(string(fileBytes))
+
+	if len(validationErrors) > 0 {
+		errString := "the following error(s) were found while validating the porter.yaml file:"
+
+		for _, err := range validationErrors {
+			errString += "\n- " + strings.ReplaceAll(err.Error(), "\n\n*", "\n  *")
+		}
+
+		return fmt.Errorf(errString)
+	}
+
+	return nil
+}
+
 func hasDeploymentHookEnvVars() bool {
 	if ghIDStr := os.Getenv("PORTER_GIT_INSTALLATION_ID"); ghIDStr == "" {
 		return false