diff --git a/cmd/query/get.go b/cmd/query/get.go index 081f5bb..5091a9d 100644 --- a/cmd/query/get.go +++ b/cmd/query/get.go @@ -15,7 +15,8 @@ func newGetCmd() *cobra.Command { Use: "get ", Short: "Fetch a saved Dune query by ID, including SQL and metadata", Long: "Retrieve SQL, metadata, owner, privacy flags, tags, and execution state for\n" + - "an existing Dune query. Only queries visible to the authenticated user are returned.\n\n" + + "an existing Dune query. Only queries visible to the authenticated user are returned.\n" + + "The folder is shown only for queries that the API key's user or team owns.\n\n" + "The latest execution ID reads its results without running the query again:\n" + " dune execution results ", Args: cobra.ExactArgs(1), @@ -42,8 +43,13 @@ func runGet(cmd *cobra.Command, args []string) error { // The REST API does not report executions; the MCP tool getDuneQuery does. The // latest execution is an extra, so the command still prints the query without it. var latest struct { - ID *string `json:"latest_execution_id"` - State *string `json:"latest_execution_state"` + ID *string `json:"latest_execution_id"` + State *string `json:"latest_execution_state"` + Folder *struct { + FolderID string `json:"folder_id"` + Path string `json:"path"` + } `json:"folder"` + FolderAccess string `json:"folder_access"` } latestKnown := true if _, err := cmdutil.CallTool(cmd, "getDuneQuery", map[string]any{"query_id": queryID}, &latest); err != nil { @@ -62,7 +68,9 @@ func runGet(cmd *cobra.Command, args []string) error { *models.GetQueryResponse LatestExecutionID *string `json:"latest_execution_id"` LatestExecutionState *string `json:"latest_execution_state"` - }{resp, latest.ID, latest.State}) + Folder any `json:"folder"` + FolderAccess string `json:"folder_access,omitempty"` + }{resp, latest.ID, latest.State, latest.Folder, latest.FolderAccess}) default: fmt.Fprintf(w, "ID: %d\n", resp.QueryID) fmt.Fprintf(w, "Name: %s\n", resp.Name) @@ -77,6 +85,9 @@ func runGet(cmd *cobra.Command, args []string) error { if len(resp.Tags) > 0 { fmt.Fprintf(w, "Tags: %s\n", strings.Join(resp.Tags, ", ")) } + if latest.Folder != nil { + fmt.Fprintf(w, "Folder: %s (%s)\n", latest.Folder.Path, latest.Folder.FolderID) + } switch { case !latestKnown: fmt.Fprintln(w, "Last run: unknown") diff --git a/cmd/query/get_test.go b/cmd/query/get_test.go index 2925066..5762495 100644 --- a/cmd/query/get_test.go +++ b/cmd/query/get_test.go @@ -129,13 +129,15 @@ func TestGetEmptyDescriptionAndTags(t *testing.T) { func TestGetShowsTheLatestExecution(t *testing.T) { mock := &mockClient{getQueryFn: func(int) (*models.GetQueryResponse, error) { return testQueryResponse, nil }} fake := &dunemcptest.Fake{Results: map[string]string{"getDuneQuery": `{"query_id":4125432, - "latest_execution_id":"01EXEC","latest_execution_state":"QUERY_STATE_COMPLETED"}`}} + "latest_execution_id":"01EXEC","latest_execution_state":"QUERY_STATE_COMPLETED", + "folder":{"folder_id":"fld_1","path":"/Research/RWA"},"folder_access":"available"}`}} root, buf := newTestRootWithMCP(mock, fake) root.SetArgs([]string{"query", "get", "4125432"}) require.NoError(t, root.Execute()) assert.JSONEq(t, `{"query_id":4125432}`, fake.Last().Args) assert.Contains(t, buf.String(), "Last run: 01EXEC (COMPLETED)") + assert.Contains(t, buf.String(), "Folder: /Research/RWA (fld_1)") // JSON keeps every REST field and adds the latest execution. root, buf = newTestRootWithMCP(mock, fake) @@ -146,6 +148,8 @@ func TestGetShowsTheLatestExecution(t *testing.T) { assert.Equal(t, "SELECT 1", got["query_sql"]) assert.Equal(t, "01EXEC", got["latest_execution_id"]) assert.Equal(t, "QUERY_STATE_COMPLETED", got["latest_execution_state"]) + assert.Equal(t, map[string]any{"folder_id": "fld_1", "path": "/Research/RWA"}, got["folder"]) + assert.Equal(t, "available", got["folder_access"]) } func TestGetNeverRun(t *testing.T) { diff --git a/cmd/query/mcp_test.go b/cmd/query/mcp_test.go index 082296e..c014b46 100644 --- a/cmd/query/mcp_test.go +++ b/cmd/query/mcp_test.go @@ -138,6 +138,11 @@ func TestSearchOwnedOnly(t *testing.T) { assert.JSONEq(t, `{"search_string":"dex","scope":"owned_only"}`, fake.Last().Args) assert.Regexp(t, `4700\s+DEX Volume\s+remix`, buf.String()) + + root, _ = newMCPRoot(fake) + root.SetArgs([]string{"query", "search", "--query", "dex", "--folder-id", "fld_1"}) + require.NoError(t, root.Execute()) + assert.JSONEq(t, `{"search_string":"dex","folder_id":"fld_1"}`, fake.Last().Args) } func TestCreateAndRunJSONIsTheToolResult(t *testing.T) { diff --git a/cmd/query/search.go b/cmd/query/search.go index b6da8cb..a7bbf51 100644 --- a/cmd/query/search.go +++ b/cmd/query/search.go @@ -15,15 +15,18 @@ func newSearchCmd() *cobra.Command { Long: "Find saved queries whose name contains the given text (not case-sensitive), to get\n" + "a query ID to run, edit, or chart. Results include public queries and the private\n" + "queries you or your team can access; --owned-only keeps only the ones the API key's\n" + - "user or team owns.\n\n" + + "user or team owns, and --folder-id keeps only the queries in a folder or its\n" + + "subfolders.\n\n" + "Examples:\n" + " dune query search --query \"dex volume\"\n" + - " dune query search --query stablecoin --owned-only --limit 50 -o json", + " dune query search --query stablecoin --owned-only --limit 50 -o json\n" + + " dune query search --query volume --folder-id ", Args: cobra.NoArgs, RunE: runSearch, } cmd.Flags().String("query", "", "text to find in the query name (required)") cmd.Flags().Bool("owned-only", false, "only return queries owned by the API key's user or team") + cmd.Flags().String("folder-id", "", "only return queries in this folder or its subfolders (see 'dune folder list')") cmd.Flags().Int("limit", 20, "maximum number of queries to return (1-50)") _ = cmd.MarkFlagRequired("query") output.AddFormatFlag(cmd, "text") @@ -31,7 +34,7 @@ func newSearchCmd() *cobra.Command { } func runSearch(cmd *cobra.Command, _ []string) error { - args := cmdutil.ToolArgs(cmd, map[string]string{"query": "search_string", "limit": "limit"}) + args := cmdutil.ToolArgs(cmd, map[string]string{"query": "search_string", "folder-id": "folder_id", "limit": "limit"}) if owned, _ := cmd.Flags().GetBool("owned-only"); owned { args["scope"] = "owned_only" }