(ctx context.Context, f *cmdutil.Factory, runF func(*APIOptions) error)
| 62 | } |
| 63 | |
| 64 | func NewCmdApiWithContext(ctx context.Context, f *cmdutil.Factory, runF func(*APIOptions) error) *cobra.Command { |
| 65 | opts := &APIOptions{Factory: f} |
| 66 | var asStr string |
| 67 | |
| 68 | cmd := &cobra.Command{ |
| 69 | Use: "api <method> <path>", |
| 70 | Short: "Raw HTTP escape hatch — call any endpoint by path (fallback when no typed command exists)", |
| 71 | Long: `Raw HTTP escape hatch: send any Lark API request by HTTP method + path. |
| 72 | |
| 73 | Prefer the typed domain command when one exists — it validates parameters, |
| 74 | shows the Risk level, gates destructive calls behind --yes, and carries usage |
| 75 | guidance that this raw command does not. If a domain command covers your task |
| 76 | (browse with ` + "`lark-cli <domain> --help`" + `), use it instead of this. |
| 77 | |
| 78 | Reach for ` + "`api`" + ` only for endpoints that have no typed command yet (e.g. |
| 79 | newer/preview APIs), where you already have the HTTP path from the Lark docs. |
| 80 | |
| 81 | Examples: |
| 82 | lark-cli api GET /open-apis/calendar/v4/calendars |
| 83 | lark-cli api POST /open-apis/im/v1/messages --params '{"receive_id_type":"open_id"}' --data @body.json`, |
| 84 | Args: cobra.ExactArgs(2), |
| 85 | RunE: func(cmd *cobra.Command, args []string) error { |
| 86 | opts.Method = strings.ToUpper(args[0]) |
| 87 | opts.Path = args[1] |
| 88 | opts.Cmd = cmd |
| 89 | opts.Ctx = cmd.Context() |
| 90 | opts.As = core.Identity(asStr) |
| 91 | if runF != nil { |
| 92 | return runF(opts) |
| 93 | } |
| 94 | return apiRun(opts) |
| 95 | }, |
| 96 | } |
| 97 | |
| 98 | cmd.Flags().StringVar(&opts.Params, "params", "", "query parameters JSON (supports - for stdin, @file for file input)") |
| 99 | cmd.Flags().StringVar(&opts.Data, "data", "", "request body JSON (supports - for stdin, @file for file input)") |
| 100 | cmdutil.AddAPIIdentityFlag(ctx, cmd, f, &asStr) |
| 101 | cmd.Flags().StringVarP(&opts.Output, "output", "o", "", "output file path for binary responses") |
| 102 | cmd.Flags().BoolVar(&opts.PageAll, "page-all", false, "automatically paginate through all pages") |
| 103 | cmd.Flags().IntVar(&opts.PageSize, "page-size", 0, "page size (0 = use API default)") |
| 104 | cmd.Flags().IntVar(&opts.PageLimit, "page-limit", 10, "max pages to fetch with --page-all (0 = unlimited)") |
| 105 | cmd.Flags().IntVar(&opts.PageDelay, "page-delay", 200, "delay in ms between pages") |
| 106 | cmd.Flags().StringVar(&opts.Format, "format", "json", "output format: json|ndjson|table|csv") |
| 107 | cmd.Flags().Bool("json", false, "shorthand for --format json") |
| 108 | cmd.Flags().StringVarP(&opts.JqExpr, "jq", "q", "", "jq expression to filter JSON output") |
| 109 | cmd.Flags().BoolVar(&opts.DryRun, "dry-run", false, "print request without executing") |
| 110 | cmd.Flags().StringVar(&opts.File, "file", "", "file to upload as multipart/form-data ([field=]path, supports - for stdin)") |
| 111 | |
| 112 | cmd.ValidArgsFunction = func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 113 | if len(args) == 0 { |
| 114 | return []string{"GET", "POST", "PUT", "PATCH", "DELETE"}, cobra.ShellCompDirectiveNoFileComp |
| 115 | } |
| 116 | return nil, cobra.ShellCompDirectiveNoFileComp |
| 117 | } |
| 118 | cmdutil.RegisterFlagCompletion(cmd, "format", func(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { |
| 119 | return []string{"json", "ndjson", "table", "csv"}, cobra.ShellCompDirectiveNoFileComp |
| 120 | }) |
| 121 | cmdutil.SetRisk(cmd, "write") |
no test coverage detected