Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ dune usage [--start-date YYYY-MM-DD] [--end-date YYYY-MM-DD]
## Output Format

All commands (except `auth`) support `-o, --output <format>` with `text` (default) or `json`.
Add `--compact` to print the JSON on one line without indentation, which cuts its size by about a third for agents and scripts.

`dataset search` and `dataset search-by-contract` list each result's columns under `COLUMNS` in text output: the top 3 hits of `dataset search` always, and every result with `--include-schema`.

## How the CLI talks to Dune

Expand Down
2 changes: 1 addition & 1 deletion cmd/contract/submissions.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func runSubmissions(cmd *cobra.Command, _ []string) error {
}
w := cmd.OutOrStdout()
if output.FormatFromCmd(cmd) == output.FormatJSON {
return output.PrintJSON(w, res)
return output.PrintJSON(cmd, res)
}
if len(res.Submissions) == 0 {
fmt.Fprintln(w, "No submissions found.")
Expand Down
2 changes: 1 addition & 1 deletion cmd/contract/submit.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func runSubmit(cmd *cobra.Command, _ []string) error {
}
w := cmd.OutOrStdout()
if output.FormatFromCmd(cmd) == output.FormatJSON {
if err := output.PrintJSON(w, res); err != nil {
if err := output.PrintJSON(cmd, res); err != nil {
return err
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion cmd/dashboard/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func runArchive(cmd *cobra.Command, args []string) error {
w := cmd.OutOrStdout()
switch output.FormatFromCmd(cmd) {
case output.FormatJSON:
return output.PrintJSON(w, resp)
return output.PrintJSON(cmd, resp)
default:
fmt.Fprintf(w, "Archived dashboard %d\n", dashboardID)
return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/dashboard/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func runCreate(cmd *cobra.Command, _ []string) error {
w := cmd.OutOrStdout()
switch output.FormatFromCmd(cmd) {
case output.FormatJSON:
return output.PrintJSON(w, resp)
return output.PrintJSON(cmd, resp)
default:
fmt.Fprintf(w, "Created dashboard %d\n%s\n", resp.DashboardID, resp.DashboardURL)
return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/dashboard/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func runGet(cmd *cobra.Command, args []string) error {
w := cmd.OutOrStdout()
switch output.FormatFromCmd(cmd) {
case output.FormatJSON:
return output.PrintJSON(w, resp)
return output.PrintJSON(cmd, resp)
default:
tags := ""
if len(resp.Tags) > 0 {
Expand Down
2 changes: 1 addition & 1 deletion cmd/dashboard/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func runSearch(cmd *cobra.Command, _ []string) error {
}
w := cmd.OutOrStdout()
if output.FormatFromCmd(cmd) == output.FormatJSON {
return output.PrintRawJSON(w, raw)
return output.PrintRawJSON(cmd, raw)
}
if len(res.Results) == 0 {
fmt.Fprintln(w, "No dashboards found.")
Expand Down
2 changes: 1 addition & 1 deletion cmd/dashboard/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func runUpdate(cmd *cobra.Command, args []string) error {
w := cmd.OutOrStdout()
switch output.FormatFromCmd(cmd) {
case output.FormatJSON:
return output.PrintJSON(w, resp)
return output.PrintJSON(cmd, resp)
default:
fmt.Fprintf(w, "Updated dashboard %d\n", resp.DashboardID)
return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/dataset/blockchains.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func runBlockchains(cmd *cobra.Command, _ []string) error {
}
w := cmd.OutOrStdout()
if output.FormatFromCmd(cmd) == output.FormatJSON {
return output.PrintRawJSON(w, raw)
return output.PrintRawJSON(cmd, raw)
}
rows := make([][]string, len(res.Results))
for i, r := range res.Results {
Expand Down
45 changes: 45 additions & 0 deletions cmd/dataset/columns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package dataset

import (
"encoding/json"
"fmt"
"io"
"strings"
)

// tableColumns returns a table's columns as "name type" strings. It reads the
// Dune MCP's columns list ([{"name","type",...}]) and falls back to the API's
// --include-schema field ({"fields":[{"name","type",...}]}).
func tableColumns(columns, schema json.RawMessage) []string {
type column struct{ Name, Type string }
var cols []column
if len(columns) > 0 {
_ = json.Unmarshal(columns, &cols)
}
if len(cols) == 0 && len(schema) > 0 {
var s struct{ Fields []column }
_ = json.Unmarshal(schema, &s)
cols = s.Fields
}
out := make([]string, len(cols))
for i, c := range cols {
out[i] = c.Name + " " + c.Type
}
return out
}

// printColumns lists the columns of each table that has them, one table per
// line, so text output carries what an agent needs to write SQL.
func printColumns(w io.Writer, names []string, columns [][]string) {
header := false
for i, cols := range columns {
if len(cols) == 0 {
continue
}
if !header {
fmt.Fprintln(w, "\nCOLUMNS")
header = true
}
fmt.Fprintf(w, "%s: %s\n", names[i], strings.Join(cols, ", "))
}
}
10 changes: 7 additions & 3 deletions cmd/dataset/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func newSearchCmd() *cobra.Command {
" - community: user-contributed tables and uploads\n\n" +
"Filter by category, blockchain, schema, dataset type, or ownership scope. Each\n" +
"result lists the columns the table is partitioned by: filter on them in SQL (e.g.\n" +
"WHERE block_month >= DATE '2026-01-01') to cut query cost. With -o json the top 3 hits also carry\n" +
"their columns and types; --include-schema adds the schema of every result.\n\n" +
"WHERE block_month >= DATE '2026-01-01') to cut query cost. The top 3 hits also list\n" +
"their columns and types; --include-schema lists them for every result.\n\n" +
"For contract-specific lookup, use 'dune dataset search-by-contract' instead.\n\n" +
"Examples:\n" +
" dune dataset search --query \"uniswap swaps\"\n" +
Expand Down Expand Up @@ -119,11 +119,14 @@ func runSearch(cmd *cobra.Command, _ []string) error {
w := cmd.OutOrStdout()
switch output.FormatFromCmd(cmd) {
case output.FormatJSON:
return output.PrintJSON(w, resp)
return output.PrintJSON(cmd, resp)
default:
columns := []string{"FULL_NAME", "CATEGORY", "DATASET_TYPE", "PARTITIONED_BY", "BLOCKCHAINS"}
rows := make([][]string, len(resp.Results))
names := make([]string, len(resp.Results))
cols := make([][]string, len(resp.Results))
for i, r := range resp.Results {
names[i], cols[i] = r.FullName, tableColumns(r.Columns, r.Schema)
dt := ""
if r.DatasetType != nil {
dt = *r.DatasetType
Expand All @@ -137,6 +140,7 @@ func runSearch(cmd *cobra.Command, _ []string) error {
}
}
output.PrintTable(w, columns, rows)
printColumns(w, names, cols)
fmt.Fprintf(w, "\n%d of %d results\n", len(resp.Results), resp.Total)
return nil
}
Expand Down
6 changes: 5 additions & 1 deletion cmd/dataset/search_by_contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,14 @@ func runSearchByContract(cmd *cobra.Command, _ []string) error {
w := cmd.OutOrStdout()
switch output.FormatFromCmd(cmd) {
case output.FormatJSON:
return output.PrintJSON(w, resp)
return output.PrintJSON(cmd, resp)
default:
columns := []string{"FULL_NAME", "CATEGORY", "DATASET_TYPE", "BLOCKCHAINS"}
rows := make([][]string, len(resp.Results))
names := make([]string, len(resp.Results))
cols := make([][]string, len(resp.Results))
for i, r := range resp.Results {
names[i], cols[i] = r.FullName, tableColumns(nil, r.Schema)
dt := ""
if r.DatasetType != nil {
dt = *r.DatasetType
Expand All @@ -110,6 +113,7 @@ func runSearchByContract(cmd *cobra.Command, _ []string) error {
}
}
output.PrintTable(w, columns, rows)
printColumns(w, names, cols)
fmt.Fprintf(w, "\n%d of %d results\n", len(resp.Results), resp.Total)
return nil
}
Expand Down
18 changes: 18 additions & 0 deletions cmd/dataset/search_by_contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,24 @@ func TestSearchByContractSuccess(t *testing.T) {
assert.Contains(t, out, "2 of 2 results")
}

func TestSearchByContractTextListsSchema(t *testing.T) {
mock := &mockClient{
searchByContractAddressFn: func(req models.SearchDatasetsByContractAddressRequest) (*models.SearchDatasetsResponse, error) {
return &models.SearchDatasetsResponse{Total: 2, Results: []models.SearchDatasetResult{
{FullName: "pool.evt_Swap", Category: "decoded",
Schema: []byte(`{"fields":[{"name":"amount0","type":"int256"},{"name":"evt_block_time","type":"timestamp"}]}`)},
{FullName: "pool.call_swap", Category: "decoded"},
}}, nil
},
}
root, buf := newTestRoot(mock)
root.SetArgs([]string{"dataset", "search-by-contract", "--contract-address", "0xabc", "--include-schema"})
require.NoError(t, root.Execute())
assert.Contains(t, buf.String(),
"\nCOLUMNS\npool.evt_Swap: amount0 int256, evt_block_time timestamp\n\n2 of 2 results\n")
assert.NotContains(t, buf.String(), "pool.call_swap:")
}

func TestSearchByContractJSON(t *testing.T) {
mock := &mockClient{
searchByContractAddressFn: func(req models.SearchDatasetsByContractAddressRequest) (*models.SearchDatasetsResponse, error) {
Expand Down
33 changes: 27 additions & 6 deletions cmd/dataset/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,18 @@ func runSearch(t *testing.T, fake *dunemcptest.Fake, args ...string) (models.Sea
next := int32(2)
mock := &mockClient{searchDatasetsFn: func(req models.SearchDatasetsRequest) (*models.SearchDatasetsResponse, error) {
got = req
trades := models.SearchDatasetResult{FullName: "dex.trades", Category: "spell", DatasetType: strPtr("spell"),
Blockchains: []string{"ethereum", "arbitrum"}}
private := models.SearchDatasetResult{FullName: "my.private_table", Category: "community",
Visibility: strPtr("private")}
if req.IncludeSchema != nil && *req.IncludeSchema {
trades.Schema = []byte(`{"fields":[{"name":"rest_only","type":"bigint","nullable":true}]}`)
private.Schema = []byte(`{"fields":[{"name":"id","type":"bigint","nullable":false},` +
`{"name":"amount","type":"decimal(38,0)","nullable":true}]}`)
}
return &models.SearchDatasetsResponse{
Total: 144,
Results: []models.SearchDatasetResult{
{FullName: "dex.trades", Category: "spell", DatasetType: strPtr("spell"),
Blockchains: []string{"ethereum", "arbitrum"}},
{FullName: "my.private_table", Category: "community", Visibility: strPtr("private")},
},
Total: 144,
Results: []models.SearchDatasetResult{trades, private},
Pagination: models.SearchDatasetsPagination{Limit: 2, NextOffset: &next, HasMore: true},
}, nil
}}
Expand Down Expand Up @@ -110,6 +115,22 @@ func TestSearchSendsEveryFlagToTheAPI(t *testing.T) {
assert.Contains(t, out, "2 of 144 results")
}

// Text output must carry columns: agents read it to write SQL. The MCP's
// columns win over the API schema; --include-schema fills in the other hits.
func TestSearchTextListsColumns(t *testing.T) {
fake := &dunemcptest.Fake{Results: map[string]string{"searchTables": searchTablesResult}}
_, out, _, err := runSearch(t, fake, "--query", "dex")
require.NoError(t, err)
assert.Contains(t, out, "\nCOLUMNS\ndex.trades: blockchain varchar\n")
assert.NotContains(t, out, "my.private_table:")

_, out, _, err = runSearch(t, fake, "--query", "dex", "--include-schema")
require.NoError(t, err)
assert.Contains(t, out, "\nCOLUMNS\ndex.trades: blockchain varchar\n"+
"my.private_table: id bigint, amount decimal(38,0)\n\n2 of 144 results\n")
assert.NotContains(t, out, "rest_only")
}

func TestSearchJSONIsTheAPIShapePlusColumns(t *testing.T) {
fake := &dunemcptest.Fake{Results: map[string]string{"searchTables": searchTablesResult}}
_, out, _, err := runSearch(t, fake, "--query", "dex", "-o", "json")
Expand Down
2 changes: 1 addition & 1 deletion cmd/dataset/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func runSize(cmd *cobra.Command, args []string) error {
}
w := cmd.OutOrStdout()
if output.FormatFromCmd(cmd) == output.FormatJSON {
return output.PrintRawJSON(w, raw)
return output.PrintRawJSON(cmd, raw)
}
if res.TotalSizeBytes != nil {
fmt.Fprintf(w, "%s: %s\n", res.TableName, output.FormatBytes(*res.TotalSizeBytes))
Expand Down
2 changes: 1 addition & 1 deletion cmd/docs/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func runSearch(cmd *cobra.Command, _ []string) error {
w := cmd.OutOrStdout()
switch output.FormatFromCmd(cmd) {
case output.FormatJSON:
return output.PrintJSON(w, searchResponse{
return output.PrintJSON(cmd, searchResponse{
Query: query,
Results: results,
})
Expand Down
2 changes: 1 addition & 1 deletion cmd/execution/results.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func handleResultsResponse(cmd *cobra.Command, executionID string, resp *models.
w := cmd.OutOrStdout()
switch output.FormatFromCmd(cmd) {
case output.FormatJSON:
return output.PrintJSON(w, resp)
return output.PrintJSON(cmd, resp)
default:
fmt.Fprintf(w, "Execution ID: %s\n", executionID)
fmt.Fprintf(w, "State: %s\n", resp.State)
Expand Down
2 changes: 1 addition & 1 deletion cmd/folder/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func runDelete(cmd *cobra.Command, posArgs []string) error {
}
w := cmd.OutOrStdout()
if output.FormatFromCmd(cmd) == output.FormatJSON {
return output.PrintJSON(w, res)
return output.PrintJSON(cmd, res)
}
fmt.Fprintf(w, "Deleted folder %s: %d folders deleted, %d queries and %d dashboards archived\n",
res.Path, res.DeletedFolderCount, res.ArchivedQueryCount, res.ArchivedDashboardCount)
Expand Down
2 changes: 1 addition & 1 deletion cmd/folder/folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewFolderCmd() *cobra.Command {
func printFolder(cmd *cobra.Command, f *models.Folder, verb string) error {
w := cmd.OutOrStdout()
if output.FormatFromCmd(cmd) == output.FormatJSON {
return output.PrintJSON(w, f)
return output.PrintJSON(cmd, f)
}
fmt.Fprintf(w, "%s folder %s (%s)\n", verb, f.Path, f.FolderID)
return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/folder/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func runGet(cmd *cobra.Command, posArgs []string) error {
}
w := cmd.OutOrStdout()
if output.FormatFromCmd(cmd) == output.FormatJSON {
return output.PrintJSON(w, res)
return output.PrintJSON(cmd, res)
}

f := res.Folder
Expand Down
2 changes: 1 addition & 1 deletion cmd/folder/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func runList(cmd *cobra.Command, _ []string) error {
}
w := cmd.OutOrStdout()
if output.FormatFromCmd(cmd) == output.FormatJSON {
return output.PrintJSON(w, res)
return output.PrintJSON(cmd, res)
}
if len(res.Folders) == 0 {
fmt.Fprintln(w, "No folders found.")
Expand Down
2 changes: 1 addition & 1 deletion cmd/folder/move_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func runMoveContent(cmd *cobra.Command, _ []string) error {
}
w := cmd.OutOrStdout()
if output.FormatFromCmd(cmd) == output.FormatJSON {
return output.PrintJSON(w, res)
return output.PrintJSON(cmd, res)
}
where := "out of folders"
if res.TargetFolder != nil {
Expand Down
2 changes: 1 addition & 1 deletion cmd/matview/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func runCreate(cmd *cobra.Command, _ []string) error {
w := cmd.OutOrStdout()
switch output.FormatFromCmd(cmd) {
case output.FormatJSON:
return output.PrintJSON(w, resp)
return output.PrintJSON(cmd, resp)
default:
fmt.Fprintf(w, "Created materialized view %s\n", resp.SQLID)
fmt.Fprintf(w, "Execution ID: %s\n", resp.ExecutionID)
Expand Down
2 changes: 1 addition & 1 deletion cmd/matview/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func runDelete(cmd *cobra.Command, args []string) error {
w := cmd.OutOrStdout()
switch output.FormatFromCmd(cmd) {
case output.FormatJSON:
return output.PrintJSON(w, resp)
return output.PrintJSON(cmd, resp)
default:
fmt.Fprintf(w, "Deleted materialized view %s\n", args[0])
return nil
Expand Down
2 changes: 1 addition & 1 deletion cmd/matview/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func runGet(cmd *cobra.Command, args []string) error {
w := cmd.OutOrStdout()
switch output.FormatFromCmd(cmd) {
case output.FormatJSON:
return output.PrintJSON(w, resp)
return output.PrintJSON(cmd, resp)
default:
fmt.Fprintf(w, "Name: %s\n", resp.SQLID)
fmt.Fprintf(w, "Query ID: %d\n", resp.QueryID)
Expand Down
2 changes: 1 addition & 1 deletion cmd/matview/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func runList(cmd *cobra.Command, _ []string) error {
w := cmd.OutOrStdout()
switch output.FormatFromCmd(cmd) {
case output.FormatJSON:
return output.PrintJSON(w, result)
return output.PrintJSON(cmd, result)
default:
if len(result.MaterializedViews) == 0 {
fmt.Fprintln(w, "No materialized views found.")
Expand Down
2 changes: 1 addition & 1 deletion cmd/matview/refresh.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func runRefresh(cmd *cobra.Command, args []string) error {
w := cmd.OutOrStdout()
switch output.FormatFromCmd(cmd) {
case output.FormatJSON:
return output.PrintJSON(w, resp)
return output.PrintJSON(cmd, resp)
default:
fmt.Fprintf(w, "Refreshing materialized view %s\n", resp.SQLID)
fmt.Fprintf(w, "Execution ID: %s\n", resp.ExecutionID)
Expand Down
2 changes: 1 addition & 1 deletion cmd/matview/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func runUpdate(cmd *cobra.Command, args []string) error {
w := cmd.OutOrStdout()
switch output.FormatFromCmd(cmd) {
case output.FormatJSON:
return output.PrintJSON(w, resp)
return output.PrintJSON(cmd, resp)
default:
fmt.Fprintf(w, "Updated materialized view %s\n", resp.SQLID)
fmt.Fprintf(w, "Execution ID: %s\n", resp.ExecutionID)
Expand Down
2 changes: 1 addition & 1 deletion cmd/query/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func runArchive(cmd *cobra.Command, args []string) error {
w := cmd.OutOrStdout()
switch output.FormatFromCmd(cmd) {
case output.FormatJSON:
return output.PrintJSON(w, resp)
return output.PrintJSON(cmd, resp)
default:
fmt.Fprintf(w, "Archived query %d\n", resp.QueryID)
return nil
Expand Down
Loading
Loading