Skip to content

Commit

Permalink
allow yml files as components (#270)
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Hillier <[email protected]>
Co-authored-by: Andrew Hillier <[email protected]>
  • Loading branch information
andrew-hillier and Andrew Hillier authored Feb 29, 2024
1 parent 6d4c52a commit a92b8cd
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
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 @@ import (
"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 @@ func (c *components) getStandaloneComponents(scope string) []Component {
}
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 @@ -180,7 +181,7 @@ func (c *components) getDockerComposeComponents(scope string) []Component {
}
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 @@ func (c *components) getDockerComposeComponents(scope string) []Component {
}
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)
})
}
}

0 comments on commit a92b8cd

Please sign in to comment.