Skip to content

Commit

Permalink
feat(task/client): #Module to return cue mod info
Browse files Browse the repository at this point in the history
  • Loading branch information
morlay committed Jun 13, 2024
1 parent 3e6e200 commit 1a6948d
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/k0sproject/rig v0.18.2
github.com/kevinburke/ssh_config v1.2.0
github.com/octohelm/crkit v0.0.0-20240529094907-e4e05776f231
github.com/octohelm/cuekit v0.0.0-20240610074644-0a72a4989f70
github.com/octohelm/cuekit v0.0.0-20240613043008-8b18a0bf3e6a
github.com/octohelm/gengo v0.0.0-20240510051519-974fb897453b
github.com/octohelm/kubekit v0.0.0-20240508035712-15cb61729772
github.com/octohelm/kubepkgspec v0.0.0-20240521102121-31a405691640
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,8 @@ github.com/octohelm/courier v0.0.0-20240527074539-a9eb7212792b h1:4La1JkCW4mZLVH
github.com/octohelm/courier v0.0.0-20240527074539-a9eb7212792b/go.mod h1:lYPuTnuOSyTHOweXCoDQbPphAZ00ZMRtctUTGSI0YNE=
github.com/octohelm/crkit v0.0.0-20240529094907-e4e05776f231 h1:KZzDM+KaoJwUg2SeonECwkJs8YeMg28WXBBKR/Fxqog=
github.com/octohelm/crkit v0.0.0-20240529094907-e4e05776f231/go.mod h1:FYJzLtshzNdqgqaptd5KVWZiVFIbLipGlpVY+G0QHwA=
github.com/octohelm/cuekit v0.0.0-20240610074644-0a72a4989f70 h1:X+tk/uCJ4lA6EVQ3vAhSINjvqqqBDBbm0IuyJg3W+QM=
github.com/octohelm/cuekit v0.0.0-20240610074644-0a72a4989f70/go.mod h1:zRFJiO6/WgqKc3Qr1TKrGeJf4r7bKTFVK71mxOfaS6k=
github.com/octohelm/cuekit v0.0.0-20240613043008-8b18a0bf3e6a h1:mPUiSxvFgmcRO/VR4I3Ym6drNQjf2Zmr4CaHKeBcdVY=
github.com/octohelm/cuekit v0.0.0-20240613043008-8b18a0bf3e6a/go.mod h1:zRFJiO6/WgqKc3Qr1TKrGeJf4r7bKTFVK71mxOfaS6k=
github.com/octohelm/gengo v0.0.0-20240510051519-974fb897453b h1:z/XJuBmwnxoPPMl2mY11CsU2w8yiLq5q3bwdmkrPCWo=
github.com/octohelm/gengo v0.0.0-20240510051519-974fb897453b/go.mod h1:mlnC4bXnp0RdKxBf1u4bFQ0pmyDHUfXI9czal6K8lS0=
github.com/octohelm/kubekit v0.0.0-20240508035712-15cb61729772 h1:X+mCc0CTxFo0BMMVeGBEDudulp2fGiftUD9rJS9OmOQ=
Expand Down
13 changes: 10 additions & 3 deletions pkg/cueflow/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"fmt"
"github.com/octohelm/cuekit/pkg/mod/module"
"io"
"sort"
"strconv"
Expand Down Expand Up @@ -34,7 +35,7 @@ import (
"github.com/octohelm/piper/pkg/generic/record"
)

func NewRunner(build func() (Value, error)) *Runner {
func NewRunner(build func() (Value, *module.Module, error)) *Runner {
return &Runner{
build: build,
}
Expand All @@ -45,8 +46,9 @@ type scope struct {
}

type Runner struct {
build func() (Value, error)
build func() (Value, *module.Module, error)
root atomic.Pointer[scope]
module atomic.Pointer[module.Module]
taskResult record.Map[string, any]

match func(p string) bool
Expand All @@ -59,6 +61,10 @@ type Runner struct {
activeTargets map[string][]string
}

func (r *Runner) Module() *module.Module {
return r.module.Load()
}

func (r *Runner) Value() Value {
return r.root.Load().Value
}
Expand Down Expand Up @@ -171,10 +177,11 @@ func (r *Runner) run(ctx context.Context) error {
}

func (r *Runner) init() error {
rootValue, err := r.build()
rootValue, mod, err := r.build()
if err != nil {
return err
}
r.module.Store(mod)
r.root.Store(&scope{Value: rootValue})
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/cueflow/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package cueflow

import (
"context"
"github.com/octohelm/cuekit/pkg/mod/module"

"cuelang.org/go/cue"
)

type Scope interface {
Value() Value
Module() *module.Module
LookupPath(path cue.Path) Value
FillPath(path cue.Path, value any) error
Processed(path cue.Path) bool
Expand Down
11 changes: 6 additions & 5 deletions pkg/engine/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package engine

import (
"context"
"github.com/octohelm/cuekit/pkg/mod/module"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -92,18 +93,18 @@ type project struct {
}

func (p *project) Run(ctx context.Context, action ...string) error {
runner := cueflow.NewRunner(func() (cueflow.Value, error) {
runner := cueflow.NewRunner(func() (cueflow.Value, *module.Module, error) {
buildConfig, err := cuecontext.NewConfig(cuecontext.WithRoot(p.opt.cwd))
if err != nil {
return nil, err
return nil, nil, err
}

val, err := cuecontext.Build(buildConfig, p.opt.entry)
val, err := cuecontext.Build(buildConfig.Config, p.opt.entry)
if err != nil {
return nil, err
return nil, nil, err
}

return cueflow.WrapValue(val), nil
return cueflow.WrapValue(val), buildConfig.Module, nil
})

ctx = cueflow.TaskRunnerFactoryContext.Inject(ctx, task.Factory)
Expand Down
41 changes: 41 additions & 0 deletions pkg/engine/task/client/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package client

import (
"context"

"github.com/octohelm/piper/pkg/cueflow"
"github.com/octohelm/piper/pkg/engine/task"
)

func init() {
cueflow.RegisterTask(task.Factory, &Module{})
}

type Module struct {
task.Task
t cueflow.Task

// root module
Module string `json:"-" output:"module"`
// { dep: version }
Deps map[string]string `json:"-" output:"deps"`
}

var _ cueflow.TaskUnmarshaler = &Module{}

func (v *Module) UnmarshalTask(t cueflow.Task) error {
v.t = t
return nil
}

func (t *Module) Do(ctx context.Context) error {
m := t.t.Scope().Module()

t.Module = m.Module
t.Deps = map[string]string{}
for name, dep := range m.Deps {
t.Deps[name] = dep.Version
}

return nil
}

0 comments on commit 1a6948d

Please sign in to comment.