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

@@ -5,7 +5,6 @@ import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"
@@ -97,16 +96,12 @@ func (p *Plan) registerLocalDirs() error {
}
for _, v := range imports {
dir, err := v.Value.Lookup("path").String()
dir, err := v.Value.Lookup("path").AbsPath()
if err != nil {
return err
}
abs, err := filepath.Abs(dir)
if err != nil {
return err
}
if _, err := os.Stat(abs); errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("path %q does not exist", abs)
if _, err := os.Stat(dir); errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("path %q does not exist", dir)
}
p.context.LocalDirs.Add(dir)
}

View File

@@ -18,8 +18,12 @@ type inputDirectoryTask struct {
}
func (c *inputDirectoryTask) Run(ctx context.Context, pctx *plancontext.Context, s solver.Solver, v *compiler.Value) (*compiler.Value, error) {
path, err := v.Lookup("path").AbsPath()
if err != nil {
return nil, err
}
var dir struct {
Path string
Include []string
Exclude []string
}
@@ -29,13 +33,13 @@ func (c *inputDirectoryTask) Run(ctx context.Context, pctx *plancontext.Context,
}
lg := log.Ctx(ctx)
lg.Debug().Str("path", dir.Path).Msg("loading local directory")
lg.Debug().Str("path", path).Msg("loading local directory")
opts := []llb.LocalOption{
withCustomName(v, "Local %s", dir.Path),
withCustomName(v, "Local %s", path),
// Without hint, multiple `llb.Local` operations on the
// same path get a different digest.
llb.SessionID(s.SessionID()),
llb.SharedKeyHint(dir.Path),
llb.SharedKeyHint(path),
}
if len(dir.Include) > 0 {
@@ -56,13 +60,13 @@ func (c *inputDirectoryTask) Run(ctx context.Context, pctx *plancontext.Context,
st := llb.Scratch().File(
llb.Copy(
llb.Local(
dir.Path,
path,
opts...,
),
"/",
"/",
),
withCustomName(v, "Local %s [copy]", dir.Path),
withCustomName(v, "Local %s [copy]", path),
)
result, err := s.Solve(ctx, st, pctx.Platform.Get())

View File

@@ -3,6 +3,7 @@ package task
import (
"context"
"fmt"
"os"
"os/exec"
"strings"
@@ -35,8 +36,18 @@ func (c *inputSecretExecTask) Run(ctx context.Context, pctx *plancontext.Context
lg := log.Ctx(ctx)
lg.Debug().Str("name", secretExec.Command.Name).Str("args", strings.Join(secretExec.Command.Args, " ")).Str("trimSpace", fmt.Sprintf("%t", secretExec.TrimSpace)).Msg("loading secret")
var err error
//#nosec G204: sec audited by @aluzzardi and @mrjones
cmd := exec.CommandContext(ctx, secretExec.Command.Name, secretExec.Command.Args...)
cmd.Env = os.Environ()
cmd.Dir, err = v.Lookup("command.name").Dirname()
if err != nil {
return nil, err
}
// sec audited by @aluzzardi and @mrjones
out, err := exec.CommandContext(ctx, secretExec.Command.Name, secretExec.Command.Args...).Output() //#nosec G204
out, err := cmd.Output()
if err != nil {
return nil, err
}

View File

@@ -2,7 +2,6 @@ package task
import (
"context"
"fmt"
"os"
"strings"
@@ -20,25 +19,26 @@ type inputSecretFileTask struct {
}
func (c *inputSecretFileTask) Run(ctx context.Context, pctx *plancontext.Context, _ solver.Solver, v *compiler.Value) (*compiler.Value, error) {
var secretFile struct {
Path string
TrimSpace bool
}
lg := log.Ctx(ctx)
if err := v.Decode(&secretFile); err != nil {
path, err := v.Lookup("path").AbsPath()
if err != nil {
return nil, err
}
lg := log.Ctx(ctx)
lg.Debug().Str("path", secretFile.Path).Str("trimSpace", fmt.Sprintf("%t", secretFile.TrimSpace)).Msg("loading secret")
lg.Debug().Str("path", path).Msg("loading secret")
fileBytes, err := os.ReadFile(secretFile.Path)
fileBytes, err := os.ReadFile(path)
if err != nil {
return nil, err
}
plaintext := string(fileBytes)
if secretFile.TrimSpace {
trimSpace, err := v.Lookup("trimSpace").Bool()
if err != nil {
return nil, err
}
if trimSpace {
plaintext = strings.TrimSpace(plaintext)
}

View File

@@ -22,7 +22,7 @@ func (c outputDirectoryTask) Run(ctx context.Context, pctx *plancontext.Context,
return nil, err
}
dest, err := v.Lookup("dest").String()
dest, err := v.Lookup("dest").AbsPath()
if err != nil {
return nil, err
}