Skip to content

Commit

Permalink
Add interactive mode, fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
TomWright committed Oct 29, 2024
1 parent bbc873f commit de29bd2
Show file tree
Hide file tree
Showing 30 changed files with 817 additions and 132 deletions.
27 changes: 3 additions & 24 deletions execution/execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,30 +135,9 @@ func exprExecutor(opts *Options, expr ast.Expr) (expressionExecutor, error) {
func chainedExprExecutor(options *Options, e ast.ChainedExpr) (expressionExecutor, error) {
return func(data *model.Value) (*model.Value, error) {
for _, expr := range e.Exprs {

if !data.IsBranch() {
res, err := ExecuteAST(expr, data, options)
if err != nil {
return nil, fmt.Errorf("error executing expression: %w", err)
}
data = res
continue
}

res := model.NewSliceValue()
res.MarkAsBranch()
if err := data.RangeSlice(func(i int, value *model.Value) error {
r, err := ExecuteAST(expr, value, options)
if err != nil {
return fmt.Errorf("error executing expression: %w", err)
}

if err := res.Append(r); err != nil {
return err
}
return nil
}); err != nil {
return nil, err
res, err := ExecuteAST(expr, data, options)
if err != nil {
return nil, fmt.Errorf("error executing expression: %w", err)
}
data = res
}
Expand Down
9 changes: 8 additions & 1 deletion execution/execute_binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ func basicBinaryExpressionExecutorFn(handler func(left *model.Value, right *mode
if err != nil {
return nil, fmt.Errorf("error evaluating right expression: %w", err)
}
return handler(left, right, expr)
res, err := handler(left, right, expr)
if err != nil {
return nil, err
}
return res, nil
}

res := model.NewSliceValue()
Expand Down Expand Up @@ -52,6 +56,9 @@ var binaryExpressionExecutors = map[lexer.TokenKind]binaryExpressionExecutorFn{}

func binaryExprExecutor(opts *Options, e ast.BinaryExpr) (expressionExecutor, error) {
return func(data *model.Value) (*model.Value, error) {
if e.Left == nil || e.Right == nil {
return nil, fmt.Errorf("left and right expressions must be provided")
}

exec, ok := binaryExpressionExecutors[e.Operator.Kind]
if !ok {
Expand Down
1 change: 1 addition & 0 deletions execution/execute_branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func branchExprExecutor(opts *Options, e ast.BranchExpr) (expressionExecutor, er
res.MarkAsBranch()

if len(e.Exprs) == 0 {
// No expressions given. We'll branch on the input data.
if err := data.RangeSlice(func(_ int, value *model.Value) error {
if err := res.Append(value); err != nil {
return fmt.Errorf("failed to append branch result: %w", err)
Expand Down
20 changes: 20 additions & 0 deletions execution/execute_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,24 @@ func TestBranch(t *testing.T) {
execution.WithUnstable(),
},
}.run)
t.Run("map on branch", testCase{
s: `branch([1], [2], [3]).map($this * 2).branch()`,
outFn: func() *model.Value {
r := model.NewSliceValue()
r.MarkAsBranch()
if err := r.Append(model.NewIntValue(2)); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if err := r.Append(model.NewIntValue(4)); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if err := r.Append(model.NewIntValue(6)); err != nil {
t.Fatalf("unexpected error: %v", err)
}
return r
},
opts: []execution.ExecuteOptionFn{
execution.WithUnstable(),
},
}.run)
}
1 change: 0 additions & 1 deletion execution/execute_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ func mapExprExecutor(opts *Options, e ast.MapExpr) (expressionExecutor, error) {
}); err != nil {
return nil, fmt.Errorf("error ranging over slice: %w", err)
}

return res, nil
}, nil
}
13 changes: 11 additions & 2 deletions execution/execute_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package execution_test
import (
"testing"

"github.com/tomwright/dasel/v3/internal/ptr"
"github.com/tomwright/dasel/v3/model"
"github.com/tomwright/dasel/v3/model/orderedmap"
)
Expand Down Expand Up @@ -38,7 +37,17 @@ func TestMap(t *testing.T) {
)
.map ( total )`,
outFn: func() *model.Value {
return model.NewValue([]any{ptr.To(int64(6)), ptr.To(int64(8)), ptr.To(int64(10))})
res := model.NewSliceValue()
if err := res.Append(model.NewValue(6)); err != nil {
t.Fatal(err)
}
if err := res.Append(model.NewValue(8)); err != nil {
t.Fatal(err)
}
if err := res.Append(model.NewValue(10)); err != nil {
t.Fatal(err)
}
return res
},
}.run)
}
2 changes: 1 addition & 1 deletion execution/execute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (tc testCase) run(t *testing.T) {
t.Fatal(err)
}
if !equal {
t.Errorf("unexpected output: %v", cmp.Diff(exp.Interface(), res.Interface()))
t.Errorf("unexpected output: %v\nexp: %s\ngot: %s", cmp.Diff(exp.Interface(), res.Interface()), exp.String(), res.String())
}

expMeta := exp.Metadata
Expand Down
23 changes: 23 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,26 @@ require (
github.com/pelletier/go-toml/v2 v2.2.2
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/bubbles v0.20.0 // indirect
github.com/charmbracelet/bubbletea v1.1.2 // indirect
github.com/charmbracelet/lipgloss v0.13.1 // indirect
github.com/charmbracelet/x/ansi v0.4.0 // indirect
github.com/charmbracelet/x/term v0.2.0 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.26.0 // indirect
golang.org/x/text v0.3.8 // indirect
)
47 changes: 47 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,56 @@ github.com/alecthomas/kong v1.2.1 h1:E8jH4Tsgv6wCRX2nGrdPyHDUCSG83WH2qE4XLACD33Q
github.com/alecthomas/kong v1.2.1/go.mod h1:rKTSFhbdp3Ryefn8x5MOEprnRFQ7nlmMC01GKhehhBM=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/bubbles v0.20.0 h1:jSZu6qD8cRQ6k9OMfR1WlM+ruM8fkPWkHvQWD9LIutE=
github.com/charmbracelet/bubbles v0.20.0/go.mod h1:39slydyswPy+uVOHZ5x/GjwVAFkCsV8IIVy+4MhzwwU=
github.com/charmbracelet/bubbletea v1.1.2 h1:naQXF2laRxyLyil/i7fxdpiz1/k06IKquhm4vBfHsIc=
github.com/charmbracelet/bubbletea v1.1.2/go.mod h1:9HIU/hBV24qKjlehyj8z1r/tR9TYTQEag+cWZnuXo8E=
github.com/charmbracelet/lipgloss v0.13.0 h1:4X3PPeoWEDCMvzDvGmTajSyYPcZM4+y8sCA/SsA3cjw=
github.com/charmbracelet/lipgloss v0.13.0/go.mod h1:nw4zy0SBX/F/eAO1cWdcvy6qnkDUxr8Lw7dvFrAIbbY=
github.com/charmbracelet/lipgloss v0.13.1 h1:Oik/oqDTMVA01GetT4JdEC033dNzWoQHdWnHnQmXE2A=
github.com/charmbracelet/lipgloss v0.13.1/go.mod h1:zaYVJ2xKSKEnTEEbX6uAHabh2d975RJ+0yfkFpRBz5U=
github.com/charmbracelet/x/ansi v0.4.0 h1:NqwHA4B23VwsDn4H3VcNX1W1tOmgnvY1NDx5tOXdnOU=
github.com/charmbracelet/x/ansi v0.4.0/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw=
github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4hwm0x0=
github.com/charmbracelet/x/term v0.2.0/go.mod h1:GVxgxAbjUrmpvIINHIQnJJKpMlHiZ4cktEQCN6GWyF0=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
Expand All @@ -24,6 +63,14 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo=
golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down
6 changes: 4 additions & 2 deletions internal/cli/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ type Globals struct {
type CLI struct {
Globals

Query QueryCmd `cmd:"" default:"withargs" help:"[default] Execute a query"`
Version VersionCmd `cmd:"" help:"Print the version"`
Query QueryCmd `cmd:"" default:"withargs" help:"[default] Execute a query"`
Version VersionCmd `cmd:"" help:"Print the version"`
Interactive InteractiveCmd `cmd:"" help:"Start an interactive session"`
}

func MustRun(stdin io.Reader, stdout, stderr io.Writer) {
Expand Down Expand Up @@ -46,6 +47,7 @@ func Run(stdin io.Reader, stdout, stderr io.Writer) (*kong.Context, error) {
},
kong.Bind(&cli.Globals),
kong.TypeMapper(reflect.TypeFor[*[]variable](), &variableMapper{}),
kong.TypeMapper(reflect.TypeFor[*[]extReadWriteFlag](), &extReadWriteFlagMapper{}),
kong.OptionFunc(func(k *kong.Kong) error {
k.Stdout = cli.Stdout
k.Stderr = cli.Stderr
Expand Down
Loading

0 comments on commit de29bd2

Please sign in to comment.