Skip to content

Commit

Permalink
refactor schema name
Browse files Browse the repository at this point in the history
  • Loading branch information
samtholiya committed Mar 9, 2025
1 parent 82b1224 commit ccebfc3
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 15 deletions.
6 changes: 3 additions & 3 deletions internal/exec/validate_stacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func ExecuteValidateStacksCmd(cmd *cobra.Command, args []string) error {
}

if schemasAtmosManifestFlag != "" {
atmosConfig.Schemas["atmos"] = schema.Schemas{
atmosConfig.Schemas["atmos"] = schema.SchemaRegistry{
Manifest: schemasAtmosManifestFlag,
}
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func ValidateStacks(atmosConfig schema.AtmosConfiguration) error {
// Check if the Atmos manifest JSON Schema is configured and the file exists
// 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
manifestSchema := atmosConfig.GetSchemas("atmos")
manifestSchema := atmosConfig.GetSchemaRegistry("atmos")
atmosManifestJsonSchemaFileAbsPath := filepath.Join(atmosConfig.BasePath, manifestSchema.Manifest)

if manifestSchema.Manifest == "" {

Check failure on line 108 in internal/exec/validate_stacks.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] internal/exec/validate_stacks.go#L108

ifElseChain: rewrite if-else to switch statement (gocritic)
Raw output
internal/exec/validate_stacks.go:108:2: ifElseChain: rewrite if-else to switch statement (gocritic)
	if manifestSchema.Manifest == "" {
	^
Expand Down Expand Up @@ -386,7 +386,7 @@ func checkComponentStackMap(componentStackMap map[string]map[string][]string) ([

// downloadSchemaFromURL downloads the Atmos JSON Schema file from the provided URL

Check failure on line 387 in internal/exec/validate_stacks.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] internal/exec/validate_stacks.go#L387

Comment should end in a period (godot)
Raw output
internal/exec/validate_stacks.go:387:1: Comment should end in a period (godot)
// downloadSchemaFromURL downloads the Atmos JSON Schema file from the provided URL
^
func downloadSchemaFromURL(atmosConfig schema.AtmosConfiguration) (string, error) {

Check failure on line 388 in internal/exec/validate_stacks.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] internal/exec/validate_stacks.go#L388

hugeParam: atmosConfig is heavy (5704 bytes); consider passing it by pointer (gocritic)
Raw output
internal/exec/validate_stacks.go:388:28: hugeParam: atmosConfig is heavy (5704 bytes); consider passing it by pointer (gocritic)
func downloadSchemaFromURL(atmosConfig schema.AtmosConfiguration) (string, error) {
                           ^
manifestSchema := atmosConfig.GetSchemas("atmos")
manifestSchema := atmosConfig.GetSchemaRegistry("atmos")
manifestURL := manifestSchema.Manifest
parsedURL, err := url.Parse(manifestURL)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ func InitCliConfig(configAndStacksInfo schema.ConfigAndStacksInfo, processStacks
// https://gist.github.com/chazcheadle/45bf85b793dea2b71bd05ebaa3c28644
// https://sagikazarmark.hu/blog/decoding-custom-formats-with-viper/
err = v.Unmarshal(&atmosConfig)
atmosConfig.ProcessSchemas()

Check failure on line 261 in pkg/config/config.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] pkg/config/config.go#L261

Error return value of `atmosConfig.ProcessSchemas` is not checked (errcheck)
Raw output
pkg/config/config.go:261:28: Error return value of `atmosConfig.ProcessSchemas` is not checked (errcheck)
	atmosConfig.ProcessSchemas()
	                          ^
if err != nil {
return atmosConfig, err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/config/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func processEnvVars(atmosConfig *schema.AtmosConfiguration) error {
atmosManifestJsonSchemaPath := os.Getenv("ATMOS_SCHEMAS_ATMOS_MANIFEST")
if len(atmosManifestJsonSchemaPath) > 0 {
u.LogDebug(fmt.Sprintf("Found ENV var ATMOS_SCHEMAS_ATMOS_MANIFEST=%s", atmosManifestJsonSchemaPath))

Check failure on line 353 in pkg/config/utils.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] pkg/config/utils.go#L353

SA1019: u.LogDebug is deprecated: Use `log.Debug` instead. This function will be removed in a future release. LogDebug logs the provided debug message (staticcheck)
Raw output
pkg/config/utils.go:353:3: SA1019: u.LogDebug is deprecated: Use `log.Debug` instead. This function will be removed in a future release. LogDebug logs the provided debug message (staticcheck)
		u.LogDebug(fmt.Sprintf("Found ENV var ATMOS_SCHEMAS_ATMOS_MANIFEST=%s", atmosManifestJsonSchemaPath))
		^
atmosConfig.Schemas["atmos"] = schema.Schemas{
atmosConfig.Schemas["atmos"] = schema.SchemaRegistry{
Manifest: atmosManifestJsonSchemaPath,
}
}
Expand Down Expand Up @@ -493,7 +493,7 @@ func processCommandLineArgs(atmosConfig *schema.AtmosConfiguration, configAndSta
u.LogDebug(fmt.Sprintf("Using command line argument '%s' as CUE schemas directory", configAndStacksInfo.CueDir))

Check failure on line 493 in pkg/config/utils.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] pkg/config/utils.go#L493

SA1019: u.LogDebug is deprecated: Use `log.Debug` instead. This function will be removed in a future release. LogDebug logs the provided debug message (staticcheck)
Raw output
pkg/config/utils.go:493:3: SA1019: u.LogDebug is deprecated: Use `log.Debug` instead. This function will be removed in a future release. LogDebug logs the provided debug message (staticcheck)
		u.LogDebug(fmt.Sprintf("Using command line argument '%s' as CUE schemas directory", configAndStacksInfo.CueDir))
		^
}
if len(configAndStacksInfo.AtmosManifestJsonSchema) > 0 {
atmosConfig.Schemas["atmos"] = schema.Schemas{
atmosConfig.Schemas["atmos"] = schema.SchemaRegistry{
Manifest: configAndStacksInfo.AtmosManifestJsonSchema,
}
u.LogDebug(fmt.Sprintf("Using command line argument '%s' as path to Atmos JSON Schema", configAndStacksInfo.AtmosManifestJsonSchema))

Check failure on line 499 in pkg/config/utils.go

View workflow job for this annotation

GitHub Actions / golangci

[golangci] pkg/config/utils.go#L499

SA1019: u.LogDebug is deprecated: Use `log.Debug` instead. This function will be removed in a future release. LogDebug logs the provided debug message (staticcheck)
Raw output
pkg/config/utils.go:499:3: SA1019: u.LogDebug is deprecated: Use `log.Debug` instead. This function will be removed in a future release. LogDebug logs the provided debug message (staticcheck)
		u.LogDebug(fmt.Sprintf("Using command line argument '%s' as path to Atmos JSON Schema", configAndStacksInfo.AtmosManifestJsonSchema))
		^
Expand Down
68 changes: 59 additions & 9 deletions pkg/schema/schema.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package schema

import (
"encoding/json"

"github.com/cloudposse/atmos/pkg/store"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -41,28 +43,28 @@ type AtmosConfiguration struct {
CliConfigPath string `yaml:"cli_config_path" json:"cli_config_path,omitempty" mapstructure:"cli_config_path"`
}

func (m *AtmosConfiguration) GetSchemas(key string) Schemas {
func (m *AtmosConfiguration) GetSchemaRegistry(key string) SchemaRegistry {
atmosSchemaInterface, interfaceOk := m.Schemas[key]
var manifestSchema Schemas
var manifestSchema SchemaRegistry
atmosSchemaFound := false
if interfaceOk {
manifestSchema, atmosSchemaFound = atmosSchemaInterface.(Schemas)
manifestSchema, atmosSchemaFound = atmosSchemaInterface.(SchemaRegistry)
}
if atmosSchemaFound {
return manifestSchema
}
return Schemas{}
return SchemaRegistry{}
}

func (m *AtmosConfiguration) GetResourcePath(key string) ResourcePath {
atmosSchemaInterface, interfaceOk := m.Schemas[key]
var manifestSchema ResourcePath
var resourcePath ResourcePath
atmosSchemaFound := false
if interfaceOk {
manifestSchema, atmosSchemaFound = atmosSchemaInterface.(ResourcePath)
resourcePath, atmosSchemaFound = atmosSchemaInterface.(ResourcePath)
}
if atmosSchemaFound {
return manifestSchema
return resourcePath
}
return ResourcePath{}
}
Expand Down Expand Up @@ -101,7 +103,7 @@ func (m *AtmosConfiguration) UnmarshalYAML(value *yaml.Node) error {
}

// Try decoding as Manifest struct
var manifest Schemas
var manifest SchemaRegistry
if err := node.Decode(&manifest); err == nil {
m.Schemas[key] = manifest
continue
Expand All @@ -114,6 +116,54 @@ func (m *AtmosConfiguration) UnmarshalYAML(value *yaml.Node) error {
return nil
}

func (a *AtmosConfiguration) ProcessSchemas() error {
for key := range a.Schemas {
if key == "cue" || key == "opa" || key == "jsonschema" {
a.processResourceSchema(key)
continue
}
a.processManifestSchemas(key)
}
return nil
}

func (a *AtmosConfiguration) processManifestSchemas(key string) {
val, exists := a.Schemas[key]
if !exists {
return
}
// Marshal the interface{} to JSON
data, err := json.Marshal(val)
if err != nil {
return
}
// Unmarshal JSON into ResourcePath struct
var schemasStruct SchemaRegistry
if err := json.Unmarshal(data, &schemasStruct); err != nil {
return
}
a.Schemas[key] = schemasStruct
}

func (a *AtmosConfiguration) processResourceSchema(key string) {
val, exists := a.Schemas[key]
if !exists {
return
}
// Marshal the interface{} to JSON
data, err := json.Marshal(val)
if err != nil {
return
}

// Unmarshal JSON into ResourcePath struct
var resource ResourcePath
if err := json.Unmarshal(data, &resource); err != nil {
return
}
a.Schemas[key] = resource
}

type Validate struct {
EditorConfig EditorConfig `yaml:"editorconfig,omitempty" json:"editorconfig,omitempty" mapstructure:"editorconfig"`
}
Expand Down Expand Up @@ -554,7 +604,7 @@ type ResourcePath struct {
BasePath string `yaml:"base_path,omitempty" json:"base_path,omitempty" mapstructure:"base_path"`
}

type Schemas struct {
type SchemaRegistry struct {
Manifest string `yaml:"manifest,omitempty" json:"manifest,omitempty" mapstructure:"manifest"`
Matches []string `yaml:"matches,omitempty" json:"matches,omitempty" mapstructure:"matches"`
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ schemas:
atmosConfig := &AtmosConfiguration{}
err := yaml.Unmarshal([]byte(yamlString), atmosConfig)
assert.NoError(t, err)
schemas := atmosConfig.GetSchemas("atmos")
schemas := atmosConfig.GetSchemaRegistry("atmos")
assert.Equal(t, "some/random/path", schemas.Manifest)
assert.Equal(t, []string{"hello", "world"}, schemas.Matches)
}

0 comments on commit ccebfc3

Please sign in to comment.