Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for remote validation schemas #731

Merged
merged 19 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/demo-context/atmos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ base_path: "./"

schemas:
atmos:
manifest: "schemas/atmos-manifest.json"

manifest: "https://atmos.tools/schemas/atmos/atmos-manifest/1.0/atmos-manifest.json"
aknysh marked this conversation as resolved.
Show resolved Hide resolved
aknysh marked this conversation as resolved.
Show resolved Hide resolved
# https://pkg.go.dev/text/template
templates:
settings:
Expand Down
23 changes: 22 additions & 1 deletion internal/exec/validate_stacks.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package exec

import (
"context"
"fmt"
"os"
"path"
"reflect"
"strings"

"github.com/hashicorp/go-getter"
"github.com/pkg/errors"
"github.com/spf13/cobra"

Expand Down Expand Up @@ -86,11 +89,29 @@ func ValidateStacks(cliConfig schema.CliConfiguration) error {
atmosManifestJsonSchemaFilePath = cliConfig.Schemas.Atmos.Manifest
} else if u.FileExists(atmosManifestJsonSchemaFileAbsPath) {
atmosManifestJsonSchemaFilePath = atmosManifestJsonSchemaFileAbsPath
} else if u.IsURL(cliConfig.Schemas.Atmos.Manifest) {
tempDir := os.TempDir()
fileName, err := u.GetFileNameFromURL(cliConfig.Schemas.Atmos.Manifest)
haitham911 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("failed to get the file name from the URL '%s': %w", cliConfig.Schemas.Atmos.Manifest, err)
}
haitham911 marked this conversation as resolved.
Show resolved Hide resolved
atmosManifestJsonSchemaFilePath = path.Join(tempDir, fileName)
haitham911 marked this conversation as resolved.
Show resolved Hide resolved
osterman marked this conversation as resolved.
Show resolved Hide resolved
client := &getter.Client{
Ctx: context.Background(),
Dst: atmosManifestJsonSchemaFilePath,
Src: cliConfig.Schemas.Atmos.Manifest,
Mode: getter.ClientModeFile,
}
haitham911 marked this conversation as resolved.
Show resolved Hide resolved
if err = client.Get(); err != nil {
return fmt.Errorf("failed to download the Atmos JSON Schema file '%s' from the URL '%s': %w", fileName, cliConfig.Schemas.Atmos.Manifest, err)
}

haitham911 marked this conversation as resolved.
Show resolved Hide resolved
haitham911 marked this conversation as resolved.
Show resolved Hide resolved
} 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'.",
"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, the schema file can be downloaded from the provided URL",
osterman marked this conversation as resolved.
Show resolved Hide resolved
cliConfig.Schemas.Atmos.Manifest)
}
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/utils/file_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package utils

import (
"io/fs"
"net/url"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -173,3 +174,22 @@ func IsSocket(path string) (bool, error) {
isSocket := fileInfo.Mode().Type() == fs.ModeSocket
return isSocket, nil
}

// IsURL checks if a string is a URL
func IsURL(s string) bool {
url, err := url.Parse(s)
haitham911 marked this conversation as resolved.
Show resolved Hide resolved
return err == nil && url.Scheme != "" && url.Host != ""
}
osterman marked this conversation as resolved.
Show resolved Hide resolved

// GetFileNameFromURL extracts the file name from a URL
func GetFileNameFromURL(rawURL string) (string, error) {
parsedURL, err := url.Parse(rawURL)
if err != nil {
return "", err
}
// Extract the path from the URL
urlPath := parsedURL.Path

// Get the base name of the path
return path.Base(urlPath), nil
}
haitham911 marked this conversation as resolved.
Show resolved Hide resolved