Skip to content

Commit

Permalink
alpha: multiple appneed, yaml by suffix
Browse files Browse the repository at this point in the history
  • Loading branch information
23doors committed Jun 22, 2022
1 parent bce270e commit 0511456
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 16 deletions.
34 changes: 22 additions & 12 deletions internal/fileutil/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,23 @@ func FindSubdirsOfMatching(path string, matching []string) []string {
return dirs
}

func FindYAMLFiles(path string, name ...string) []string {
func FindYAMLFilesByName(path string, name ...string) []string {
return FindYAMLFiles(path, func(f string) bool {
for _, ext := range []string{".yaml", ".yml"} {
for _, n := range name {
if n+ext != f {
continue
}

return true
}
}

return false
})
}

func FindYAMLFiles(path string, match func(string) bool) []string {
var files []string

pathMap := make(map[string]struct{})
Expand All @@ -110,19 +126,13 @@ func FindYAMLFiles(path string, name ...string) []string {
return nil
}

for _, ext := range []string{".yaml", ".yml"} {
for _, n := range name {
if n+ext != info.Name() {
continue
}

files = append(files, path)
pathMap[path] = struct{}{}

return nil
}
if !match(info.Name()) {
return nil
}

files = append(files, path)
pathMap[path] = struct{}{}

return nil
})

Expand Down
5 changes: 5 additions & 0 deletions pkg/config/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,17 @@ func (i *AppDeployInfo) Proto() *apiv1.AppDeployInfo {
}

type AppNeed struct {
Dep string `yaml:"dependency,omitempty"`
Other map[string]interface{} `yaml:"-,remain"`

dep *Dependency
}

func (n *AppNeed) Normalize(name string, cfg *Project, data []byte) error {
if n.Dep != "" {
name = n.Dep
}

dep := cfg.DependencyByName(name)
if dep == nil {
return fileutil.YAMLError(fmt.Sprintf("$.needs.%s", name), fmt.Sprintf("'%s' not found in project dependencies", name), data)
Expand Down
29 changes: 25 additions & 4 deletions pkg/config/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ import (
apiv1 "github.com/outblocks/outblocks-plugin-go/gen/api/v1"
plugin_util "github.com/outblocks/outblocks-plugin-go/util"
"github.com/pterm/pterm"
"golang.org/x/exp/slices"
)

const (
ProjectYAMLName = "project.outblocks"
LockfileName = "outblocks.lock"
AppYAMLName = "app.outblocks"
ProjectYAMLName = "project.outblocks"
LockfileName = "outblocks.lock"
AppYAMLName = "app.outblocks"
AppYAMLNameSuffix = ".app.outblocks"
)

var (
Expand Down Expand Up @@ -196,7 +198,26 @@ func (p *Project) LoadApps(mode LoadMode) error {
return nil
}

files := fileutil.FindYAMLFiles(p.Dir, AppYAMLNames...)
files := fileutil.FindYAMLFiles(p.Dir, func(f string) bool {
idx := strings.LastIndex(f, ".")
if idx == -1 {
return false
}

ext := f[idx+1:]

if ext != "yaml" && ext != "yml" {
return false
}

name := f[:idx]

if slices.Contains(AppYAMLNames, name) {
return true
}

return strings.HasSuffix(name, AppYAMLNameSuffix)
})

if err := p.LoadAppFiles(files, mode); err != nil {
return err
Expand Down

0 comments on commit 0511456

Please sign in to comment.