Skip to content

Commit

Permalink
Add github driver and low level transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
kajogo777 committed Dec 3, 2022
1 parent 22e842d commit cb4ebf6
Show file tree
Hide file tree
Showing 4 changed files with 1,020 additions and 0 deletions.
3 changes: 3 additions & 0 deletions internal/drivers/drivers.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ func NewDriversMap(environment string) map[string]Driver {
"gitlab": &GitlabDriver{
Path: path.Join("build", environment, "gitlab"),
},
"github": &GitHubDriver{
Path: path.Join("build", environment, "github"),
},
}
}
60 changes: 60 additions & 0 deletions internal/drivers/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package drivers

import (
"fmt"
"os"
"path"

"cuelang.org/go/cue"
"cuelang.org/go/encoding/yaml"
"devopzilla.com/guku/internal/stack"
"devopzilla.com/guku/internal/utils"
)

type GitHubDriver struct {
Path string
}

func (d *GitHubDriver) match(resource cue.Value) bool {
driverName, _ := resource.LookupPath(cue.ParsePath("$metadata.labels.driver")).String()
return driverName == "github"
}

func (d *GitHubDriver) ApplyAll(stack *stack.Stack) error {
foundResources := false

for _, componentId := range stack.GetTasks() {
component, _ := stack.GetComponent(componentId)

resourceIter, _ := component.LookupPath(cue.ParsePath("$resources")).Fields()
for resourceIter.Next() {
if d.match(resourceIter.Value()) {
foundResources = true
resource, err := utils.RemoveMeta(resourceIter.Value())
if err != nil {
return err
}

data, err := yaml.Encode(resource)
if err != nil {
return err
}

fileName := fmt.Sprintf("%s-github-workflow.yml", resourceIter.Label())
resourceFilePath := path.Join(d.Path, fileName)
if _, err := os.Stat(d.Path); os.IsNotExist(err) {
os.MkdirAll(d.Path, 0700)
}
os.WriteFile(resourceFilePath, data, 0700)
}
}
}

if !foundResources {
return nil
}

fmt.Printf("[github] applied resources to \"%s/*-github-workflow.yml\"\n", d.Path)

return nil
}
Loading

0 comments on commit cb4ebf6

Please sign in to comment.