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

allow .yml files as components #270

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 10 additions & 2 deletions pkg/components/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"log"
"os"
"path/filepath"
"slices"

"github.com/dapr/cli/pkg/standalone"
v1alpha1 "github.com/dapr/dapr/pkg/apis/components/v1alpha1"
Expand Down Expand Up @@ -132,7 +133,7 @@
}
if info.IsDir() && info.Name() != filepath.Base(componentsDirectory) {
return filepath.SkipDir
} else if !info.IsDir() && filepath.Ext(path) == ".yaml" {
} else if !info.IsDir() && isYamlFile(path) {

Check warning on line 136 in pkg/components/components.go

View check run for this annotation

Codecov / codecov/patch

pkg/components/components.go#L136

Added line #L136 was not covered by tests
content, err := os.ReadFile(path)
if err != nil {
log.Printf("Failure reading file %s: %v\n", path, err)
Expand Down Expand Up @@ -180,7 +181,7 @@
}
if info.IsDir() && info.Name() != filepath.Base(componentsDirectory) {
return filepath.SkipDir
} else if !info.IsDir() && filepath.Ext(path) == ".yaml" {
} else if !info.IsDir() && isYamlFile(path) {
content, err := os.ReadFile(path)
if err != nil {
log.Printf("Failure reading file %s: %v\n", path, err)
Expand Down Expand Up @@ -217,3 +218,10 @@
}
return dockerComposeComponents
}

func isYamlFile(path string) bool {
actualExtension := filepath.Ext(path)
validExtensions := []string{".yaml", ".yml"}

return slices.Contains(validExtensions, actualExtension)
}
19 changes: 19 additions & 0 deletions pkg/components/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,22 @@ func TestDockerComposeGetComponent(t *testing.T) {
})
}
}

func TestIsYamlFile(t *testing.T) {
var scenarios = []struct {
path string
want bool
}{
{"pubsub.yaml", true},
{"pubsub.yml", true},
{"pubsub.txt", false},
{"pubsub", false},
}

for _, scenario := range scenarios {
t.Run(fmt.Sprintf("Should return valid yaml file - %t", scenario.want), func(t *testing.T) {
actual := isYamlFile(scenario.path)
assert.Equal(t, scenario.want, actual)
})
}
}
Loading