dagger do action options flags

Signed-off-by: Joel Longtine <joel@dagger.io>
This commit is contained in:
Joel Longtine
2022-03-28 13:47:42 -06:00
parent 069227e30c
commit dae0ee1d1e
10 changed files with 341 additions and 46 deletions

View File

@@ -2,6 +2,7 @@ package plan
import (
"cuelang.org/go/cue"
"go.dagger.io/dagger/compiler"
)
type Action struct {
@@ -10,6 +11,14 @@ type Action struct {
Path cue.Path
Documentation string
Children []*Action
Value *compiler.Value
inputs []Input
}
type Input struct {
Name string
Type string
Documentation string
}
func (a *Action) AddChild(c *Action) {
@@ -31,3 +40,63 @@ func (a *Action) FindByPath(path cue.Path) *Action {
}
return nil
}
func (a *Action) FindClosest(path cue.Path) *Action {
if a.Path.String() == path.String() {
return a
}
commonSubPath := commonSubPath(a.Path, path)
if commonSubPath.String() == a.Path.String() {
if len(a.Children) > 0 {
for _, c := range a.Children {
if c.Path.String() == path.String() {
return c.FindClosest(path)
}
}
}
return a
}
return nil
}
func commonSubPath(a, b cue.Path) cue.Path {
if a.String() == b.String() {
return a
}
aSelectors := a.Selectors()
bSelectors := b.Selectors()
commonSelectors := []cue.Selector{}
for i := 0; i < len(aSelectors) && i < len(bSelectors); i++ {
if aSelectors[i].String() != bSelectors[i].String() {
break
}
commonSelectors = append(commonSelectors, aSelectors[i])
}
return cue.MakePath(commonSelectors...)
}
func (a *Action) Inputs() []Input {
if a.inputs == nil {
cueVal := a.Value.Cue()
inputs := []Input{}
for iter, _ := cueVal.Fields(cue.All()); iter.Next(); {
val := iter.Value()
cVal := compiler.Wrap(val)
_, refPath := val.ReferencePath()
ik := val.IncompleteKind()
validKind := ik == cue.StringKind || ik == cue.NumberKind || ik == cue.BoolKind || ik == cue.IntKind || ik == cue.FloatKind
if validKind && !val.IsConcrete() && len(refPath.Selectors()) == 0 {
inputs = append(inputs, Input{
Name: iter.Label(),
Type: ik.String(),
Documentation: cVal.DocSummary(),
})
}
}
a.inputs = inputs
}
return a.inputs
}

23
plan/action_test.go Normal file
View File

@@ -0,0 +1,23 @@
package plan
import (
"testing"
"cuelang.org/go/cue"
"github.com/stretchr/testify/require"
)
func TestClosestSubPath(t *testing.T) {
rootPath := cue.MakePath(ActionSelector, cue.Str("test"))
path1 := cue.MakePath(ActionSelector, cue.Str("test"), cue.Str("one"))
path2 := cue.MakePath(ActionSelector, cue.Str("test"), cue.Str("two"))
require.Equal(t, "actions.test.one", path1.String())
require.Equal(t, "actions.test.two", path2.String())
require.Equal(t, "actions.test", commonSubPath(rootPath, path1).String())
require.Equal(t, "actions.test", commonSubPath(path1, path2).String())
path3 := cue.MakePath(ActionSelector, cue.Str("test"), cue.Str("golang"), cue.Str("three"))
path4 := cue.MakePath(ActionSelector, cue.Str("test"), cue.Str("java"), cue.Str("three"))
require.Equal(t, "actions.test", commonSubPath(path3, path4).String())
}

View File

@@ -191,18 +191,20 @@ func (p *Plan) fillAction() {
noOpRunner,
)
p.action = &Action{
Name: ActionSelector.String(),
Hidden: false,
Path: cue.MakePath(ActionSelector),
Children: []*Action{},
}
actions := p.source.LookupPath(cue.MakePath(ActionSelector))
actionsPath := cue.MakePath(ActionSelector)
actions := p.source.LookupPath(actionsPath)
if !actions.Exists() {
return
}
p.action.Documentation = actions.DocSummary()
p.action = &Action{
Name: ActionSelector.String(),
Documentation: actions.DocSummary(),
Hidden: false,
Path: actionsPath,
Children: []*Action{},
Value: p.Source().LookupPath(actionsPath),
}
tasks := flow.Tasks()
@@ -221,6 +223,7 @@ func (p *Plan) fillAction() {
Path: path,
Documentation: v.DocSummary(),
Children: []*Action{},
Value: v,
}
prevAction.AddChild(a)
}