(ch *cmdutil.Helper)
| 18 | ` |
| 19 | |
| 20 | func QueryCmd(ch *cmdutil.Helper) *cobra.Command { |
| 21 | var sql, connector, resolver, project, path, branch string |
| 22 | var properties, args map[string]string |
| 23 | var limit int |
| 24 | var local bool |
| 25 | |
| 26 | queryCmd := &cobra.Command{ |
| 27 | Use: "query [<project>]", |
| 28 | Short: "Query data in a project", |
| 29 | Long: long, |
| 30 | Example: ` # SQL query against a Rill Cloud project |
| 31 | rill query my-project --sql "SELECT * FROM my-table" |
| 32 | |
| 33 | # SQL query against a local Rill project running with 'rill start' |
| 34 | rill query --local --sql "SELECT * FROM my-table"`, |
| 35 | RunE: func(cmd *cobra.Command, cmdArgs []string) error { |
| 36 | // Validate the inputs |
| 37 | if resolver == "" && sql == "" { |
| 38 | return fmt.Errorf("must provide --sql or --resolver") |
| 39 | } |
| 40 | if resolver != "" && (sql != "" || connector != "") { |
| 41 | return fmt.Errorf("cannot combine --resolver with --sql or --connector") |
| 42 | } |
| 43 | if sql != "" && len(properties) > 0 { |
| 44 | return fmt.Errorf("cannot combine --sql with --properties") |
| 45 | } |
| 46 | |
| 47 | // Rewrite --sql to resolver |
| 48 | if sql != "" { |
| 49 | resolver = "sql" |
| 50 | properties = map[string]string{"sql": sql} |
| 51 | if connector != "" { |
| 52 | properties["connector"] = connector |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | // Determine project name |
| 57 | if len(cmdArgs) > 0 { |
| 58 | project = cmdArgs[0] |
| 59 | } |
| 60 | if !local && project == "" { |
| 61 | if !ch.Interactive { |
| 62 | return fmt.Errorf("set --project to target a Rill Cloud project, or use --local to target a locally running Rill project") |
| 63 | } |
| 64 | // Check if a local Rill project is running; if so, target it automatically. |
| 65 | if cmdutil.IsLocalRillRunning(cmd.Context()) { |
| 66 | local = true |
| 67 | } else { |
| 68 | var err error |
| 69 | project, err = ch.InferProjectName(cmd.Context(), path, "set --project to target a Rill Cloud project, or use --local to target a locally running Rill project") |
| 70 | if err != nil { |
| 71 | return err |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // If targeting a local runtime, verify that rill start is running. |
| 77 | if local && !cmdutil.IsLocalRillRunning(cmd.Context()) { |
no test coverage detected