Skip to content

Commit

Permalink
Also fix relative path normalization
Browse files Browse the repository at this point in the history
When the bundle file is passed via stdin (i.e. `-f -`), a `file://` with
a relative path would be normalized to the tmpfolder where stdin was
saved.

This fixes it.
  • Loading branch information
huguesalary committed Oct 3, 2024
1 parent a858e4e commit 2b7c953
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
9 changes: 8 additions & 1 deletion cmd/timoni/bundle_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,15 @@ func runBundleApplyCmd(cmd *cobra.Command, _ []string) error {
ctx, cancel := context.WithTimeout(cmd.Context(), rootArgs.timeout)
defer cancel()

// Figure out the current working directory
// Required by the BundleBuilder for resolving relative paths
cwd, err := os.Getwd()
if err != nil {
return err
}

cuectx := cuecontext.New()
bm := engine.NewBundleBuilder(cuectx, files)
bm := engine.NewBundleBuilder(cuectx, files, cwd)

runtimeValues := make(map[string]string)

Expand Down
9 changes: 8 additions & 1 deletion cmd/timoni/bundle_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,15 @@ func runBundleBuildCmd(cmd *cobra.Command, _ []string) error {
}
defer os.RemoveAll(tmpDir)

// Figure out the current working directory
// Required by the BundleBuilder for resolving relative paths
cwd, err := os.Getwd()
if err != nil {
return err
}

ctx := cuecontext.New()
bm := engine.NewBundleBuilder(ctx, files)
bm := engine.NewBundleBuilder(ctx, files, cwd)

runtimeValues := make(map[string]string)

Expand Down
9 changes: 8 additions & 1 deletion cmd/timoni/bundle_vet.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,15 @@ func runBundleVetCmd(cmd *cobra.Command, args []string) error {
}
defer os.RemoveAll(tmpDir)

// Figure out the current working directory
// Required by the BundleBuilder for resolving relative paths
cwd, err := os.Getwd()
if err != nil {
return err
}

cuectx := cuecontext.New()
bm := engine.NewBundleBuilder(cuectx, files)
bm := engine.NewBundleBuilder(cuectx, files, cwd)

runtimeValues := make(map[string]string)

Expand Down
29 changes: 12 additions & 17 deletions internal/engine/bundle_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ import (

// BundleBuilder compiles CUE definitions to Go Bundle objects.
type BundleBuilder struct {
ctx *cue.Context
files []string
workspaceFiles map[string][]string
mapSourceToOrigin map[string]string
injector *RuntimeInjector
ctx *cue.Context
cwd string
files []string
workspaceFiles map[string][]string
injector *RuntimeInjector
}

type Bundle struct {
Expand All @@ -57,16 +57,16 @@ type BundleInstance struct {
}

// NewBundleBuilder creates a BundleBuilder for the given module and package.
func NewBundleBuilder(ctx *cue.Context, files []string) *BundleBuilder {
func NewBundleBuilder(ctx *cue.Context, files []string, cwd string) *BundleBuilder {
if ctx == nil {
ctx = cuecontext.New()
}
b := &BundleBuilder{
ctx: ctx,
files: files,
workspaceFiles: make(map[string][]string),
mapSourceToOrigin: make(map[string]string, len(files)),
injector: NewRuntimeInjector(ctx),
ctx: ctx,
cwd: cwd,
files: files,
workspaceFiles: make(map[string][]string),
injector: NewRuntimeInjector(ctx),
}
return b
}
Expand Down Expand Up @@ -113,7 +113,6 @@ func (b *BundleBuilder) InitWorkspace(workspace string, runtimeValues map[string
if err := os.WriteFile(dstFile, data, os.ModePerm); err != nil {
return fmt.Errorf("failed to write %s: %w", fn, err)
}
b.mapSourceToOrigin[dstFile] = file

files = append(files, dstFile)
}
Expand Down Expand Up @@ -162,11 +161,7 @@ func (b *BundleBuilder) Build(workspace string) (cue.Value, error) {
func (b *BundleBuilder) getInstanceUrl(v cue.Value) string {
url, _ := v.String()
if path := strings.TrimPrefix(url, apiv1.LocalPrefix); IsFileUrl(url) && !filepath.IsAbs(path) {
source := v.Pos().Filename()
if origin, ok := b.mapSourceToOrigin[source]; ok {
source = origin
}
url = apiv1.LocalPrefix + filepath.Clean(filepath.Join(filepath.Dir(source), path))
url = apiv1.LocalPrefix + filepath.Clean(filepath.Join(b.cwd, path))
}
return url
}
Expand Down
2 changes: 1 addition & 1 deletion internal/engine/bundle_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ bundle: {
}
`
v := ctx.CompileString(bundle)
builder := NewBundleBuilder(ctx, []string{})
builder := NewBundleBuilder(ctx, []string{}, "")
b, err := builder.GetBundle(v)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(b.Instances).To(HaveLen(2))
Expand Down

0 comments on commit 2b7c953

Please sign in to comment.