Skip to content

Commit

Permalink
feat(template): enable remote file templating (#680)
Browse files Browse the repository at this point in the history
* feat(template): sample sensitive config

* feat(template): updated funcs, tests

* fix: go mod tidy

* fix: small refactor on template render

* docs: updated config/template docs

* fix: bump timeout in tui tests

* fix: small refactor for extending tmpl

* fix: bumped timeout

* feat(template): add remote fetch with helper commands

* fix(merge): issue with conflicts

* fix(title): pr title fix

---------

Co-authored-by: Megan Wolf <megan@defenseunicorns.com>
  • Loading branch information
brandtkeller and meganwolf0 authored Sep 27, 2024
1 parent 5d1f232 commit f16bcf6
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/cmd/tools/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/defenseunicorns/go-oscal/src/pkg/files"
"github.com/defenseunicorns/lula/src/cmd/common"
"github.com/defenseunicorns/lula/src/internal/template"
pkgCommon "github.com/defenseunicorns/lula/src/pkg/common"
"github.com/defenseunicorns/lula/src/pkg/common/network"
"github.com/defenseunicorns/lula/src/pkg/message"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -46,7 +46,7 @@ func TemplateCommand() *cobra.Command {
Example: templateHelp,
RunE: func(cmd *cobra.Command, args []string) error {
// Read file
data, err := pkgCommon.ReadFileToBytes(inputFile)
data, err := network.Fetch(inputFile)
if err != nil {
return fmt.Errorf("error reading file: %v", err)
}
Expand Down
20 changes: 19 additions & 1 deletion src/internal/template/helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package template

import "fmt"
import (
"fmt"
"time"

"github.com/defenseunicorns/go-oscal/src/pkg/uuid"
)

func concatToRegoList(list []any) string {
regoList := ""
Expand All @@ -12,3 +17,16 @@ func concatToRegoList(list []any) string {
}
return regoList
}

func newUUID(source ...string) string {
if len(source) == 0 {
return uuid.NewUUID()
} else {
return uuid.NewUUIDWithSource(source[0])
}
}

func timestamp() string {
t := time.Now()
return t.Format("2006-01-02T15:04:05.999999999Z")
}
2 changes: 2 additions & 0 deletions src/internal/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,8 @@ func createTemplate() *template.Template {
"concatToRegoList": func(a []any) string {
return concatToRegoList(a)
},
"uuid": newUUID,
"timestamp": timestamp,
// Add more custom functions as needed
}

Expand Down

0 comments on commit f16bcf6

Please sign in to comment.