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

make embed assets relative to where annotation lives or abs from proj… #30

Merged
merged 4 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
48 changes: 48 additions & 0 deletions pkg/exec_unit/plugin_assets.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package execunit

import (
"path/filepath"
"strings"

"github.com/bmatcuk/doublestar/v4"
"github.com/klothoplatform/klotho/pkg/annotation"
"github.com/klothoplatform/klotho/pkg/core"
Expand Down Expand Up @@ -43,6 +46,12 @@ func (p Assets) Transform(result *core.CompilationResult, deps *core.Dependencie
matcher.include, _ = annot.Capability.Directives.StringArray("include")
matcher.exclude, _ = annot.Capability.Directives.StringArray("exclude")

err := matcher.ModifyPathsForAnnotatedFile(f.Path())
if err != nil {
errs.Append(err)
break
}

matchCount := 0
for _, asset := range input.Files() {
if matcher.Matches(asset.Path()) {
Expand Down Expand Up @@ -75,6 +84,45 @@ type assetPathMatcher struct {
err error
}

func (m *assetPathMatcher) ModifyPathsForAnnotatedFile(path string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Having this be a mutator function seems a bit weird to me. An assetPathMatcher isn't valid before calling this function and I'm not sure if it'd behave right if called twice, so callers need to make sure to call it exactly once on construction. Seems like it might be better served in a NewAssetPathMatcher constructor function.

newInclude := []string{}
for _, pattern := range m.include {
absPath, err := filepath.Abs(pattern)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems odd to convert the pattern to an absolute path, perhaps IsAbs would be better?

Also, do we expect these paths to be standard unix or platform-specific? These calls will all be platform specific. If platform specific is intended, the TrimPrefix later on the leading slash won't work on Windows.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IsAbs is probably better

so youre saying we will see the path for our files as static/file on mac or linux and /static/file on windows?

If so then why dont we convert everything to an absolute path from the klotho root in readdir so we have a standard path we can follow? Our internal compiler logic shouldnt be dependent on the platform we are running on.

if err != nil {
return err
}
if absPath == pattern {
newInclude = append(newInclude, strings.TrimPrefix(pattern, "/"))
continue
}
relPath, err := filepath.Rel(filepath.Dir("."), filepath.Join(filepath.Dir(path), pattern))
if err != nil {
return err
}
newInclude = append(newInclude, relPath)
}
m.include = newInclude

newExclude := []string{}
for _, pattern := range m.exclude {
absPath, err := filepath.Abs(pattern)
if err != nil {
return err
}
if absPath == pattern {
newExclude = append(newExclude, strings.TrimPrefix(pattern, "/"))
continue
}
relPath, err := filepath.Rel(filepath.Dir("."), filepath.Join(filepath.Dir(path), pattern))
if err != nil {
return err
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Seems like this could be extracted into a reusable function.

newExclude = append(newExclude, relPath)
}
m.exclude = newExclude
return nil
}

func (m *assetPathMatcher) Matches(p string) bool {
if m.err != nil {
return false
Expand Down
76 changes: 76 additions & 0 deletions pkg/exec_unit/plugin_assets_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package execunit

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -68,3 +69,78 @@ func Test_assetPathMatcher_Matches(t *testing.T) {
})
}
}

func Test_assetPathMatcher_ModifyPathsForAnnotatedFile(t *testing.T) {
type testResult struct {
include []string
exclude []string
}
tests := []struct {
name string
matcher assetPathMatcher
path string
want testResult
}{
{
name: "simple relative match",
matcher: assetPathMatcher{include: []string{"file.txt"}, exclude: []string{"notfile.txt"}},
path: "file.txt",
want: testResult{include: []string{"file.txt"}, exclude: []string{"notfile.txt"}},
},
{
name: "nested relative match",
matcher: assetPathMatcher{include: []string{"file.txt"}, exclude: []string{"notfile.txt"}},
path: "dir/file.txt",
want: testResult{include: []string{"dir/file.txt"}, exclude: []string{"dir/notfile.txt"}},
},
{
name: "simple absolute match",
matcher: assetPathMatcher{include: []string{"/file.txt"}, exclude: []string{"/notfile.txt"}},
path: "file.txt",
want: testResult{include: []string{"file.txt"}, exclude: []string{"notfile.txt"}},
},
{
name: "nested absolute match",
matcher: assetPathMatcher{include: []string{"/dir/file.txt"}, exclude: []string{"/dir/notfile.txt"}},
path: "dir/file.txt",
want: testResult{include: []string{"dir/file.txt"}, exclude: []string{"dir/notfile.txt"}},
},
{
name: "mix relative and absolute match",
matcher: assetPathMatcher{include: []string{"/dir/file.txt", "other.txt"}, exclude: []string{"/dir/notfile.txt", "notother.txt"}},
path: "dir/file.txt",
want: testResult{include: []string{"dir/file.txt", "dir/other.txt"}, exclude: []string{"dir/notfile.txt", "dir/notother.txt"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert := assert.New(t)

err := tt.matcher.ModifyPathsForAnnotatedFile(tt.path)
if !assert.NoError(err) {
return
}

for _, wantPath := range tt.want.include {
found := false
for _, path := range tt.matcher.include {
if wantPath == path {
found = true
}
}
assert.True(found)
}

for _, wantPath := range tt.want.exclude {
found := false
for _, path := range tt.matcher.exclude {
if wantPath == path {
found = true
}
fmt.Println(path)
}
assert.True(found)
}
})
}
}