|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + "text/tabwriter" |
| 8 | + |
| 9 | + "github.com/pepabo/xpoint-cli/internal/xpoint" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + queryListOutput string |
| 15 | + queryListJQ string |
| 16 | + |
| 17 | + queryExecNoRun bool |
| 18 | + queryExecRows int |
| 19 | + queryExecOffset int |
| 20 | + queryExecJQ string |
| 21 | +) |
| 22 | + |
| 23 | +var queryCmd = &cobra.Command{ |
| 24 | + Use: "query", |
| 25 | + Short: "Manage X-point queries", |
| 26 | +} |
| 27 | + |
| 28 | +var queryListCmd = &cobra.Command{ |
| 29 | + Use: "list", |
| 30 | + Short: "List available queries", |
| 31 | + Long: "List available queries via GET /api/v1/query/.", |
| 32 | + RunE: runQueryList, |
| 33 | +} |
| 34 | + |
| 35 | +var queryExecCmd = &cobra.Command{ |
| 36 | + Use: "exec <query_code>", |
| 37 | + Short: "Execute a query and show its result", |
| 38 | + Long: `Fetch a query and its execution result via GET /api/v1/query/{query_code}. |
| 39 | +
|
| 40 | +By default the query is executed (exec_flg=true) and the response contains |
| 41 | +both the definition and exec_result. Pass --no-run to fetch the definition |
| 42 | +only. --rows (default 500) and --offset control pagination for list queries.`, |
| 43 | + Args: cobra.ExactArgs(1), |
| 44 | + RunE: runQueryExec, |
| 45 | +} |
| 46 | + |
| 47 | +func init() { |
| 48 | + rootCmd.AddCommand(queryCmd) |
| 49 | + queryCmd.AddCommand(queryListCmd) |
| 50 | + queryCmd.AddCommand(queryExecCmd) |
| 51 | + |
| 52 | + lf := queryListCmd.Flags() |
| 53 | + lf.StringVarP(&queryListOutput, "output", "o", "", "output format: table|json (default: table on TTY, json otherwise)") |
| 54 | + lf.StringVar(&queryListJQ, "jq", "", "apply a gojq filter to the JSON response (forces JSON output)") |
| 55 | + |
| 56 | + ef := queryExecCmd.Flags() |
| 57 | + ef.BoolVar(&queryExecNoRun, "no-run", false, "do not execute the query; return the definition only (exec_flg=false)") |
| 58 | + ef.IntVar(&queryExecRows, "rows", 0, "max rows returned by list queries (0 = omit; server default 500; range 1-10000)") |
| 59 | + ef.IntVar(&queryExecOffset, "offset", 0, "offset for list queries (0 = omit)") |
| 60 | + ef.StringVar(&queryExecJQ, "jq", "", "apply a gojq filter to the JSON response") |
| 61 | +} |
| 62 | + |
| 63 | +func runQueryList(cmd *cobra.Command, args []string) error { |
| 64 | + client, err := newClientFromFlags(cmd.Context()) |
| 65 | + if err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + res, err := client.ListAvailableQueries(cmd.Context()) |
| 69 | + if err != nil { |
| 70 | + return err |
| 71 | + } |
| 72 | + return render(res, resolveOutputFormat(queryListOutput), queryListJQ, func() error { |
| 73 | + w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) |
| 74 | + defer w.Flush() |
| 75 | + fmt.Fprintln(w, "GROUP_ID\tGROUP_NAME\tQUERY_ID\tQUERY_CODE\tQUERY_NAME\tQUERY_TYPE\tFORM") |
| 76 | + for _, g := range res.QueryGroups { |
| 77 | + if len(g.Queries) == 0 { |
| 78 | + fmt.Fprintf(w, "%d\t%s\t-\t-\t-\t-\t-\n", g.QueryGroupID, g.QueryGroupName) |
| 79 | + continue |
| 80 | + } |
| 81 | + for _, q := range g.Queries { |
| 82 | + fmt.Fprintf(w, "%d\t%s\t%d\t%s\t%s\t%s\t%s\n", |
| 83 | + g.QueryGroupID, g.QueryGroupName, q.QueryID, q.QueryCode, q.QueryName, q.QueryType, formatQueryForm(q), |
| 84 | + ) |
| 85 | + } |
| 86 | + } |
| 87 | + return nil |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +// formatQueryForm renders the form column for `query list`: single form shows |
| 92 | +// "<name> (fid)"; multi-form shows form count and a "+" suffix. |
| 93 | +func formatQueryForm(q xpoint.Query) string { |
| 94 | + if q.FormCount > 1 { |
| 95 | + return fmt.Sprintf("%d forms", q.FormCount) |
| 96 | + } |
| 97 | + if q.FormName == "" { |
| 98 | + return "-" |
| 99 | + } |
| 100 | + return fmt.Sprintf("%s (%d)", q.FormName, q.FID) |
| 101 | +} |
| 102 | + |
| 103 | +func runQueryExec(cmd *cobra.Command, args []string) error { |
| 104 | + queryCode := strings.TrimSpace(args[0]) |
| 105 | + if queryCode == "" { |
| 106 | + return fmt.Errorf("query_code is required") |
| 107 | + } |
| 108 | + client, err := newClientFromFlags(cmd.Context()) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + p := xpoint.GetQueryParams{ExecFlag: !queryExecNoRun} |
| 113 | + if cmd.Flags().Changed("rows") { |
| 114 | + v := queryExecRows |
| 115 | + p.Rows = &v |
| 116 | + } |
| 117 | + if cmd.Flags().Changed("offset") { |
| 118 | + v := queryExecOffset |
| 119 | + p.Offset = &v |
| 120 | + } |
| 121 | + |
| 122 | + raw, err := client.GetQuery(cmd.Context(), queryCode, p) |
| 123 | + if err != nil { |
| 124 | + return err |
| 125 | + } |
| 126 | + if queryExecJQ != "" { |
| 127 | + return runJQ(raw, queryExecJQ) |
| 128 | + } |
| 129 | + return writeJSON(os.Stdout, raw) |
| 130 | +} |
0 commit comments