|
| 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 | + systemWebhooklogListFrom string |
| 15 | + systemWebhooklogListTo string |
| 16 | + systemWebhooklogListDocID int |
| 17 | + systemWebhooklogListFormCode string |
| 18 | + systemWebhooklogListRouteCode string |
| 19 | + systemWebhooklogListStatus string |
| 20 | + systemWebhooklogListURL string |
| 21 | + systemWebhooklogListLimit int |
| 22 | + systemWebhooklogListOffset int |
| 23 | + systemWebhooklogListOutput string |
| 24 | + systemWebhooklogListJQ string |
| 25 | + |
| 26 | + systemWebhooklogShowJQ string |
| 27 | +) |
| 28 | + |
| 29 | +var systemWebhooklogCmd = &cobra.Command{ |
| 30 | + Use: "webhooklog", |
| 31 | + Short: "Inspect X-point webhook delivery logs (admin)", |
| 32 | +} |
| 33 | + |
| 34 | +var systemWebhooklogListCmd = &cobra.Command{ |
| 35 | + Use: "list", |
| 36 | + Short: "List webhook delivery logs", |
| 37 | + Long: `List webhook delivery logs via GET /api/v1/system/webhooklog. |
| 38 | +
|
| 39 | +Filters are all optional. --status accepts all | success | failed. |
| 40 | +Requires an administrator account.`, |
| 41 | + RunE: runSystemWebhooklogList, |
| 42 | +} |
| 43 | + |
| 44 | +var systemWebhooklogShowCmd = &cobra.Command{ |
| 45 | + Use: "show <uuid>", |
| 46 | + Short: "Show webhook delivery log detail (request/response)", |
| 47 | + Long: `Fetch detailed request/response data for a single webhook log entry via |
| 48 | +GET /api/v1/system/webhooklog/{uuid}. Requires an administrator account. |
| 49 | +
|
| 50 | +The response is emitted as JSON because the request/response body shape |
| 51 | +depends on the remote endpoint. Use --jq to filter the output.`, |
| 52 | + Args: cobra.ExactArgs(1), |
| 53 | + RunE: runSystemWebhooklogShow, |
| 54 | +} |
| 55 | + |
| 56 | +func init() { |
| 57 | + systemCmd.AddCommand(systemWebhooklogCmd) |
| 58 | + systemWebhooklogCmd.AddCommand(systemWebhooklogListCmd) |
| 59 | + systemWebhooklogCmd.AddCommand(systemWebhooklogShowCmd) |
| 60 | + |
| 61 | + lf := systemWebhooklogListCmd.Flags() |
| 62 | + lf.StringVar(&systemWebhooklogListFrom, "from", "", "send date from (e.g. 2022/10/01)") |
| 63 | + lf.StringVar(&systemWebhooklogListTo, "to", "", "send date to (e.g. 2022/12/01)") |
| 64 | + lf.IntVar(&systemWebhooklogListDocID, "docid", 0, "document id (0 = omit)") |
| 65 | + lf.StringVar(&systemWebhooklogListFormCode, "form-code", "", "form code") |
| 66 | + lf.StringVar(&systemWebhooklogListRouteCode, "route-code", "", "approval route code") |
| 67 | + lf.StringVar(&systemWebhooklogListStatus, "status", "", "status: all | success | failed") |
| 68 | + lf.StringVar(&systemWebhooklogListURL, "url", "", "destination URL (partial match)") |
| 69 | + lf.IntVar(&systemWebhooklogListLimit, "limit", 0, "fetch count (0 = omit; server default 50)") |
| 70 | + lf.IntVar(&systemWebhooklogListOffset, "offset", 0, "offset (0 = omit; server default 0)") |
| 71 | + lf.StringVarP(&systemWebhooklogListOutput, "output", "o", "", "output format: table|json (default: table on TTY, json otherwise)") |
| 72 | + lf.StringVar(&systemWebhooklogListJQ, "jq", "", "apply a gojq filter to the JSON response (forces JSON output)") |
| 73 | + |
| 74 | + sf := systemWebhooklogShowCmd.Flags() |
| 75 | + sf.StringVar(&systemWebhooklogShowJQ, "jq", "", "apply a gojq filter to the JSON response") |
| 76 | +} |
| 77 | + |
| 78 | +func runSystemWebhooklogList(cmd *cobra.Command, _ []string) error { |
| 79 | + p := xpoint.WebhooklogListParams{ |
| 80 | + From: systemWebhooklogListFrom, |
| 81 | + To: systemWebhooklogListTo, |
| 82 | + FormCode: systemWebhooklogListFormCode, |
| 83 | + RouteCode: systemWebhooklogListRouteCode, |
| 84 | + Status: systemWebhooklogListStatus, |
| 85 | + URL: systemWebhooklogListURL, |
| 86 | + } |
| 87 | + if cmd.Flags().Changed("docid") { |
| 88 | + v := systemWebhooklogListDocID |
| 89 | + p.DocID = &v |
| 90 | + } |
| 91 | + if cmd.Flags().Changed("limit") { |
| 92 | + v := systemWebhooklogListLimit |
| 93 | + p.Limit = &v |
| 94 | + } |
| 95 | + if cmd.Flags().Changed("offset") { |
| 96 | + v := systemWebhooklogListOffset |
| 97 | + p.Offset = &v |
| 98 | + } |
| 99 | + if p.Status != "" { |
| 100 | + switch strings.ToLower(p.Status) { |
| 101 | + case "all", "success", "failed": |
| 102 | + default: |
| 103 | + return fmt.Errorf("--status must be all|success|failed, got %q", p.Status) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + client, err := newClientFromFlags(cmd.Context()) |
| 108 | + if err != nil { |
| 109 | + return err |
| 110 | + } |
| 111 | + res, err := client.ListWebhooklog(cmd.Context(), p) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + |
| 116 | + return render(res, resolveOutputFormat(systemWebhooklogListOutput), systemWebhooklogListJQ, func() error { |
| 117 | + w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) |
| 118 | + defer w.Flush() |
| 119 | + fmt.Fprintln(w, "SEND_DATE\tSTATUS\tDOCID\tFORM_CODE\tROUTE_CODE\tTITLE\tURL\tUUID") |
| 120 | + for _, e := range res.Data { |
| 121 | + fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", |
| 122 | + e.SendDate, e.StatusCode, e.DocID, e.FormCode, e.RouteCode, e.Title1, e.URL, e.UUID, |
| 123 | + ) |
| 124 | + } |
| 125 | + return nil |
| 126 | + }) |
| 127 | +} |
| 128 | + |
| 129 | +func runSystemWebhooklogShow(cmd *cobra.Command, args []string) error { |
| 130 | + uuid := strings.TrimSpace(args[0]) |
| 131 | + if uuid == "" { |
| 132 | + return fmt.Errorf("uuid is required") |
| 133 | + } |
| 134 | + client, err := newClientFromFlags(cmd.Context()) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + out, err := client.GetWebhooklog(cmd.Context(), uuid) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + if systemWebhooklogShowJQ != "" { |
| 143 | + return runJQ(out, systemWebhooklogShowJQ) |
| 144 | + } |
| 145 | + return writeJSON(os.Stdout, out) |
| 146 | +} |
0 commit comments