engine: Make paths relative to the CUE file they're defined in

Fixes #1309

Signed-off-by: Andrea Luzzardi <aluzzardi@gmail.com>
This commit is contained in:
Andrea Luzzardi
2021-12-23 19:09:26 +01:00
committed by Sam Alba
parent ec69734c51
commit 9c81a155c9
12 changed files with 134 additions and 30 deletions

View File

@@ -1,6 +1,9 @@
package compiler
import (
"errors"
"path"
"path/filepath"
"sort"
"strconv"
@@ -282,6 +285,43 @@ func (v *Value) HasAttr(filter ...string) bool {
return false
}
// Filename returns the CUE filename where the value was defined
func (v *Value) Filename() (string, error) {
pos := v.Cue().Pos()
if !pos.IsValid() {
return "", errors.New("invalid token position")
}
return pos.Filename(), nil
}
// Dirname returns the CUE dirname where the value was defined
func (v *Value) Dirname() (string, error) {
f, err := v.Filename()
if err != nil {
return "", err
}
return filepath.Dir(f), nil
}
// AbsPath returns an absolute path contained in Value
// Paths are relative to the CUE file they were declared in.
func (v *Value) AbsPath() (string, error) {
p, err := v.String()
if err != nil {
return "", nil
}
if filepath.IsAbs(p) {
return p, nil
}
d, err := v.Dirname()
if err != nil {
return "", err
}
return path.Join(d, p), nil
}
func (v *Value) Dereference() *Value {
dVal := cue.Dereference(v.val)
return v.cc.Wrap(dVal)