diff --git a/pkg/config/imports.go b/pkg/config/imports.go index 58f470745..e2c0f0191 100644 --- a/pkg/config/imports.go +++ b/pkg/config/imports.go @@ -15,9 +15,15 @@ import ( "github.com/spf13/viper" ) +// import Resolved Paths +type ResolvedPaths struct { + filePath string // path to the resolved config file + importPaths string // import path from atmos config +} + func (cl *ConfigLoader) downloadRemoteConfig(url string, tempDir string) (string, error) { // uniq name for the temp file - fileName := fmt.Sprintf("atmos-%d.yaml", time.Now().UnixNano()) + fileName := fmt.Sprintf("atmos-import-%d.yaml", time.Now().UnixNano()) tempFile := filepath.Join(tempDir, fileName) ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() @@ -35,7 +41,7 @@ func (cl *ConfigLoader) downloadRemoteConfig(url string, tempDir string) (string return tempFile, nil } -func (cl *ConfigLoader) processImports(importPaths []string, tempDir string, currentDepth, maxDepth int) (resolvedPaths []string, err error) { +func (cl *ConfigLoader) processImports(importPaths []string, tempDir string, currentDepth, maxDepth int) (resolvedPaths []ResolvedPaths, err error) { if tempDir == "" { return nil, fmt.Errorf("tempDir required to process imports") } @@ -76,7 +82,7 @@ func isRemoteImport(importPath string) bool { } // Process remote imports -func (cl *ConfigLoader) processRemoteImport(importPath, tempDir string, currentDepth, maxDepth int) ([]string, error) { +func (cl *ConfigLoader) processRemoteImport(importPath, tempDir string, currentDepth, maxDepth int) ([]ResolvedPaths, error) { parsedURL, err := url.Parse(importPath) if err != nil || (parsedURL.Scheme != "http" && parsedURL.Scheme != "https") { return nil, fmt.Errorf("unsupported URL '%s': %v", importPath, err) @@ -99,7 +105,11 @@ func (cl *ConfigLoader) processRemoteImport(importPath, tempDir string, currentD return nil, fmt.Errorf("failed to unmarshal remote config '%s': %v", importPath, err) } - resolvedPaths := []string{tempFile} + resolvedPaths := make([]ResolvedPaths, 0) + resolvedPaths = append(resolvedPaths, ResolvedPaths{ + filePath: tempFile, + importPaths: importPath, + }) // Recursively process imports from the remote file if importedConfig.Import != nil { @@ -114,7 +124,7 @@ func (cl *ConfigLoader) processRemoteImport(importPath, tempDir string, currentD } // Process local imports -func (cl *ConfigLoader) processLocalImport(importPath, tempDir string, currentDepth, maxDepth int) ([]string, error) { +func (cl *ConfigLoader) processLocalImport(importPath, tempDir string, currentDepth, maxDepth int) ([]ResolvedPaths, error) { basePath, err := filepath.Abs(cl.atmosConfig.BasePath) if err != nil { return nil, fmt.Errorf("failed to resolve base path: %v", err) @@ -126,7 +136,13 @@ func (cl *ConfigLoader) processLocalImport(importPath, tempDir string, currentDe return nil, fmt.Errorf("failed to resolve local import path '%s': %v", importPath, err) } - resolvedPaths := paths + resolvedPaths := make([]ResolvedPaths, 0) + for _, path := range paths { + resolvedPaths = append(resolvedPaths, ResolvedPaths{ + filePath: path, + importPaths: importPath, + }) + } // Load the local configuration file to check for further imports for _, path := range paths { diff --git a/pkg/config/loader.go b/pkg/config/loader.go index 0ebb3fe0e..b20b14026 100644 --- a/pkg/config/loader.go +++ b/pkg/config/loader.go @@ -799,18 +799,18 @@ func (cl *ConfigLoader) processConfigImports() error { return err } - for _, configPath := range resolvedPaths { - err := cl.loadConfigFileViber(cl.atmosConfig, configPath, cl.viper) + for _, resolvedPath := range resolvedPaths { + err := cl.loadConfigFileViber(cl.atmosConfig, resolvedPath.filePath, cl.viper) if err != nil { - log.Debug("error load config file", "path", configPath, "error", err) + log.Debug("error load config file", "import", resolvedPath.importPaths, "file_path", resolvedPath.filePath, "error", err) continue } - log.Debug("atmos merged config from import path", "path", configPath) err = cl.deepMergeConfig() if err != nil { - log.Debug("error merge config after imports", "path", configPath, "error", err) + log.Debug("error merge config after imports", "import", resolvedPath.importPaths, "file_path", resolvedPath.filePath, "error", err) continue } + log.Debug("atmos merged config from import", "import", resolvedPath.importPaths, "file_path", resolvedPath.filePath) } cl.atmosConfig.Import = importPaths } diff --git a/tests/cli_test.go b/tests/cli_test.go index 865659339..dedb32def 100644 --- a/tests/cli_test.go +++ b/tests/cli_test.go @@ -245,6 +245,9 @@ func sanitizeOutput(output string) (string, error) { fixedRemainder := collapseExtraSlashes(groups[2]) return groups[1] + fixedRemainder }) + // 6. Replace lines atmos-import log + filePathRegex := regexp.MustCompile(`file_path=[^ ]+/atmos-import-\d+/atmos-import-\d+\.yaml`) + result = filePathRegex.ReplaceAllString(result, "file_path=/atmos-import/atmos-import.yaml") return result, nil } diff --git a/examples/demo-atmos-cli-imports/atmos.yaml b/tests/fixtures/scenarios/atmos-cli-imports/atmos.yaml similarity index 100% rename from examples/demo-atmos-cli-imports/atmos.yaml rename to tests/fixtures/scenarios/atmos-cli-imports/atmos.yaml diff --git a/examples/demo-atmos-cli-imports/configs.d/commands.yaml b/tests/fixtures/scenarios/atmos-cli-imports/configs.d/commands.yaml similarity index 100% rename from examples/demo-atmos-cli-imports/configs.d/commands.yaml rename to tests/fixtures/scenarios/atmos-cli-imports/configs.d/commands.yaml diff --git a/examples/demo-atmos-cli-imports/configs.d/tools/stack.yml b/tests/fixtures/scenarios/atmos-cli-imports/configs.d/tools/stack.yml similarity index 100% rename from examples/demo-atmos-cli-imports/configs.d/tools/stack.yml rename to tests/fixtures/scenarios/atmos-cli-imports/configs.d/tools/stack.yml diff --git a/examples/demo-atmos-cli-imports/configs.d/tools/terraform.yaml b/tests/fixtures/scenarios/atmos-cli-imports/configs.d/tools/terraform.yaml similarity index 100% rename from examples/demo-atmos-cli-imports/configs.d/tools/terraform.yaml rename to tests/fixtures/scenarios/atmos-cli-imports/configs.d/tools/terraform.yaml diff --git a/examples/demo-atmos-cli-imports/configs.d/vendor.yaml b/tests/fixtures/scenarios/atmos-cli-imports/configs.d/vendor.yaml similarity index 100% rename from examples/demo-atmos-cli-imports/configs.d/vendor.yaml rename to tests/fixtures/scenarios/atmos-cli-imports/configs.d/vendor.yaml diff --git a/examples/demo-atmos-cli-imports/logs.yaml b/tests/fixtures/scenarios/atmos-cli-imports/logs.yaml similarity index 100% rename from examples/demo-atmos-cli-imports/logs.yaml rename to tests/fixtures/scenarios/atmos-cli-imports/logs.yaml diff --git a/tests/test-cases/atmos-cli-imports.yaml b/tests/test-cases/atmos-cli-imports.yaml new file mode 100644 index 000000000..0936e3afa --- /dev/null +++ b/tests/test-cases/atmos-cli-imports.yaml @@ -0,0 +1,13 @@ +# yaml-language-server: $schema=schema.json +tests: + - name: atmos_describe_config_imports + snapshot: true + enabled: true + description: "Ensure atmos execute import configuration" + workdir: "fixtures/scenarios/atmos-cli-imports" + command: "atmos" + args: + - "describe" + - "config" + expect: + exit_code: 0