From 39cf094629fc4eedbb4a7780f88dcb1fbb31c908 Mon Sep 17 00:00:00 2001 From: Haitham Rageh Date: Tue, 19 Nov 2024 16:04:14 +0200 Subject: [PATCH] Set Default Schema to Remote Schema (#777) * feat: set default Atmos manifest URL if not specified in configuration * feat: log trace message when using default Atmos JSON Schema file * fix: improve error message for missing Atmos JSON Schema file * Update internal/exec/validate_stacks.go * fix: change log level from trace to info for default Atmos JSON Schema message * fix: change log level from info to trace for default Atmos JSON Schema message --------- Co-authored-by: Erik Osterman (CEO @ Cloud Posse) Co-authored-by: Andriy Knysh --- internal/exec/validate_stacks.go | 43 ++++++++++++++++++-------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/internal/exec/validate_stacks.go b/internal/exec/validate_stacks.go index 29c50bd15..13830b1a0 100644 --- a/internal/exec/validate_stacks.go +++ b/internal/exec/validate_stacks.go @@ -19,6 +19,8 @@ import ( u "github.com/cloudposse/atmos/pkg/utils" ) +const atmosManifestDefault = "https://atmos.tools/schemas/atmos/atmos-manifest/1.0/atmos-manifest.json" + // ExecuteValidateStacksCmd executes `validate stacks` command func ExecuteValidateStacksCmd(cmd *cobra.Command, args []string) error { info, err := processCommandLineArgs("", cmd, args, nil) @@ -84,27 +86,30 @@ func ValidateStacks(cliConfig schema.CliConfiguration) error { // The path to the Atmos manifest JSON Schema can be absolute path or a path relative to the `base_path` setting in `atmos.yaml` var atmosManifestJsonSchemaFilePath string - if cliConfig.Schemas.Atmos.Manifest != "" { - atmosManifestJsonSchemaFileAbsPath := path.Join(cliConfig.BasePath, cliConfig.Schemas.Atmos.Manifest) - - if u.FileExists(cliConfig.Schemas.Atmos.Manifest) { - atmosManifestJsonSchemaFilePath = cliConfig.Schemas.Atmos.Manifest - } else if u.FileExists(atmosManifestJsonSchemaFileAbsPath) { - atmosManifestJsonSchemaFilePath = atmosManifestJsonSchemaFileAbsPath - } else if u.IsURL(cliConfig.Schemas.Atmos.Manifest) { - atmosManifestJsonSchemaFilePath, err = downloadSchemaFromURL(cliConfig.Schemas.Atmos.Manifest) - if err != nil { - return err - } - } else { - return fmt.Errorf("the Atmos JSON Schema file '%s' does not exist.\n"+ - "It can be configured in the 'schemas.atmos.manifest' section in 'atmos.yaml', or provided using the 'ATMOS_SCHEMAS_ATMOS_MANIFEST' "+ - "ENV variable or '--schemas-atmos-manifest' command line argument.\n"+ - "The path to the schema file should be an absolute path or a path relative to the 'base_path' setting in 'atmos.yaml'. \n"+ - "Alternatively, you can specify a schema file using a URL that will be downloaded automatically.", - cliConfig.Schemas.Atmos.Manifest) + if cliConfig.Schemas.Atmos.Manifest == "" { + cliConfig.Schemas.Atmos.Manifest = atmosManifestDefault + u.LogTrace(cliConfig, fmt.Sprintf("The Atmos JSON Schema file is not configured. Using the default schema '%s'", atmosManifestDefault)) + } + atmosManifestJsonSchemaFileAbsPath := path.Join(cliConfig.BasePath, cliConfig.Schemas.Atmos.Manifest) + + if u.FileExists(cliConfig.Schemas.Atmos.Manifest) { + atmosManifestJsonSchemaFilePath = cliConfig.Schemas.Atmos.Manifest + } else if u.FileExists(atmosManifestJsonSchemaFileAbsPath) { + atmosManifestJsonSchemaFilePath = atmosManifestJsonSchemaFileAbsPath + } else if u.IsURL(cliConfig.Schemas.Atmos.Manifest) { + atmosManifestJsonSchemaFilePath, err = downloadSchemaFromURL(cliConfig.Schemas.Atmos.Manifest) + if err != nil { + return err } + } else { + return fmt.Errorf("Schema file '%s' not found. Configure via:\n"+ + "1. 'schemas.atmos.manifest' in atmos.yaml\n"+ + "2. ATMOS_SCHEMAS_ATMOS_MANIFEST env var\n"+ + "3. --schemas-atmos-manifest flag\n\n"+ + "Accepts: absolute path, paths relative to base_path, or URL", + cliConfig.Schemas.Atmos.Manifest) } + // Include (process and validate) all YAML files in the `stacks` folder in all subfolders includedPaths := []string{"**/*"} // Don't exclude any YAML files for validation