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

feat: Support Relative Path Imports #891

Merged
merged 48 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
c50d071
Handle relative paths for stack imports
milldr Dec 24, 2024
f79e16a
Tests for relative paths
milldr Dec 24, 2024
74ab1d8
Refactor handling of relative paths in ProcessImportSection
milldr Dec 24, 2024
e2af2db
Handle relative paths in StackImport.Path and string imports
milldr Dec 24, 2024
9d39012
Add new test2/us-west-1.yaml file and update test counts
milldr Dec 24, 2024
12358b0
Update import paths in us-west-1.yaml file
milldr Dec 24, 2024
093ac5e
Merge branch 'main' into feat/DEV-1856_relative-paths
milldr Dec 24, 2024
b9856ff
Merge branch 'main' into feat/DEV-1856_relative-paths
osterman Dec 26, 2024
71eaca0
Merge branch 'main' into feat/DEV-1856_relative-paths
milldr Dec 26, 2024
4f6170c
Merge branch 'feat/DEV-1856_relative-paths' of github.com:cloudposse/…
milldr Dec 26, 2024
362e979
Merge branch 'main' into feat/DEV-1856_relative-paths
milldr Dec 27, 2024
8d76059
Refactor handling of import paths in ProcessImportSection
milldr Dec 27, 2024
1beeb8a
Resolve relative paths in ProcessImportSection function
milldr Dec 27, 2024
dd55c0f
Update internal/exec/stack_processor_utils.go
milldr Dec 27, 2024
dfa5a84
Refactor resolveRelativePath for improved path handling
milldr Dec 27, 2024
507a5dd
Refactor filepath handling for better readability
milldr Dec 27, 2024
cafdfdd
Remove unnecessary path normalization code and simplify logic
milldr Dec 27, 2024
ddde3d5
Improve path resolution handling in stack_processor_utils
milldr Dec 27, 2024
4eeef33
Merge branch 'main' into feat/DEV-1856_relative-paths
milldr Jan 2, 2025
7fa809e
relative path handling improvements for windows; added security check…
milldr Jan 3, 2025
3be0d81
Validate import paths for security and base path inclusion
milldr Jan 3, 2025
2f89d4a
Update path.Join calls to use filepath.Join for consistency
milldr Jan 3, 2025
2c80a89
Update stack_processor_utils.go to use filepath package
milldr Jan 3, 2025
062cf05
validate stack paths to check against the global base path
milldr Jan 3, 2025
3d98cd4
Refactor path resolution and validation logic
milldr Jan 3, 2025
558c324
Refactor resolving relative path logic for clarity
milldr Jan 3, 2025
88740a5
Revert changes
milldr Jan 3, 2025
94886b6
Refactor path resolution for consistency
milldr Jan 3, 2025
1bc256a
Refactor resolveRelativePath function for path consistency
milldr Jan 3, 2025
037e0ba
Convert relative path to base path if starts with "." or ".."
milldr Jan 3, 2025
358128c
Simplify resolving relative paths in stack_processor_utils
milldr Jan 3, 2025
7bf9208
added tests for atmos stacks with relative paths
milldr Jan 3, 2025
29b390d
Merge branch 'main' into feat/DEV-1856_relative-paths
milldr Jan 3, 2025
daefbe1
merge
milldr Jan 7, 2025
10d41dc
PR feedback
milldr Jan 7, 2025
9767879
merged main, incorporate new test fixture pattern
milldr Jan 7, 2025
fcf08fc
Merge branch 'main' into feat/DEV-1856_relative-paths
milldr Jan 9, 2025
229aa91
Add test case for atmos stacks with relative paths
milldr Jan 9, 2025
8e47d61
Delete nonprod and prod cache weather files
milldr Jan 9, 2025
b215eca
Delete outdated Atmos manifest JSON schema
milldr Jan 9, 2025
bcd13ac
consistent indentation
milldr Jan 9, 2025
57b557f
Add ignore rule for cache text files
milldr Jan 9, 2025
6faf6dd
Update stack processor test cases and relative paths
milldr Jan 9, 2025
187e61b
revert cahnges
milldr Jan 9, 2025
a482907
use relative paths in advanced usage example
milldr Jan 9, 2025
dc897c7
use relative paths in advanced usage example
milldr Jan 9, 2025
a427817
reset advanced usage
milldr Jan 9, 2025
fb6ac32
added relative paths to demo-context
milldr Jan 9, 2025
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
8 changes: 8 additions & 0 deletions examples/tests/stacks/mixins/stage/test2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# yaml-language-server: $schema=https://atmos.tools/schemas/atmos/atmos-manifest/1.0/atmos-manifest.json

vars:
stage: test-2

settings:
config:
is_prod: false
5 changes: 5 additions & 0 deletions examples/tests/stacks/orgs/cp/tenant1/test2/_defaults.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# yaml-language-server: $schema=https://atmos.tools/schemas/atmos/atmos-manifest/1.0/atmos-manifest.json

import:
- mixins/stage/test2
- ../_defaults # validate relative paths
5 changes: 5 additions & 0 deletions examples/tests/stacks/orgs/cp/tenant1/test2/us-east-2.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# yaml-language-server: $schema=https://atmos.tools/schemas/atmos/atmos-manifest/1.0/atmos-manifest.json

import:
- mixins/region/us-east-2
- ./_defaults # validate relative paths
5 changes: 5 additions & 0 deletions examples/tests/stacks/orgs/cp/tenant1/test2/us-west-1.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# yaml-language-server: $schema=https://atmos.tools/schemas/atmos/atmos-manifest/1.0/atmos-manifest.json

import:
- path: mixins/region/us-west-1
- path: ./_defaults # validate relative paths using the `path` field
24 changes: 20 additions & 4 deletions internal/exec/stack_processor_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -1744,10 +1744,10 @@ func FindComponentDependenciesLegacy(
}

// ProcessImportSection processes the `import` section in stack manifests
// The `import` section` can be of the following types:
// 1. list of `StackImport` structs
// 2. list of strings
// 3. List of strings and `StackImport` structs in the same file
// The `import` section can contain:
// 1. Project-relative paths (e.g. "mixins/region/us-east-2")
// 2. Paths relative to the current file (starting with "./" or "../")
// 3. StackImport structs containing either of the above path types
func ProcessImportSection(stackMap map[string]any, filePath string) ([]schema.StackImport, error) {
stackImports, ok := stackMap[cfg.ImportSectionName]

Expand All @@ -1773,6 +1773,15 @@ func ProcessImportSection(stackMap map[string]any, filePath string) ([]schema.St
var importObj schema.StackImport
err := mapstructure.Decode(imp, &importObj)
if err == nil {
// Handle paths relative to current file (starting with ./ or ../)
if strings.HasPrefix(importObj.Path, "./") || strings.HasPrefix(importObj.Path, "../") {
// Get the directory of the current file
baseDir := filepath.Dir(filePath)
// Join the base directory with the relative path
importObj.Path = filepath.Join(baseDir, importObj.Path)
// Clean the path to resolve any .. segments
importObj.Path = filepath.Clean(importObj.Path)
}
result = append(result, importObj)
continue
}
Expand All @@ -1786,6 +1795,13 @@ func ProcessImportSection(stackMap map[string]any, filePath string) ([]schema.St
return nil, fmt.Errorf("invalid empty import in the file '%s'", filePath)
}

// Handle paths relative to current file (starting with ./ or ../)
if strings.HasPrefix(s, "./") || strings.HasPrefix(s, "../") {
osterman marked this conversation as resolved.
Show resolved Hide resolved
baseDir := filepath.Dir(filePath)
s = filepath.Join(baseDir, s)
s = filepath.Clean(s)
}

result = append(result, schema.StackImport{Path: s})
}

Expand Down
9 changes: 6 additions & 3 deletions pkg/stack/stack_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func TestStackProcessor(t *testing.T) {
"../../examples/tests/stacks/orgs/cp/tenant1/prod/us-east-2.yaml",
"../../examples/tests/stacks/orgs/cp/tenant1/staging/us-east-2.yaml",
"../../examples/tests/stacks/orgs/cp/tenant1/test1/us-east-2.yaml",
"../../examples/tests/stacks/orgs/cp/tenant1/test2/us-east-2.yaml",
"../../examples/tests/stacks/orgs/cp/tenant1/test2/us-west-1.yaml",
}

processStackDeps := true
Expand Down Expand Up @@ -50,15 +52,16 @@ func TestStackProcessor(t *testing.T) {
)

assert.Nil(t, err)
assert.Equal(t, 4, len(listResult))
assert.Equal(t, 4, len(mapResult))
assert.Equal(t, 6, len(listResult))
assert.Equal(t, 6, len(mapResult))

mapResultKeys := u.StringKeysFromMap(mapResult)
assert.Equal(t, "orgs/cp/tenant1/dev/us-east-2", mapResultKeys[0])
assert.Equal(t, "orgs/cp/tenant1/prod/us-east-2", mapResultKeys[1])
assert.Equal(t, "orgs/cp/tenant1/staging/us-east-2", mapResultKeys[2])
assert.Equal(t, "orgs/cp/tenant1/test1/us-east-2", mapResultKeys[3])

assert.Equal(t, "orgs/cp/tenant1/test2/us-east-2", mapResultKeys[4])
assert.Equal(t, "orgs/cp/tenant1/test2/us-west-1", mapResultKeys[5])
mapConfig1, err := u.UnmarshalYAML[schema.AtmosSectionMapType](listResult[0])
assert.Nil(t, err)

Expand Down
Loading