Skip to content

Commit

Permalink
hof/runtime: add support for @userFiles to load content
Browse files Browse the repository at this point in the history
  • Loading branch information
Tony Worm committed May 4, 2024
1 parent 8d4daf6 commit 5fe6171
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
39 changes: 38 additions & 1 deletion lib/runtime/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ func (R *Runtime) prepPlacedUserfiles() error {
if len(parts) != 2 {
return fmt.Errorf("-U/--user-files only supports one % to trim prefix")
}
trimPath = parts[0] + "/"
trimPath = parts[0]
if !strings.HasSuffix(trimPath, "/") {
trimPath += "/"
}
filePath = strings.Replace(filePath, "%", "/", 1)
}

Expand All @@ -211,6 +214,40 @@ func (R *Runtime) prepPlacedUserfiles() error {
}

// look for @userfiles attributes and do similar
kvals, err := cuetils.GetByAttrKeys(R.Value, "userfiles", nil, nil)
if err != nil {
return err
}
for _, kval := range kvals {
cuePath := kval.Key
trimPath := ""
filePath := ""
attrs := kval.Val.Attributes(cue.ValueAttr)
for _, A := range attrs {
if A.Name() == "userfiles" {
for i := 0; i < A.NumArgs(); i++ {
k,v := A.Arg(i)
if k == "trim" {
trimPath = v
if !strings.HasSuffix(trimPath, "/") {
trimPath += "/"
}
}
if v == "" {
filePath = k
}
}

// fmt.Println(cuePath, trimPath, filePath)
err := embedFiles(cuePath, trimPath, filePath)
if err != nil {
return err
}

break
}
}
}

if R.Flags.Verbosity > 1 {
fmt.Println("user files:", R.userFiles)
Expand Down
4 changes: 3 additions & 1 deletion test/userfiles/main.cue
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ package main

hello: "world"

files: {}
files: {
@userfiles(content,trim=content)
}

0 comments on commit 5fe6171

Please sign in to comment.