diff --git a/.github/actions/setup-go/action.yml b/.github/actions/setup-go/action.yml new file mode 100644 index 0000000..0b2f3fa --- /dev/null +++ b/.github/actions/setup-go/action.yml @@ -0,0 +1,29 @@ +name: Setup Go +description: 'Sets up Go environment with private modules' +inputs: + stainless-api-key: + required: false + description: the value of the STAINLESS_API_KEY secret +runs: + using: composite + steps: + - uses: stainless-api/retrieve-github-access-token@1f03f929b746c5b03dcdafa2bebbb18ca5672e1a # v1.0.0 + if: github.repository == 'stainless-sdks/context.dev-cli' + id: get_token + with: + repo: stainless-sdks/context.dev-go + stainless-api-key: ${{ inputs.stainless-api-key }} + + - name: Configure Git for access to the Go SDK's staging repo + if: github.repository == 'stainless-sdks/context.dev-cli' + shell: bash + run: git config --global url."https://x-access-token:${{ steps.get_token.outputs.github_access_token }}@github.com/stainless-sdks/context.dev-go".insteadOf "https://github.com/stainless-sdks/context.dev-go" + + - name: Setup go + uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5.6.0 + with: + go-version-file: ./go.mod + + - name: Bootstrap + shell: bash + run: ./scripts/bootstrap diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..556a5f1 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,116 @@ +name: CI +on: + push: + branches: + - '**' + - '!integrated/**' + - '!stl-preview-head/**' + - '!stl-preview-base/**' + - '!generated' + - '!codegen/**' + - 'codegen/stl/**' + pull_request: + branches-ignore: + - 'stl-preview-head/**' + - 'stl-preview-base/**' + +env: + GOPRIVATE: github.com/context-dot-dev/context-go-sdk/v2,github.com/stainless-sdks/context.dev-go/v2 + +jobs: + lint: + timeout-minutes: 10 + name: lint + runs-on: ${{ startsWith(github.repository, 'stainless-sdks/') && 'depot-ubuntu-24.04' || 'ubuntu-latest' }} + if: (github.event_name == 'push' || github.event.pull_request.head.repo.fork) && (github.event_name != 'push' || github.event.head_commit.message != 'codegen metadata') + + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + + - uses: ./.github/actions/setup-go + with: + stainless-api-key: ${{ secrets.STAINLESS_API_KEY }} + + - name: Link staging branch + if: github.repository == 'stainless-sdks/context.dev-cli' + run: | + ./scripts/link 'github.com/stainless-sdks/context.dev-go/v2@${{ github.ref_name }}' || true + + - name: Bootstrap + run: ./scripts/bootstrap + + - name: Run lints + run: ./scripts/lint + + build: + timeout-minutes: 10 + name: build + permissions: + contents: read + id-token: write + runs-on: ${{ startsWith(github.repository, 'stainless-sdks/') && 'depot-ubuntu-24.04' || 'ubuntu-latest' }} + if: (github.event_name == 'push' || github.event.pull_request.head.repo.fork) && (github.event_name != 'push' || github.event.head_commit.message != 'codegen metadata') + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + + - uses: ./.github/actions/setup-go + with: + stainless-api-key: ${{ secrets.STAINLESS_API_KEY }} + + - name: Link staging branch + if: github.repository == 'stainless-sdks/context.dev-cli' + run: | + ./scripts/link 'github.com/stainless-sdks/context.dev-go/v2@${{ github.ref_name }}' || true + + - name: Bootstrap + run: ./scripts/bootstrap + + - name: Run goreleaser + uses: goreleaser/goreleaser-action@9ed2f89a662bf1735a48bc8557fd212fa902bebf # v6.1.0 + with: + version: latest + args: release --snapshot --clean --skip=publish + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + - name: Get GitHub OIDC Token + if: |- + github.repository == 'stainless-sdks/context.dev-cli' && + !startsWith(github.ref, 'refs/heads/stl/') + id: github-oidc + uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 + with: + script: core.setOutput('github_token', await core.getIDToken()); + + - name: Upload tarball + if: |- + github.repository == 'stainless-sdks/context.dev-cli' && + !startsWith(github.ref, 'refs/heads/stl/') + env: + URL: https://pkg.stainless.com/s + AUTH: ${{ steps.github-oidc.outputs.github_token }} + SHA: ${{ github.sha }} + run: ./scripts/utils/upload-artifact.sh + + test: + timeout-minutes: 10 + name: test + runs-on: ${{ startsWith(github.repository, 'stainless-sdks/') && 'depot-ubuntu-24.04' || 'ubuntu-latest' }} + if: github.event_name == 'push' || github.event.pull_request.head.repo.fork + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + + - uses: ./.github/actions/setup-go + with: + stainless-api-key: ${{ secrets.STAINLESS_API_KEY }} + + - name: Link staging branch + if: github.repository == 'stainless-sdks/context.dev-cli' + run: | + ./scripts/link 'github.com/stainless-sdks/context.dev-go/v2@${{ github.ref_name }}' || true + + - name: Bootstrap + run: ./scripts/bootstrap + + - name: Run tests + run: ./scripts/test diff --git a/.github/workflows/publish-release.yml b/.github/workflows/publish-release.yml new file mode 100644 index 0000000..6b026e0 --- /dev/null +++ b/.github/workflows/publish-release.yml @@ -0,0 +1,32 @@ +--- +name: Publish Release +permissions: + contents: write + +concurrency: + group: publish + +on: + push: + tags: + - "v*" + workflow_dispatch: {} +jobs: + goreleaser: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + with: + fetch-depth: 0 + - name: Set up Go + uses: actions/setup-go@40f1582b2485089dde7abd97c1529aa768e1baff # v5.6.0 + with: + go-version-file: "go.mod" + - name: Run GoReleaser + uses: goreleaser/goreleaser-action@9ed2f89a662bf1735a48bc8557fd212fa902bebf # v6.1.0 + with: + version: latest + args: release --clean + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/release-doctor.yml b/.github/workflows/release-doctor.yml new file mode 100644 index 0000000..6b1e45d --- /dev/null +++ b/.github/workflows/release-doctor.yml @@ -0,0 +1,19 @@ +name: Release Doctor +on: + pull_request: + branches: + - main + workflow_dispatch: + +jobs: + release_doctor: + name: release doctor + runs-on: ubuntu-latest + if: github.repository == 'context-dot-dev/context-dev-cli' && (github.event_name == 'push' || github.event_name == 'workflow_dispatch' || startsWith(github.head_ref, 'release-please') || github.head_ref == 'next') + + steps: + - uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2 + + - name: Check release environment + run: | + bash ./bin/check-release-environment diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3006e55 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.prism.log +.stdy.log +dist/ +/context-dev +*.exe diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..87fb1b3 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,80 @@ +project_name: context-dev +version: 2 + +before: + hooks: + - mkdir -p completions + - sh -c "go run ./cmd/context-dev/main.go @completion bash > completions/context-dev.bash" + - sh -c "go run ./cmd/context-dev/main.go @completion zsh > completions/context-dev.zsh" + - sh -c "go run ./cmd/context-dev/main.go @completion fish > completions/context-dev.fish" + - sh -c "go run ./cmd/context-dev/main.go @manpages -o man" + +builds: + - id: macos + goos: [darwin] + goarch: [amd64, arm64] + binary: '{{ .ProjectName }}' + main: ./cmd/context-dev/main.go + mod_timestamp: '{{ .CommitTimestamp }}' + ldflags: + - '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}' + + - id: linux + goos: [linux] + goarch: ['386', arm, amd64, arm64] + env: + - CGO_ENABLED=0 + binary: '{{ .ProjectName }}' + main: ./cmd/context-dev/main.go + mod_timestamp: '{{ .CommitTimestamp }}' + ldflags: + - '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}' + + - id: windows + goos: [windows] + goarch: ['386', amd64, arm64] + binary: '{{ .ProjectName }}' + main: ./cmd/context-dev/main.go + mod_timestamp: '{{ .CommitTimestamp }}' + ldflags: + - '-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}' + +archives: + - id: linux-archive + ids: [linux] + name_template: '{{ .ProjectName }}_{{ .Version }}_linux_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}' + formats: [tar.gz] + files: + - completions/* + - man/*/* + - id: macos-archive + ids: [macos] + name_template: '{{ .ProjectName }}_{{ .Version }}_macos_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}' + formats: [zip] + files: + - completions/* + - man/*/* + - id: windows-archive + ids: [windows] + name_template: '{{ .ProjectName }}_{{ .Version }}_windows_{{ .Arch }}{{ if .Arm }}v{{ .Arm }}{{ end }}' + formats: [zip] + files: + - completions/* + - man/*/* + +snapshot: + version_template: '{{ .Tag }}-next' + +nfpms: + - license: Apache-2.0 + maintainer: hello@context.dev + bindir: /usr + formats: + - apk + - deb + - rpm + - termux.deb + - archlinux + contents: + - src: man/man1/*.1.gz + dst: /usr/share/man/man1/ diff --git a/.release-please-manifest.json b/.release-please-manifest.json new file mode 100644 index 0000000..c3c9552 --- /dev/null +++ b/.release-please-manifest.json @@ -0,0 +1,3 @@ +{ + ".": "1.9.0" +} \ No newline at end of file diff --git a/.stats.yml b/.stats.yml new file mode 100644 index 0000000..9aabd7d --- /dev/null +++ b/.stats.yml @@ -0,0 +1,4 @@ +configured_endpoints: 41 +openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/context-dev/context.dev-dc61df29e1b6282f9df685fadd65f3683afe352deccae94d0c080092e0eae4b3.yml +openapi_spec_hash: 258caa98cd0de5f7149f6937d071c912 +config_hash: 0fb0ceca5946298c416cec0cca5260c7 diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..dde764d --- /dev/null +++ b/LICENSE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2026 Context Dev + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. diff --git a/README.md b/README.md index 9aab08b..b36987c 100644 --- a/README.md +++ b/README.md @@ -1 +1,135 @@ -# context.dev-cli \ No newline at end of file +# Context Dev CLI + +The official CLI for the [Context Dev REST API](https://docs.context.dev/). + +It is generated with [Stainless](https://www.stainless.com/). + + + +## Installation + +### Installing with Go + +To test or install the CLI locally, you need [Go](https://go.dev/doc/install) version 1.22 or later installed. + +```sh +go install 'github.com/context-dot-dev/context-dev-cli/cmd/context-dev@latest' +``` + +Once you have run `go install`, the binary is placed in your Go bin directory: + +- **Default location**: `$HOME/go/bin` (or `$GOPATH/bin` if GOPATH is set) +- **Check your path**: Run `go env GOPATH` to see the base directory + +If commands aren't found after installation, add the Go bin directory to your PATH: + +```sh +# Add to your shell profile (.zshrc, .bashrc, etc.) +export PATH="$PATH:$(go env GOPATH)/bin" +``` + + + +### Running Locally + +After cloning the git repository for this project, you can use the +`scripts/run` script to run the tool locally: + +```sh +./scripts/run args... +``` + +## Usage + +The CLI follows a resource-based command structure: + +```sh +context-dev [resource] [flags...] +``` + +```sh +context-dev brand retrieve \ + --api-key 'My API Key' \ + --domain REPLACE_ME \ + --type by_domain +``` + +For details about specific commands, use the `--help` flag. + +### Environment variables + +| Environment variable | Required | +| --------------------- | -------- | +| `CONTEXT_DEV_API_KEY` | yes | + +### Global flags + +- `--api-key` (can also be set with `CONTEXT_DEV_API_KEY` env var) +- `--help` - Show command line usage +- `--debug` - Enable debug logging (includes HTTP request/response details) +- `--version`, `-v` - Show the CLI version +- `--base-url` - Use a custom API backend URL +- `--format` - Change the output format (`auto`, `explore`, `json`, `jsonl`, `pretty`, `raw`, `yaml`) +- `--format-error` - Change the output format for errors (`auto`, `explore`, `json`, `jsonl`, `pretty`, `raw`, `yaml`) +- `--transform` - Transform the data output using [GJSON syntax](https://github.com/tidwall/gjson/blob/master/SYNTAX.md) +- `--transform-error` - Transform the error output using [GJSON syntax](https://github.com/tidwall/gjson/blob/master/SYNTAX.md) + +### Passing files as arguments + +To pass files to your API, you can use the `@myfile.ext` syntax: + +```bash +context-dev --arg @abe.jpg +``` + +Files can also be passed inside JSON or YAML blobs: + +```bash +context-dev --arg '{image: "@abe.jpg"}' +# Equivalent: +context-dev < --username '\@abe' +``` + +#### Explicit encoding + +For JSON endpoints, the CLI tool does filetype sniffing to determine whether the +file contents should be sent as a string literal (for plain text files) or as a +base64-encoded string literal (for binary files). If you need to explicitly send +the file as either plain text or base64-encoded data, you can use +`@file://myfile.txt` (for string encoding) or `@data://myfile.dat` (for +base64-encoding). Note that absolute paths will begin with `@file://` or +`@data://`, followed by a third `/` (for example, `@file:///tmp/file.txt`). + +```bash +context-dev --arg @data://file.txt +``` + +## Linking different Go SDK versions + +You can link the CLI against a different version of the Context Dev Go SDK +for development purposes using the `./scripts/link` script. + +To link to a specific version from a repository (version can be a branch, +git tag, or commit hash): + +```bash +./scripts/link github.com/org/repo@version +``` + +To link to a local copy of the SDK: + +```bash +./scripts/link ../path/to/contextdev-go +``` + +If you run the link script without any arguments, it will default to `../contextdev-go`. diff --git a/SECURITY.md b/SECURITY.md new file mode 100644 index 0000000..22d7755 --- /dev/null +++ b/SECURITY.md @@ -0,0 +1,27 @@ +# Security Policy + +## Reporting Security Issues + +This SDK is generated by [Stainless Software Inc](http://stainless.com). Stainless takes security seriously, and encourages you to report any security vulnerability promptly so that appropriate action can be taken. + +To report a security issue, please contact the Stainless team at security@stainless.com. + +## Responsible Disclosure + +We appreciate the efforts of security researchers and individuals who help us maintain the security of +SDKs we generate. If you believe you have found a security vulnerability, please adhere to responsible +disclosure practices by allowing us a reasonable amount of time to investigate and address the issue +before making any information public. + +## Reporting Non-SDK Related Security Issues + +If you encounter security issues that are not directly related to SDKs but pertain to the services +or products provided by Context Dev, please follow the respective company's security reporting guidelines. + +### Context Dev Terms and Policies + +Please contact hello@context.dev for any questions or concerns regarding the security of our services. + +--- + +Thank you for helping us keep the SDKs and systems they interact with secure. diff --git a/bin/check-release-environment b/bin/check-release-environment new file mode 100644 index 0000000..1e951e9 --- /dev/null +++ b/bin/check-release-environment @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +errors=() + +lenErrors=${#errors[@]} + +if [[ lenErrors -gt 0 ]]; then + echo -e "Found the following errors in the release environment:\n" + + for error in "${errors[@]}"; do + echo -e "- $error\n" + done + + exit 1 +fi + +echo "The environment is ready to push releases!" diff --git a/cmd/context-dev/main.go b/cmd/context-dev/main.go new file mode 100644 index 0000000..7631be3 --- /dev/null +++ b/cmd/context-dev/main.go @@ -0,0 +1,74 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package main + +import ( + "context" + "errors" + "fmt" + "net/http" + "os" + "slices" + + "github.com/context-dot-dev/context-dev-cli/pkg/cmd" + "github.com/context-dot-dev/context-go-sdk/v2" + "github.com/tidwall/gjson" + "github.com/urfave/cli/v3" +) + +func main() { + app := cmd.Command + + if slices.Contains(os.Args, "__complete") { + prepareForAutocomplete(app) + } + + if baseURL, ok := os.LookupEnv("CONTEXT_DEV_BASE_URL"); ok { + if err := cmd.ValidateBaseURL(baseURL, "CONTEXT_DEV_BASE_URL"); err != nil { + fmt.Fprintf(os.Stderr, "%s\n", err.Error()) + os.Exit(1) + } + } + + if err := app.Run(context.Background(), os.Args); err != nil { + exitCode := 1 + + // Check if error has a custom exit code + if exitErr, ok := err.(cli.ExitCoder); ok { + exitCode = exitErr.ExitCode() + } + + var apierr *contextdev.Error + if errors.As(err, &apierr) { + fmt.Fprintf(os.Stderr, "%s %q: %d %s\n", apierr.Request.Method, apierr.Request.URL, apierr.Response.StatusCode, http.StatusText(apierr.Response.StatusCode)) + format := app.String("format-error") + json := gjson.Parse(apierr.RawJSON()) + show_err := cmd.ShowJSON(json, cmd.ShowJSONOpts{ + ExplicitFormat: app.IsSet("format-error"), + Format: format, + Title: "Error", + Transform: app.String("transform-error"), + }) + if show_err != nil { + // Just print the original error: + fmt.Fprintf(os.Stderr, "%s\n", err.Error()) + } + } else { + if cmd.CommandErrorBuffer.Len() > 0 { + os.Stderr.Write(cmd.CommandErrorBuffer.Bytes()) + } else { + fmt.Fprintf(os.Stderr, "%s\n", err.Error()) + } + } + os.Exit(exitCode) + } +} + +func prepareForAutocomplete(cmd *cli.Command) { + // urfave/cli does not handle flag completions and will print an error if we inspect a command with invalid flags. + // This skips that sort of validation + cmd.SkipFlagParsing = true + for _, child := range cmd.Commands { + prepareForAutocomplete(child) + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..8604556 --- /dev/null +++ b/go.mod @@ -0,0 +1,46 @@ +module github.com/context-dot-dev/context-dev-cli + +go 1.25 + +require ( + github.com/charmbracelet/bubbles v0.21.0 + github.com/charmbracelet/bubbletea v1.3.6 + github.com/charmbracelet/lipgloss v1.1.0 + github.com/charmbracelet/x/term v0.2.1 + github.com/context-dot-dev/context-go-sdk/v2 v2.13.0 + github.com/goccy/go-yaml v1.18.0 + github.com/itchyny/json2yaml v0.1.4 + github.com/muesli/reflow v0.3.0 + github.com/stretchr/testify v1.10.0 + github.com/tidwall/gjson v1.18.0 + github.com/tidwall/pretty v1.2.1 + github.com/urfave/cli-docs/v3 v3.0.0-alpha6 + github.com/urfave/cli/v3 v3.3.2 + golang.org/x/sys v0.38.0 +) + +require ( + github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect + github.com/charmbracelet/x/ansi v0.9.3 // indirect + github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect + github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect + github.com/mattn/go-localereader v0.0.1 // indirect + github.com/mattn/go-runewidth v0.0.16 // indirect + github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect + github.com/muesli/cancelreader v0.2.2 // indirect + github.com/muesli/termenv v0.16.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/rivo/uniseg v0.4.7 // indirect + github.com/russross/blackfriday/v2 v2.1.0 // indirect + github.com/tidwall/match v1.1.1 // indirect + github.com/tidwall/sjson v1.2.5 // indirect + github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect + golang.org/x/sync v0.15.0 // indirect + golang.org/x/text v0.3.8 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5415828 --- /dev/null +++ b/go.sum @@ -0,0 +1,89 @@ +github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= +github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= +github.com/aymanbagabas/go-udiff v0.2.0 h1:TK0fH4MteXUDspT88n8CKzvK0X9O2xu9yQjWpi6yML8= +github.com/aymanbagabas/go-udiff v0.2.0/go.mod h1:RE4Ex0qsGkTAJoQdQQCA0uG+nAzJO/pI/QwceO5fgrA= +github.com/charmbracelet/bubbles v0.21.0 h1:9TdC97SdRVg/1aaXNVWfFH3nnLAwOXr8Fn6u6mfQdFs= +github.com/charmbracelet/bubbles v0.21.0/go.mod h1:HF+v6QUR4HkEpz62dx7ym2xc71/KBHg+zKwJtMw+qtg= +github.com/charmbracelet/bubbletea v1.3.6 h1:VkHIxPJQeDt0aFJIsVxw8BQdh/F/L2KKZGsK6et5taU= +github.com/charmbracelet/bubbletea v1.3.6/go.mod h1:oQD9VCRQFF8KplacJLo28/jofOI2ToOfGYeFgBBxHOc= +github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc h1:4pZI35227imm7yK2bGPcfpFEmuY1gc2YSTShr4iJBfs= +github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc/go.mod h1:X4/0JoqgTIPSFcRA/P6INZzIuyqdFY5rm8tb41s9okk= +github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoFqR/noCY= +github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30= +github.com/charmbracelet/x/ansi v0.9.3 h1:BXt5DHS/MKF+LjuK4huWrC6NCvHtexww7dMayh6GXd0= +github.com/charmbracelet/x/ansi v0.9.3/go.mod h1:3RQDQ6lDnROptfpWuUVIUG64bD2g2BgntdxH0Ya5TeE= +github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd h1:vy0GVL4jeHEwG5YOXDmi86oYw2yuYUGqz6a8sLwg0X8= +github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd/go.mod h1:xe0nKWGd3eJgtqZRaN9RjMtK7xUYchjzPr7q6kcvCCs= +github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91 h1:payRxjMjKgx2PaCWLZ4p3ro9y97+TVLZNaRZgJwSVDQ= +github.com/charmbracelet/x/exp/golden v0.0.0-20241011142426-46044092ad91/go.mod h1:wDlXFlCrmJ8J+swcL/MnGUuYnqgQdW9rhSD61oNMb6U= +github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ= +github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg= +github.com/context-dot-dev/context-go-sdk/v2 v2.13.0 h1:4XJmz1E3jso9ngv8WEN0sfQmp5+2tgDT2xl4GVTu0Ug= +github.com/context-dot-dev/context-go-sdk/v2 v2.13.0/go.mod h1:CvRANWLP0FHj5XyUwzEVX79ujdwr/2Jy375UVzKNQm8= +github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w= +github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4= +github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM= +github.com/goccy/go-yaml v1.18.0 h1:8W7wMFS12Pcas7KU+VVkaiCng+kG8QiFeFwzFb+rwuw= +github.com/goccy/go-yaml v1.18.0/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= +github.com/itchyny/json2yaml v0.1.4 h1:/pErVOXGG5iTyXHi/QKR4y3uzhLjGTEmmJIy97YT+k8= +github.com/itchyny/json2yaml v0.1.4/go.mod h1:6iudhBZdarpjLFRNj+clWLAkGft+9uCcjAZYXUH9eGI= +github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= +github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= +github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4= +github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88= +github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= +github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= +github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI= +github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo= +github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA= +github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= +github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= +github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= +github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc= +github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= +github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY= +github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk= +github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA= +github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM= +github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4= +github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU= +github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY= +github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28= +github.com/urfave/cli-docs/v3 v3.0.0-alpha6 h1:w/l/N0xw1rO/aHRIGXJ0lDwwYFOzilup1qGvIytP3BI= +github.com/urfave/cli-docs/v3 v3.0.0-alpha6/go.mod h1:p7Z4lg8FSTrPB9GTaNyTrK3ygffHZcK3w0cU2VE+mzU= +github.com/urfave/cli/v3 v3.3.2 h1:BYFVnhhZ8RqT38DxEYVFPPmGFTEf7tJwySTXsVRrS/o= +github.com/urfave/cli/v3 v3.3.2/go.mod h1:FJSKtM/9AiiTOJL4fJ6TbMUkxBXn7GO9guZqoZtpYpo= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no= +github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= +golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E= +golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= +golang.org/x/sync v0.15.0 h1:KWH3jNZsfyT6xfAfKiz6MRNmd46ByHDYaZ7KSkCtdW8= +golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= +golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.38.0 h1:3yZWxaJjBmCWXqhN1qh02AkOnCQ1poK6oF+a7xWL6Gc= +golang.org/x/sys v0.38.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= +golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY= +golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/apiform/encoder.go b/internal/apiform/encoder.go new file mode 100644 index 0000000..857fe14 --- /dev/null +++ b/internal/apiform/encoder.go @@ -0,0 +1,236 @@ +package apiform + +import ( + "fmt" + "io" + "mime/multipart" + "net/textproto" + "path" + "reflect" + "sort" + "strconv" + "strings" +) + +// Marshal encodes a value as multipart form data using default settings +func Marshal(value any, writer *multipart.Writer) error { + e := &encoder{ + format: FormatRepeat, + } + return e.marshal(value, writer) +} + +// MarshalWithSettings encodes a value with custom array format +func MarshalWithSettings(value any, writer *multipart.Writer, arrayFormat FormFormat) error { + e := &encoder{ + format: arrayFormat, + } + return e.marshal(value, writer) +} + +type encoder struct { + format FormFormat +} + +func (e *encoder) marshal(value any, writer *multipart.Writer) error { + val := reflect.ValueOf(value) + if !val.IsValid() { + return nil + } + return e.encodeValue("", val, writer) +} + +func (e *encoder) encodeValue(key string, val reflect.Value, writer *multipart.Writer) error { + if !val.IsValid() { + return writer.WriteField(key, "") + } + + t := val.Type() + + if t.Implements(reflect.TypeOf((*io.Reader)(nil)).Elem()) { + return e.encodeReader(key, val, writer) + } + + switch t.Kind() { + case reflect.Pointer: + if val.IsNil() || !val.IsValid() { + return writer.WriteField(key, "") + } + return e.encodeValue(key, val.Elem(), writer) + + case reflect.Slice, reflect.Array: + return e.encodeArray(key, val, writer) + + case reflect.Map: + return e.encodeMap(key, val, writer) + + case reflect.Interface: + if val.IsNil() { + return writer.WriteField(key, "") + } + return e.encodeValue(key, val.Elem(), writer) + + case reflect.String: + return writer.WriteField(key, val.String()) + + case reflect.Bool: + if val.Bool() { + return writer.WriteField(key, "true") + } + return writer.WriteField(key, "false") + + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return writer.WriteField(key, strconv.FormatInt(val.Int(), 10)) + + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + return writer.WriteField(key, strconv.FormatUint(val.Uint(), 10)) + + case reflect.Float32: + return writer.WriteField(key, strconv.FormatFloat(val.Float(), 'f', -1, 32)) + + case reflect.Float64: + return writer.WriteField(key, strconv.FormatFloat(val.Float(), 'f', -1, 64)) + + default: + return fmt.Errorf("unknown type: %s", t.String()) + } +} + +func (e *encoder) encodeArray(key string, val reflect.Value, writer *multipart.Writer) error { + if e.format == FormatComma { + var values []string + for i := 0; i < val.Len(); i++ { + item := val.Index(i) + if (item.Kind() == reflect.Pointer || item.Kind() == reflect.Interface) && item.IsNil() { + // Null values are sent as an empty string + values = append(values, "") + continue + } + // If item is an interface, reduce it to the concrete type + if item.Kind() == reflect.Interface { + item = item.Elem() + } + var strValue string + switch item.Kind() { + case reflect.String: + strValue = item.String() + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + strValue = strconv.FormatInt(item.Int(), 10) + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + strValue = strconv.FormatUint(item.Uint(), 10) + case reflect.Float32, reflect.Float64: + strValue = strconv.FormatFloat(item.Float(), 'f', -1, 64) + case reflect.Bool: + strValue = strconv.FormatBool(item.Bool()) + default: + return fmt.Errorf("comma format not supported for complex array elements") + } + values = append(values, strValue) + } + return writer.WriteField(key, strings.Join(values, ",")) + } + + for i := 0; i < val.Len(); i++ { + var formattedKey string + switch e.format { + case FormatRepeat: + formattedKey = key + case FormatBrackets: + formattedKey = key + "[]" + case FormatIndicesDots: + if key == "" { + formattedKey = strconv.Itoa(i) + } else { + formattedKey = key + "." + strconv.Itoa(i) + } + case FormatIndicesBrackets: + if key == "" { + formattedKey = strconv.Itoa(i) + } else { + formattedKey = key + "[" + strconv.Itoa(i) + "]" + } + default: + return fmt.Errorf("apiform: unsupported array format") + } + + if err := e.encodeValue(formattedKey, val.Index(i), writer); err != nil { + return err + } + } + return nil +} + +var quoteEscaper = strings.NewReplacer("\\", "\\\\", `"`, "\\\"") + +func escapeQuotes(s string) string { + return quoteEscaper.Replace(s) +} + +func (e *encoder) encodeReader(key string, val reflect.Value, writer *multipart.Writer) error { + reader, ok := val.Convert(reflect.TypeOf((*io.Reader)(nil)).Elem()).Interface().(io.Reader) + if !ok { + return nil + } + + // Set defaults + filename := "anonymous_file" + contentType := "application/octet-stream" + + // Get filename if available + if named, ok := reader.(interface{ Filename() string }); ok { + filename = named.Filename() + } else if named, ok := reader.(interface{ Name() string }); ok { + filename = path.Base(named.Name()) + } + + // Get content type if available + if typed, ok := reader.(interface{ ContentType() string }); ok { + contentType = typed.ContentType() + } + + h := make(textproto.MIMEHeader) + h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="%s"; filename="%s"`, + escapeQuotes(key), escapeQuotes(filename))) + h.Set("Content-Type", contentType) + + filewriter, err := writer.CreatePart(h) + if err != nil { + return err + } + _, err = io.Copy(filewriter, reader) + return err +} + +func (e *encoder) encodeMap(key string, val reflect.Value, writer *multipart.Writer) error { + type mapPair struct { + key string + value reflect.Value + } + + if key != "" { + key = key + "." + } + + // Collect and sort map entries for deterministic output + pairs := []mapPair{} + iter := val.MapRange() + for iter.Next() { + if iter.Key().Type().Kind() != reflect.String { + return fmt.Errorf("cannot encode a map with a non string key") + } + pairs = append(pairs, mapPair{key: iter.Key().String(), value: iter.Value()}) + } + + sort.Slice(pairs, func(i, j int) bool { + return pairs[i].key < pairs[j].key + }) + + // Process sorted pairs + for _, p := range pairs { + if err := e.encodeValue(key+p.key, p.value, writer); err != nil { + return err + } + } + + return nil +} diff --git a/internal/apiform/form.go b/internal/apiform/form.go new file mode 100644 index 0000000..024de27 --- /dev/null +++ b/internal/apiform/form.go @@ -0,0 +1,20 @@ +package apiform + +type Marshaler interface { + MarshalMultipart() ([]byte, string, error) +} + +type FormFormat int + +const ( + // FormatRepeat represents arrays as repeated keys with the same value + FormatRepeat FormFormat = iota + // Comma-separated values 1,2,3 + FormatComma + // FormatBrackets uses the key[] notation for arrays + FormatBrackets + // FormatIndicesDots uses key.0, key.1, etc. notation + FormatIndicesDots + // FormatIndicesBrackets uses key[0], key[1], etc. notation + FormatIndicesBrackets +) diff --git a/internal/apiform/form_test.go b/internal/apiform/form_test.go new file mode 100644 index 0000000..f68cfd1 --- /dev/null +++ b/internal/apiform/form_test.go @@ -0,0 +1,113 @@ +package apiform + +import ( + "bytes" + "mime/multipart" + "testing" +) + +// Define test cases +var tests = map[string]struct { + value any + format FormFormat + expected string +}{ + "nil": { + value: nil, + expected: "--xxx\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\n\r\n--xxx--\r\n", + }, + "string": { + value: "hello", + expected: "--xxx\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\nhello\r\n--xxx--\r\n", + }, + "int": { + value: 42, + expected: "--xxx\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\n42\r\n--xxx--\r\n", + }, + "float": { + value: 3.14, + expected: "--xxx\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\n3.14\r\n--xxx--\r\n", + }, + "bool": { + value: true, + expected: "--xxx\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\ntrue\r\n--xxx--\r\n", + }, + "empty slice": { + value: []string{}, + expected: "\r\n--xxx--\r\n", + }, + "nil slice": { + value: []string(nil), + expected: "\r\n--xxx--\r\n", + }, + "slice with dot indices": { + value: []string{"a", "b", "c"}, + format: FormatIndicesDots, + expected: "--xxx\r\nContent-Disposition: form-data; name=\"foo.0\"\r\n\r\na\r\n--xxx\r\nContent-Disposition: form-data; name=\"foo.1\"\r\n\r\nb\r\n--xxx\r\nContent-Disposition: form-data; name=\"foo.2\"\r\n\r\nc\r\n--xxx--\r\n", + }, + "slice with bracket indices": { + value: []int{10, 20, 30}, + format: FormatIndicesBrackets, + expected: "--xxx\r\nContent-Disposition: form-data; name=\"foo[0]\"\r\n\r\n10\r\n--xxx\r\nContent-Disposition: form-data; name=\"foo[1]\"\r\n\r\n20\r\n--xxx\r\nContent-Disposition: form-data; name=\"foo[2]\"\r\n\r\n30\r\n--xxx--\r\n", + }, + "slice with repeat": { + value: []int{10, 20, 30}, + format: FormatRepeat, + expected: "--xxx\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\n10\r\n--xxx\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\n20\r\n--xxx\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\n30\r\n--xxx--\r\n", + }, + "slice with commas": { + value: []int{10, 20, 30}, + format: FormatComma, + expected: "--xxx\r\nContent-Disposition: form-data; name=\"foo\"\r\n\r\n10,20,30\r\n--xxx--\r\n", + }, + "empty map": { + value: map[string]any{}, + expected: "\r\n--xxx--\r\n", + }, + "nil map": { + value: map[string]any(nil), + expected: "\r\n--xxx--\r\n", + }, + "map": { + value: map[string]any{"key1": "value1", "key2": "value2"}, + expected: "--xxx\r\nContent-Disposition: form-data; name=\"foo.key1\"\r\n\r\nvalue1\r\n--xxx\r\nContent-Disposition: form-data; name=\"foo.key2\"\r\n\r\nvalue2\r\n--xxx--\r\n", + }, + "nested_map": { + value: map[string]any{"outer": map[string]int{"inner1": 10, "inner2": 20}}, + format: FormatIndicesDots, + expected: "--xxx\r\nContent-Disposition: form-data; name=\"foo.outer.inner1\"\r\n\r\n10\r\n--xxx\r\nContent-Disposition: form-data; name=\"foo.outer.inner2\"\r\n\r\n20\r\n--xxx--\r\n", + }, + "mixed_map": { + value: map[string]any{"name": "John", "ages": []int{25, 30, 35}}, + format: FormatIndicesDots, + expected: "--xxx\r\nContent-Disposition: form-data; name=\"foo.ages.0\"\r\n\r\n25\r\n--xxx\r\nContent-Disposition: form-data; name=\"foo.ages.1\"\r\n\r\n30\r\n--xxx\r\nContent-Disposition: form-data; name=\"foo.ages.2\"\r\n\r\n35\r\n--xxx\r\nContent-Disposition: form-data; name=\"foo.name\"\r\n\r\nJohn\r\n--xxx--\r\n", + }, +} + +func TestEncode(t *testing.T) { + t.Parallel() + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + t.Parallel() + + buf := bytes.NewBuffer(nil) + writer := multipart.NewWriter(buf) + writer.SetBoundary("xxx") + + form := map[string]any{"foo": test.value} + err := MarshalWithSettings(form, writer, test.format) + if err != nil { + t.Errorf("serialization of %v failed with error %v", test.value, err) + } + err = writer.Close() + if err != nil { + t.Errorf("serialization of %v failed with error %v", test.value, err) + } + result := buf.String() + if result != test.expected { + t.Errorf("expected %+#v to serialize to:\n\t%q\nbut got:\n\t%q", test.value, test.expected, result) + } + }) + } +} diff --git a/internal/apiquery/encoder.go b/internal/apiquery/encoder.go new file mode 100644 index 0000000..0d09dee --- /dev/null +++ b/internal/apiquery/encoder.go @@ -0,0 +1,166 @@ +package apiquery + +import ( + "fmt" + "reflect" + "strconv" + "strings" +) + +type encoder struct { + settings QuerySettings +} + +type Pair struct { + key string + value string +} + +func (e *encoder) Encode(key string, value reflect.Value) ([]Pair, error) { + t := value.Type() + switch t.Kind() { + case reflect.Pointer: + if value.IsNil() || !value.IsValid() { + return []Pair{{key, ""}}, nil + } + return e.Encode(key, value.Elem()) + + case reflect.Array, reflect.Slice: + return e.encodeArray(key, value) + + case reflect.Map: + return e.encodeMap(key, value) + + case reflect.Interface: + if !value.Elem().IsValid() { + return []Pair{{key, ""}}, nil + } + return e.Encode(key, value.Elem()) + + default: + return e.encodePrimitive(key, value) + } +} + +func (e *encoder) encodeMap(key string, value reflect.Value) ([]Pair, error) { + var pairs []Pair + iter := value.MapRange() + for iter.Next() { + subkey := iter.Key().String() + keyPath := subkey + if len(key) > 0 { + if e.settings.NestedFormat == NestedQueryFormatDots { + keyPath = fmt.Sprintf("%s.%s", key, subkey) + } else { + keyPath = fmt.Sprintf("%s[%s]", key, subkey) + } + } + + subpairs, err := e.Encode(keyPath, iter.Value()) + if err != nil { + return nil, err + } + pairs = append(pairs, subpairs...) + } + return pairs, nil +} + +func (e *encoder) encodeArray(key string, value reflect.Value) ([]Pair, error) { + switch e.settings.ArrayFormat { + case ArrayQueryFormatComma: + elements := []string{} + for i := 0; i < value.Len(); i++ { + innerPairs, err := e.Encode("", value.Index(i)) + if err != nil { + return nil, err + } + for _, pair := range innerPairs { + elements = append(elements, pair.value) + } + } + return []Pair{{key, strings.Join(elements, ",")}}, nil + + case ArrayQueryFormatRepeat: + var pairs []Pair + for i := 0; i < value.Len(); i++ { + subpairs, err := e.Encode(key, value.Index(i)) + if err != nil { + return nil, err + } + pairs = append(pairs, subpairs...) + } + return pairs, nil + + case ArrayQueryFormatIndices: + var pairs []Pair + for i := 0; i < value.Len(); i++ { + subpairs, err := e.Encode(fmt.Sprintf("%s[%d]", key, i), value.Index(i)) + if err != nil { + return nil, err + } + pairs = append(pairs, subpairs...) + } + return pairs, nil + + case ArrayQueryFormatBrackets: + var pairs []Pair + for i := 0; i < value.Len(); i++ { + subpairs, err := e.Encode(key+"[]", value.Index(i)) + if err != nil { + return nil, err + } + pairs = append(pairs, subpairs...) + } + return pairs, nil + + default: + panic(fmt.Sprintf("Unknown ArrayFormat value: %d", e.settings.ArrayFormat)) + } +} + +func (e *encoder) encodePrimitive(key string, value reflect.Value) ([]Pair, error) { + switch value.Kind() { + case reflect.Pointer: + if !value.IsValid() || value.IsNil() { + return nil, nil + } + return e.encodePrimitive(key, value.Elem()) + + case reflect.String: + return []Pair{{key, value.String()}}, nil + + case reflect.Bool: + if value.Bool() { + return []Pair{{key, "true"}}, nil + } + return []Pair{{key, "false"}}, nil + + case reflect.Int, reflect.Int16, reflect.Int32, reflect.Int64: + return []Pair{{key, strconv.FormatInt(value.Int(), 10)}}, nil + + case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64: + return []Pair{{key, strconv.FormatUint(value.Uint(), 10)}}, nil + + case reflect.Float32, reflect.Float64: + return []Pair{{key, strconv.FormatFloat(value.Float(), 'f', -1, 64)}}, nil + + default: + return nil, nil + } +} + +func (e *encoder) encodeField(key string, value reflect.Value) ([]Pair, error) { + present := value.FieldByName("Present") + if !present.Bool() { + return nil, nil + } + null := value.FieldByName("Null") + if null.Bool() { + return nil, fmt.Errorf("apiquery: field cannot be null") + } + raw := value.FieldByName("Raw") + if !raw.IsNil() { + return e.Encode(key, raw) + } + return e.Encode(key, value.FieldByName("Value")) +} diff --git a/internal/apiquery/query.go b/internal/apiquery/query.go new file mode 100644 index 0000000..fd07a2f --- /dev/null +++ b/internal/apiquery/query.go @@ -0,0 +1,53 @@ +package apiquery + +import ( + "net/url" + "reflect" +) + +func MarshalWithSettings(value any, settings QuerySettings) (url.Values, error) { + val := reflect.ValueOf(value) + if !val.IsValid() { + return nil, nil + } + + e := encoder{settings} + pairs, err := e.Encode("", val) + if err != nil { + return nil, err + } + + kv := url.Values{} + for _, pair := range pairs { + kv.Add(pair.key, pair.value) + } + return kv, nil +} +func Marshal(value any) (url.Values, error) { + return MarshalWithSettings(value, QuerySettings{}) +} + +type Queryer interface { + URLQuery() (url.Values, error) +} + +type NestedQueryFormat int + +const ( + NestedQueryFormatBrackets NestedQueryFormat = iota + NestedQueryFormatDots +) + +type ArrayQueryFormat int + +const ( + ArrayQueryFormatComma ArrayQueryFormat = iota + ArrayQueryFormatRepeat + ArrayQueryFormatIndices + ArrayQueryFormatBrackets +) + +type QuerySettings struct { + NestedFormat NestedQueryFormat + ArrayFormat ArrayQueryFormat +} diff --git a/internal/apiquery/query_test.go b/internal/apiquery/query_test.go new file mode 100644 index 0000000..3791ec9 --- /dev/null +++ b/internal/apiquery/query_test.go @@ -0,0 +1,132 @@ +package apiquery + +import ( + "net/url" + "testing" +) + +func TestEncode(t *testing.T) { + t.Parallel() + + tests := map[string]struct { + val any + settings QuerySettings + enc string + }{ + "null": { + val: nil, + enc: "query=", + }, + "string": { + val: "hello world", + enc: "query=hello world", + }, + "int": { + val: 42, + enc: "query=42", + }, + "float": { + val: 3.14, + enc: "query=3.14", + }, + "bool": { + val: true, + enc: "query=true", + }, + "empty_slice": { + val: []any{}, + settings: QuerySettings{ArrayFormat: ArrayQueryFormatComma}, + enc: "query=", + }, + "nil_slice": { + val: []any(nil), + settings: QuerySettings{ArrayFormat: ArrayQueryFormatComma}, + enc: "query=", + }, + "slice_of_ints": { + val: []any{10, 20, 30}, + settings: QuerySettings{ArrayFormat: ArrayQueryFormatComma}, + enc: "query=10,20,30", + }, + "slice_of_ints_repeat": { + val: []any{10, 20, 30}, + settings: QuerySettings{ArrayFormat: ArrayQueryFormatRepeat}, + enc: "query=10&query=20&query=30", + }, + "slice_of_ints_indices": { + val: []any{10, 20, 30}, + settings: QuerySettings{ArrayFormat: ArrayQueryFormatIndices}, + enc: "query[0]=10&query[1]=20&query[2]=30", + }, + "slice_of_ints_brackets": { + val: []any{10, 20, 30}, + settings: QuerySettings{ArrayFormat: ArrayQueryFormatBrackets}, + enc: "query[]=10&query[]=20&query[]=30", + }, + "slice_of_strings": { + val: []any{"a", "b", "c"}, + settings: QuerySettings{}, + enc: "query=a,b,c", + }, + "empty_map": { + val: map[string]any{}, + settings: QuerySettings{NestedFormat: NestedQueryFormatBrackets}, + enc: "", + }, + "nil_map": { + val: map[string]any(nil), + settings: QuerySettings{NestedFormat: NestedQueryFormatBrackets}, + enc: "", + }, + "map_string_to_int_brackets": { + val: map[string]any{"one": 1, "two": 2}, + settings: QuerySettings{NestedFormat: NestedQueryFormatBrackets}, + enc: "query[one]=1&query[two]=2", + }, + "map_string_to_int_dots": { + val: map[string]any{"one": 1, "two": 2}, + settings: QuerySettings{NestedFormat: NestedQueryFormatDots}, + enc: "query.one=1&query.two=2", + }, + "map_string_to_slice": { + val: map[string][]any{"nums": {10, 20, 30}}, + settings: QuerySettings{}, + enc: "query[nums]=10,20,30", + }, + "map_string_to_slice_repeat_dots": { + val: map[string][]any{"nums": {10, 20, 30}}, + settings: QuerySettings{ArrayFormat: ArrayQueryFormatRepeat, NestedFormat: NestedQueryFormatDots}, + enc: "query.nums=10&query.nums=20&query.nums=30", + }, + "map_with_empties": { + val: map[string]any{ + "empty-array": []any{}, + "nil-array": []any(nil), + "null": nil, + }, + settings: QuerySettings{ArrayFormat: ArrayQueryFormatComma, NestedFormat: NestedQueryFormatDots}, + enc: "query.empty-array=&query.nil-array=&query.null=", + }, + "nested_map": { + val: map[string]map[string]any{"outer": {"inner": 42}}, + settings: QuerySettings{}, + enc: "query[outer][inner]=42", + }, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + t.Parallel() + + query := map[string]any{"query": test.val} + values, err := MarshalWithSettings(query, test.settings) + if err != nil { + t.Fatalf("failed to marshal url %s", err) + } + str, _ := url.QueryUnescape(values.Encode()) + if str != test.enc { + t.Fatalf("expected %+#v to serialize to:\n\t%q\nbut got:\n\t%q", test.val, test.enc, str) + } + }) + } +} diff --git a/internal/autocomplete/autocomplete.go b/internal/autocomplete/autocomplete.go new file mode 100644 index 0000000..97fe1a8 --- /dev/null +++ b/internal/autocomplete/autocomplete.go @@ -0,0 +1,361 @@ +package autocomplete + +import ( + "context" + "embed" + "fmt" + "os" + "slices" + "strings" + + "github.com/urfave/cli/v3" +) + +type CompletionStyle string + +const ( + CompletionStyleZsh CompletionStyle = "zsh" + CompletionStyleBash CompletionStyle = "bash" + CompletionStylePowershell CompletionStyle = "pwsh" + CompletionStyleFish CompletionStyle = "fish" +) + +type renderCompletion func(cmd *cli.Command, appName string) (string, error) + +var ( + //go:embed shellscripts + autoCompleteFS embed.FS + + shellCompletions = map[CompletionStyle]renderCompletion{ + "bash": func(c *cli.Command, appName string) (string, error) { + b, err := autoCompleteFS.ReadFile("shellscripts/bash_autocomplete.bash") + return strings.ReplaceAll(string(b), "__APPNAME__", appName), err + }, + "fish": func(c *cli.Command, appName string) (string, error) { + b, err := autoCompleteFS.ReadFile("shellscripts/fish_autocomplete.fish") + return strings.ReplaceAll(string(b), "__APPNAME__", appName), err + }, + "pwsh": func(c *cli.Command, appName string) (string, error) { + b, err := autoCompleteFS.ReadFile("shellscripts/pwsh_autocomplete.ps1") + return strings.ReplaceAll(string(b), "__APPNAME__", appName), err + }, + "zsh": func(c *cli.Command, appName string) (string, error) { + b, err := autoCompleteFS.ReadFile("shellscripts/zsh_autocomplete.zsh") + return strings.ReplaceAll(string(b), "__APPNAME__", appName), err + }, + } +) + +func OutputCompletionScript(ctx context.Context, cmd *cli.Command) error { + shells := make([]CompletionStyle, 0, len(shellCompletions)) + for k := range shellCompletions { + shells = append(shells, k) + } + + if cmd.Args().Len() == 0 { + return cli.Exit(fmt.Sprintf("no shell provided for completion command. available shells are %+v", shells), 1) + } + s := CompletionStyle(cmd.Args().First()) + + renderCompletion, ok := shellCompletions[s] + if !ok { + return cli.Exit(fmt.Sprintf("unknown shell %s, available shells are %+v", s, shells), 1) + } + + completionScript, err := renderCompletion(cmd, cmd.Root().Name) + if err != nil { + return cli.Exit(err, 1) + } + + _, err = cmd.Writer.Write([]byte(completionScript)) + if err != nil { + return cli.Exit(err, 1) + } + + return nil +} + +type ShellCompletion struct { + Name string + Usage string +} + +func NewShellCompletion(name string, usage string) ShellCompletion { + return ShellCompletion{Name: name, Usage: usage} +} + +type ShellCompletionBehavior int + +const ( + ShellCompletionBehaviorDefault ShellCompletionBehavior = iota + ShellCompletionBehaviorFile = 10 + ShellCompletionBehaviorNoComplete +) + +type CompletionResult struct { + Completions []ShellCompletion + Behavior ShellCompletionBehavior +} + +func isFlag(arg string) bool { + return strings.HasPrefix(arg, "-") +} + +func findFlag(cmd *cli.Command, arg string) *cli.Flag { + name := strings.TrimLeft(arg, "-") + for _, flag := range cmd.Flags { + if vf, ok := flag.(cli.VisibleFlag); ok && !vf.IsVisible() { + continue + } + + if slices.Contains(flag.Names(), name) { + return &flag + } + } + return nil +} + +func findChild(cmd *cli.Command, name string) *cli.Command { + for _, c := range cmd.Commands { + if !c.Hidden && c.Name == name { + return c + } + } + return nil +} + +type shellCompletionBuilder struct { + completionStyle CompletionStyle +} + +func (scb *shellCompletionBuilder) createFromCommand(input string, command *cli.Command, result []ShellCompletion) []ShellCompletion { + matchingNames := make([]string, 0, len(command.Names())) + + for _, name := range command.Names() { + if strings.HasPrefix(name, input) { + matchingNames = append(matchingNames, name) + } + } + + if scb.completionStyle == CompletionStyleBash { + index := strings.LastIndex(input, ":") + 1 + if index > 0 { + for _, name := range matchingNames { + result = append(result, NewShellCompletion(name[index:], command.Usage)) + } + return result + } + } + + for _, name := range matchingNames { + result = append(result, NewShellCompletion(name, command.Usage)) + } + return result +} + +func (scb *shellCompletionBuilder) createFromFlag(input string, flag *cli.Flag, result []ShellCompletion) []ShellCompletion { + matchingNames := make([]string, 0, len((*flag).Names())) + + for _, name := range (*flag).Names() { + withPrefix := "" + if len(name) == 1 { + withPrefix = "-" + name + } else { + withPrefix = "--" + name + } + + if strings.HasPrefix(withPrefix, input) { + matchingNames = append(matchingNames, withPrefix) + } + } + + usage := "" + if dgf, ok := (*flag).(cli.DocGenerationFlag); ok { + usage = dgf.GetUsage() + } + + for _, name := range matchingNames { + result = append(result, NewShellCompletion(name, usage)) + } + + return result +} + +func GetCompletions(completionStyle CompletionStyle, root *cli.Command, args []string) CompletionResult { + result := getAllPossibleCompletions(completionStyle, root, args) + + // If the user has not put in a colon, filter out colon commands + if len(args) > 0 && !strings.Contains(args[len(args)-1], ":") { + // Nothing with anything after a colon. Create a single entry for groups with the same colon subset + foundNames := make([]string, 0, len(result.Completions)) + filteredCompletions := make([]ShellCompletion, 0, len(result.Completions)) + + for _, completion := range result.Completions { + name := completion.Name + firstColonIndex := strings.Index(name, ":") + if firstColonIndex > -1 { + name = name[0:firstColonIndex] + completion.Name = name + completion.Usage = "" + } + + if !slices.Contains(foundNames, name) { + foundNames = append(foundNames, name) + filteredCompletions = append(filteredCompletions, completion) + } + } + + result.Completions = filteredCompletions + } + + return result +} + +func getAllPossibleCompletions(completionStyle CompletionStyle, root *cli.Command, args []string) CompletionResult { + builder := shellCompletionBuilder{completionStyle: completionStyle} + completions := make([]ShellCompletion, 0) + if len(args) == 0 { + for _, child := range root.Commands { + completions = builder.createFromCommand("", child, completions) + } + return CompletionResult{Completions: completions, Behavior: ShellCompletionBehaviorDefault} + } + + current := args[len(args)-1] + preceding := args[0 : len(args)-1] + cmd := root + i := 0 + for i < len(preceding) { + arg := preceding[i] + + if isFlag(arg) { + flag := findFlag(cmd, arg) + if flag == nil { + i++ + } else if docFlag, ok := (*flag).(cli.DocGenerationFlag); ok && docFlag.TakesValue() { + // All flags except for bool flags take values + i += 2 + } else { + i++ + } + } else { + child := findChild(cmd, arg) + if child != nil { + cmd = child + } + i++ + } + } + + // Check if the previous arg was a flag expecting a value + if len(preceding) > 0 { + prev := preceding[len(preceding)-1] + if isFlag(prev) { + flag := findFlag(cmd, prev) + if flag != nil { + if fb, ok := (*flag).(*cli.StringFlag); ok && fb.TakesFile { + return CompletionResult{Completions: completions, Behavior: ShellCompletionBehaviorFile} + } else if docFlag, ok := (*flag).(cli.DocGenerationFlag); ok && docFlag.TakesValue() { + return CompletionResult{Completions: completions, Behavior: ShellCompletionBehaviorNoComplete} + } + } + } + } + + // Completing a flag name + if isFlag(current) { + for _, flag := range cmd.Flags { + completions = builder.createFromFlag(current, &flag, completions) + } + } + + for _, child := range cmd.Commands { + if !child.Hidden { + completions = builder.createFromCommand(current, child, completions) + } + } + + return CompletionResult{ + Completions: completions, + Behavior: ShellCompletionBehaviorDefault, + } +} + +func ExecuteShellCompletion(ctx context.Context, cmd *cli.Command) error { + root := cmd.Root() + args := rebuildColonSeparatedArgs(root.Args().Slice()[1:]) + + var completionStyle CompletionStyle + if style, ok := os.LookupEnv("COMPLETION_STYLE"); ok { + switch style { + case "bash": + completionStyle = CompletionStyleBash + case "zsh": + completionStyle = CompletionStyleZsh + case "pwsh": + completionStyle = CompletionStylePowershell + case "fish": + completionStyle = CompletionStyleFish + default: + return cli.Exit("COMPLETION_STYLE must be set to 'bash', 'zsh', 'pwsh', or 'fish'", 1) + } + } else { + return cli.Exit("COMPLETION_STYLE must be set to 'bash', 'zsh', 'pwsh', 'fish'", 1) + } + + result := GetCompletions(completionStyle, root, args) + + for _, completion := range result.Completions { + name := completion.Name + if completionStyle == CompletionStyleZsh { + name = strings.ReplaceAll(name, ":", "\\:") + } + if completionStyle == CompletionStyleZsh && len(completion.Usage) > 0 { + _, _ = fmt.Fprintf(cmd.Writer, "%s:%s\n", name, completion.Usage) + } else if completionStyle == CompletionStyleFish && len(completion.Usage) > 0 { + _, _ = fmt.Fprintf(cmd.Writer, "%s\t%s\n", name, completion.Usage) + } else { + _, _ = fmt.Fprintf(cmd.Writer, "%s\n", name) + } + } + return cli.Exit("", int(result.Behavior)) +} + +// When CLI arguments are passed in, they are separated on word barriers. +// Most commonly this is whitespace but in some cases that may also be colons. +// We wish to allow arguments with colons. To handle this, we append/prepend colons to their neighboring +// arguments. +// +// Example: `rebuildColonSeparatedArgs(["a", "b", ":", "c", "d"])` => `["a", "b:c", "d"]` +func rebuildColonSeparatedArgs(args []string) []string { + if len(args) == 0 { + return args + } + + result := []string{} + i := 0 + + for i < len(args) { + current := args[i] + + // Keep joining while the next element is ":" or the current element ends with ":" + for i+1 < len(args) && (args[i+1] == ":" || strings.HasSuffix(current, ":")) { + if args[i+1] == ":" { + current += ":" + i++ + // Check if there's a following element after the ":" + if i+1 < len(args) && args[i+1] != ":" { + current += args[i+1] + i++ + } + } else { + break + } + } + + result = append(result, current) + i++ + } + + return result +} diff --git a/internal/autocomplete/autocomplete_test.go b/internal/autocomplete/autocomplete_test.go new file mode 100644 index 0000000..2338924 --- /dev/null +++ b/internal/autocomplete/autocomplete_test.go @@ -0,0 +1,433 @@ +package autocomplete + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/urfave/cli/v3" +) + +func TestGetCompletions_EmptyArgs(t *testing.T) { + t.Parallel() + + root := &cli.Command{ + Commands: []*cli.Command{ + {Name: "generate", Usage: "Generate SDK"}, + {Name: "test", Usage: "Run tests"}, + {Name: "build", Usage: "Build project"}, + }, + } + + result := GetCompletions(CompletionStyleBash, root, []string{}) + + assert.Equal(t, ShellCompletionBehaviorDefault, result.Behavior) + assert.Len(t, result.Completions, 3) + assert.Contains(t, result.Completions, ShellCompletion{Name: "generate", Usage: "Generate SDK"}) + assert.Contains(t, result.Completions, ShellCompletion{Name: "test", Usage: "Run tests"}) + assert.Contains(t, result.Completions, ShellCompletion{Name: "build", Usage: "Build project"}) +} + +func TestGetCompletions_SubcommandPrefix(t *testing.T) { + t.Parallel() + + root := &cli.Command{ + Commands: []*cli.Command{ + {Name: "generate", Usage: "Generate SDK"}, + {Name: "test", Usage: "Run tests"}, + {Name: "build", Usage: "Build project"}, + }, + } + + result := GetCompletions(CompletionStyleBash, root, []string{"ge"}) + + assert.Equal(t, ShellCompletionBehaviorDefault, result.Behavior) + assert.Len(t, result.Completions, 1) + assert.Equal(t, "generate", result.Completions[0].Name) + assert.Equal(t, "Generate SDK", result.Completions[0].Usage) +} + +func TestGetCompletions_HiddenCommand(t *testing.T) { + t.Parallel() + + root := &cli.Command{ + Commands: []*cli.Command{ + {Name: "visible", Usage: "Visible command"}, + {Name: "hidden", Usage: "Hidden command", Hidden: true}, + }, + } + + result := GetCompletions(CompletionStyleBash, root, []string{""}) + + assert.Len(t, result.Completions, 1) + assert.Equal(t, "visible", result.Completions[0].Name) +} + +func TestGetCompletions_NestedSubcommand(t *testing.T) { + t.Parallel() + + root := &cli.Command{ + Commands: []*cli.Command{ + { + Name: "config", + Usage: "Configuration commands", + Commands: []*cli.Command{ + {Name: "get", Usage: "Get config value"}, + {Name: "set", Usage: "Set config value"}, + }, + }, + }, + } + + result := GetCompletions(CompletionStyleBash, root, []string{"config", "s"}) + + assert.Equal(t, ShellCompletionBehaviorDefault, result.Behavior) + assert.Len(t, result.Completions, 1) + assert.Equal(t, "set", result.Completions[0].Name) + assert.Equal(t, "Set config value", result.Completions[0].Usage) +} + +func TestGetCompletions_FlagCompletion(t *testing.T) { + t.Parallel() + + root := &cli.Command{ + Commands: []*cli.Command{ + { + Name: "generate", + Usage: "Generate SDK", + Flags: []cli.Flag{ + &cli.StringFlag{Name: "output", Aliases: []string{"o"}, Usage: "Output directory"}, + &cli.BoolFlag{Name: "verbose", Aliases: []string{"v"}, Usage: "Verbose output"}, + &cli.StringFlag{Name: "format", Usage: "Output format"}, + }, + }, + }, + } + + result := GetCompletions(CompletionStyleBash, root, []string{"generate", "--o"}) + + assert.Equal(t, ShellCompletionBehaviorDefault, result.Behavior) + assert.Len(t, result.Completions, 1) + assert.Equal(t, "--output", result.Completions[0].Name) + assert.Equal(t, "Output directory", result.Completions[0].Usage) +} + +func TestGetCompletions_ShortFlagCompletion(t *testing.T) { + t.Parallel() + + root := &cli.Command{ + Commands: []*cli.Command{ + { + Name: "generate", + Usage: "Generate SDK", + Flags: []cli.Flag{ + &cli.StringFlag{Name: "output", Aliases: []string{"o"}, Usage: "Output directory"}, + &cli.BoolFlag{Name: "verbose", Aliases: []string{"v"}, Usage: "Verbose output"}, + }, + }, + }, + } + + result := GetCompletions(CompletionStyleBash, root, []string{"generate", "-v"}) + + assert.Equal(t, ShellCompletionBehaviorDefault, result.Behavior) + assert.Len(t, result.Completions, 1) + assert.Equal(t, "-v", result.Completions[0].Name) +} + +func TestGetCompletions_FileFlagBehavior(t *testing.T) { + t.Parallel() + + root := &cli.Command{ + Commands: []*cli.Command{ + { + Name: "generate", + Usage: "Generate SDK", + Flags: []cli.Flag{ + &cli.StringFlag{Name: "config", Aliases: []string{"c"}, Usage: "Config file", TakesFile: true}, + }, + }, + }, + } + + result := GetCompletions(CompletionStyleBash, root, []string{"generate", "--config", ""}) + + assert.EqualValues(t, ShellCompletionBehaviorFile, result.Behavior) + assert.Empty(t, result.Completions) +} + +func TestGetCompletions_NonBoolFlagValue(t *testing.T) { + t.Parallel() + + root := &cli.Command{ + Commands: []*cli.Command{ + { + Name: "generate", + Usage: "Generate SDK", + Flags: []cli.Flag{ + &cli.StringFlag{Name: "format", Usage: "Output format"}, + }, + }, + }, + } + + result := GetCompletions(CompletionStyleBash, root, []string{"generate", "--format", ""}) + + assert.EqualValues(t, ShellCompletionBehaviorNoComplete, result.Behavior) + assert.Empty(t, result.Completions) +} + +func TestGetCompletions_BoolFlagDoesNotBlockCompletion(t *testing.T) { + t.Parallel() + + root := &cli.Command{ + Commands: []*cli.Command{ + { + Name: "generate", + Usage: "Generate SDK", + Flags: []cli.Flag{ + &cli.BoolFlag{Name: "verbose", Aliases: []string{"v"}, Usage: "Verbose output"}, + }, + Commands: []*cli.Command{ + {Name: "typescript", Usage: "Generate TypeScript SDK"}, + {Name: "python", Usage: "Generate Python SDK"}, + }, + }, + }, + } + + result := GetCompletions(CompletionStyleBash, root, []string{"generate", "--verbose", "ty"}) + + assert.Equal(t, ShellCompletionBehaviorDefault, result.Behavior) + assert.Len(t, result.Completions, 1) + assert.Equal(t, "typescript", result.Completions[0].Name) +} + +func TestGetCompletions_ColonCommands_NoColonTyped(t *testing.T) { + t.Parallel() + + root := &cli.Command{ + Commands: []*cli.Command{ + {Name: "config:get", Usage: "Get config value"}, + {Name: "config:set", Usage: "Set config value"}, + {Name: "config:list", Usage: "List config values"}, + }, + } + + result := GetCompletions(CompletionStyleBash, root, []string{"co"}) + + // Should collapse to single "config" entry without usage + assert.Len(t, result.Completions, 1) + assert.Equal(t, "config", result.Completions[0].Name) + assert.Equal(t, "", result.Completions[0].Usage) +} + +func TestGetCompletions_ColonCommands_ColonTyped_Bash(t *testing.T) { + t.Parallel() + + root := &cli.Command{ + Commands: []*cli.Command{ + {Name: "config:get", Usage: "Get config value"}, + {Name: "config:set", Usage: "Set config value"}, + {Name: "config:list", Usage: "List config values"}, + }, + } + + result := GetCompletions(CompletionStyleBash, root, []string{"config:"}) + + // For bash, should show suffixes only + assert.Len(t, result.Completions, 3) + names := []string{result.Completions[0].Name, result.Completions[1].Name, result.Completions[2].Name} + assert.Contains(t, names, "get") + assert.Contains(t, names, "set") + assert.Contains(t, names, "list") +} + +func TestGetCompletions_ColonCommands_ColonTyped_Zsh(t *testing.T) { + t.Parallel() + + root := &cli.Command{ + Commands: []*cli.Command{ + {Name: "config:get", Usage: "Get config value"}, + {Name: "config:set", Usage: "Set config value"}, + {Name: "config:list", Usage: "List config values"}, + }, + } + + result := GetCompletions(CompletionStyleZsh, root, []string{"config:"}) + + // For zsh, should show full names + assert.Len(t, result.Completions, 3) + names := []string{result.Completions[0].Name, result.Completions[1].Name, result.Completions[2].Name} + assert.Contains(t, names, "config:get") + assert.Contains(t, names, "config:set") + assert.Contains(t, names, "config:list") +} + +func TestGetCompletions_BashStyleColonCompletion(t *testing.T) { + t.Parallel() + + root := &cli.Command{ + Commands: []*cli.Command{ + {Name: "config:get", Usage: "Get config value"}, + {Name: "config:set", Usage: "Set config value"}, + }, + } + + result := GetCompletions(CompletionStyleBash, root, []string{"config:g"}) + + // For bash, should return suffix from after the colon in the input + // Input "config:g" has colon at index 6, so we take name[7:] from matched commands + assert.Len(t, result.Completions, 1) + assert.Equal(t, "get", result.Completions[0].Name) + assert.Equal(t, "Get config value", result.Completions[0].Usage) +} + +func TestGetCompletions_BashStyleColonCompletion_NoMatch(t *testing.T) { + t.Parallel() + + root := &cli.Command{ + Commands: []*cli.Command{ + {Name: "config:get", Usage: "Get config value"}, + {Name: "config:set", Usage: "Set config value"}, + }, + } + + result := GetCompletions(CompletionStyleBash, root, []string{"other:g"}) + + // No matches + assert.Len(t, result.Completions, 0) +} + +func TestGetCompletions_ZshStyleColonCompletion(t *testing.T) { + t.Parallel() + + root := &cli.Command{ + Commands: []*cli.Command{ + {Name: "config:get", Usage: "Get config value"}, + {Name: "config:set", Usage: "Set config value"}, + }, + } + + result := GetCompletions(CompletionStyleZsh, root, []string{"config:g"}) + + // For zsh, should return full name + assert.Len(t, result.Completions, 1) + assert.Equal(t, "config:get", result.Completions[0].Name) + assert.Equal(t, "Get config value", result.Completions[0].Usage) +} + +func TestGetCompletions_MixedColonAndRegularCommands(t *testing.T) { + t.Parallel() + + root := &cli.Command{ + Commands: []*cli.Command{ + {Name: "generate", Usage: "Generate SDK"}, + {Name: "config:get", Usage: "Get config value"}, + {Name: "config:set", Usage: "Set config value"}, + }, + } + + result := GetCompletions(CompletionStyleBash, root, []string{""}) + + // Should show "generate" and "config" (collapsed) + assert.Len(t, result.Completions, 2) + names := []string{result.Completions[0].Name, result.Completions[1].Name} + assert.Contains(t, names, "generate") + assert.Contains(t, names, "config") +} + +func TestGetCompletions_FlagWithBoolFlagSkipsValue(t *testing.T) { + t.Parallel() + + root := &cli.Command{ + Commands: []*cli.Command{ + { + Name: "generate", + Usage: "Generate SDK", + Flags: []cli.Flag{ + &cli.BoolFlag{Name: "verbose", Aliases: []string{"v"}}, + &cli.StringFlag{Name: "output", Aliases: []string{"o"}}, + }, + Commands: []*cli.Command{ + {Name: "typescript", Usage: "TypeScript SDK"}, + }, + }, + }, + } + + // Bool flag should not consume the next arg as a value + result := GetCompletions(CompletionStyleBash, root, []string{"generate", "-v", "ty"}) + + assert.Len(t, result.Completions, 1) + assert.Equal(t, "typescript", result.Completions[0].Name) +} + +func TestGetCompletions_MultipleFlagsBeforeSubcommand(t *testing.T) { + t.Parallel() + + root := &cli.Command{ + Commands: []*cli.Command{ + { + Name: "generate", + Usage: "Generate SDK", + Flags: []cli.Flag{ + &cli.StringFlag{Name: "config", Aliases: []string{"c"}}, + &cli.BoolFlag{Name: "verbose", Aliases: []string{"v"}}, + }, + Commands: []*cli.Command{ + {Name: "typescript", Usage: "TypeScript SDK"}, + {Name: "python", Usage: "Python SDK"}, + }, + }, + }, + } + + result := GetCompletions(CompletionStyleBash, root, []string{"generate", "-c", "config.yml", "-v", "py"}) + + assert.Len(t, result.Completions, 1) + assert.Equal(t, "python", result.Completions[0].Name) +} + +func TestGetCompletions_CommandAliases(t *testing.T) { + t.Parallel() + + root := &cli.Command{ + Commands: []*cli.Command{ + {Name: "generate", Aliases: []string{"gen", "g"}, Usage: "Generate SDK"}, + }, + } + + result := GetCompletions(CompletionStyleBash, root, []string{"g"}) + + // Should match all aliases that start with "g" + assert.GreaterOrEqual(t, len(result.Completions), 2) // "generate" and "gen", possibly "g" too + names := []string{} + for _, c := range result.Completions { + names = append(names, c.Name) + } + assert.Contains(t, names, "generate") + assert.Contains(t, names, "gen") +} + +func TestGetCompletions_AllFlagsWhenNoPrefix(t *testing.T) { + t.Parallel() + + root := &cli.Command{ + Commands: []*cli.Command{ + { + Name: "generate", + Usage: "Generate SDK", + Flags: []cli.Flag{ + &cli.StringFlag{Name: "output", Aliases: []string{"o"}}, + &cli.BoolFlag{Name: "verbose", Aliases: []string{"v"}}, + &cli.StringFlag{Name: "format", Aliases: []string{"f"}}, + }, + }, + }, + } + + result := GetCompletions(CompletionStyleBash, root, []string{"generate", "-"}) + + // Should show all flag variations + assert.GreaterOrEqual(t, len(result.Completions), 6) // -o, --output, -v, --verbose, -f, --format +} diff --git a/internal/autocomplete/shellscripts/bash_autocomplete.bash b/internal/autocomplete/shellscripts/bash_autocomplete.bash new file mode 100755 index 0000000..8fb7b0b --- /dev/null +++ b/internal/autocomplete/shellscripts/bash_autocomplete.bash @@ -0,0 +1,59 @@ +#!/bin/bash + +____APPNAME___bash_autocomplete() { + if [[ "${COMP_WORDS[0]}" != "source" ]]; then + local cur completions exit_code + local IFS=$'\n' + cur="${COMP_WORDS[COMP_CWORD]}" + + completions=$(COMPLETION_STYLE=bash "${COMP_WORDS[0]}" __complete -- "${COMP_WORDS[@]:1:$COMP_CWORD-1}" "$cur" 2>/dev/null) + exit_code=$? + + local last_token="$cur" + + # If the last token has been split apart by a ':', join it back together. + # Ex: 'a:b' will be represented in COMP_WORDS as 'a', ':', 'b' + if [[ $COMP_CWORD -ge 2 ]]; then + local prev2="${COMP_WORDS[COMP_CWORD - 2]}" + local prev1="${COMP_WORDS[COMP_CWORD - 1]}" + if [[ "$prev2" =~ ^@(file|data)$ && "$prev1" == ":" && "$cur" =~ ^// ]]; then + last_token="$prev2:$cur" + fi + fi + + # Check for custom file completion patterns + local prefix="" + local file_part="$cur" + local force_file_completion=false + if [[ "$last_token" =~ (.*)@(file://|data://)?(.*)$ ]]; then + local before_at="${BASH_REMATCH[1]}" + local protocol="${BASH_REMATCH[2]}" + file_part="${BASH_REMATCH[3]}" + + if [[ "$protocol" == "" ]]; then + prefix="$before_at@" + else + if [[ "$before_at" == "" ]]; then + prefix="//" + else + prefix="$before_at@$protocol" + fi + fi + + force_file_completion=true + fi + + if [[ "$force_file_completion" == true ]]; then + mapfile -t COMPREPLY < <(compgen -f -- "$file_part" | sed "s|^|$prefix|") + else + case $exit_code in + 10) mapfile -t COMPREPLY < <(compgen -f -- "$cur") ;; # file completion + 11) COMPREPLY=() ;; # no completion + 0) mapfile -t COMPREPLY <<<"$completions" ;; # use returned completions + esac + fi + return 0 + fi +} + +complete -F ____APPNAME___bash_autocomplete __APPNAME__ diff --git a/internal/autocomplete/shellscripts/fish_autocomplete.fish b/internal/autocomplete/shellscripts/fish_autocomplete.fish new file mode 100644 index 0000000..b853057 --- /dev/null +++ b/internal/autocomplete/shellscripts/fish_autocomplete.fish @@ -0,0 +1,51 @@ +#!/usr/bin/env fish + +function ____APPNAME___fish_autocomplete + set -l tokens (commandline -xpc) + set -l current (commandline -ct) + + set -l cmd $tokens[1] + set -l args $tokens[2..-1] + + set -l completions (env COMPLETION_STYLE=fish $cmd __complete -- $args $current 2>>/tmp/fish-debug.log) + set -l exit_code $status + + # Check for custom file completion patterns + # Patterns can appear anywhere in the word (e.g., inside quotes: 'my file is @file://path') + set -l prefix "" + set -l file_part "$current" + set -l force_file_completion 0 + + if string match -gqr '^(?.*)@(?file://|data://)?(?.*)$' -- $current + if string match -qr '^[\'"]' -- $before + # Ensures we don't insert an extra quote when the user is building an argument in quotes + set before (string sub -s 2 -- $before) + end + + set prefix "$before@$protocol" + set force_file_completion 1 + end + + if test $force_file_completion -eq 1 + for path in (__fish_complete_path "$file_part") + echo $prefix$path + end + else + switch $exit_code + case 10 + # File completion + __fish_complete_path "$current" + case 11 + # No completion + return 0 + case 0 + # Use returned completions + for completion in $completions + echo $completion + end + end + end +end + +complete -c __APPNAME__ -f -a '(____APPNAME___fish_autocomplete)' + diff --git a/internal/autocomplete/shellscripts/pwsh_autocomplete.ps1 b/internal/autocomplete/shellscripts/pwsh_autocomplete.ps1 new file mode 100644 index 0000000..7cd6e62 --- /dev/null +++ b/internal/autocomplete/shellscripts/pwsh_autocomplete.ps1 @@ -0,0 +1,97 @@ +Register-ArgumentCompleter -Native -CommandName __APPNAME__ -ScriptBlock { + param($wordToComplete, $commandAst, $cursorPosition) + + $elements = $commandAst.CommandElements + $completionArgs = @() + + # Extract each of the arguments + for ($i = 0; $i -lt $elements.Count; $i++) { + $completionArgs += $elements[$i].Extent.Text + } + + # Add empty string if there's a trailing space (wordToComplete is empty but cursor is after space) + # Necessary for differentiating between getting completions for namespaced commands vs. subcommands + if ($wordToComplete.Length -eq 0 -and $elements.Count -gt 0) { + $completionArgs += "" + } + + $output = & { + $env:COMPLETION_STYLE = 'pwsh' + __APPNAME__ __complete @completionArgs 2>&1 + } + $exitCode = $LASTEXITCODE + + # Check for custom file completion patterns + # Patterns can appear anywhere in the word (e.g., inside quotes: 'my file is @file://path') + $prefix = "" + $filePart = $wordToComplete + $forceFileCompletion = $false + + # PowerShell includes quotes in $wordToComplete - strip them for pattern matching + # but preserve them in the prefix for the completion result + $wordContent = $wordToComplete + $leadingQuote = "" + if ($wordToComplete -match '^([''"])(.*)(\1)$') { + # Fully quoted: "content" or 'content' + $leadingQuote = $Matches[1] + $wordContent = $Matches[2] + } elseif ($wordToComplete -match '^([''"])(.*)$') { + # Opening quote only: "content or 'content + $leadingQuote = $Matches[1] + $wordContent = $Matches[2] + } + + if ($wordContent -match '^(.*)@(file://|data://)?(.*)$') { + $prefix = $leadingQuote + $Matches[1] + '@' + $Matches[2] + $filePart = $Matches[3] + $forceFileCompletion = $true + } + + if ($forceFileCompletion) { + # Handle empty filePart (e.g., "@" or "@file://") by listing current directory + $items = if ([string]::IsNullOrEmpty($filePart)) { + Get-ChildItem -ErrorAction SilentlyContinue + } else { + Get-ChildItem -Path "$filePart*" -ErrorAction SilentlyContinue + } + $items | ForEach-Object { + $completionText = if ($_.PSIsContainer) { $prefix + $_.Name + "/" } else { $prefix + $_.Name } + [System.Management.Automation.CompletionResult]::new( + $completionText, + $completionText, + 'ProviderItem', + $completionText + ) + } + } else { + switch ($exitCode) { + 10 { + # File completion behavior + $items = if ([string]::IsNullOrEmpty($wordToComplete)) { + Get-ChildItem -ErrorAction SilentlyContinue + } else { + Get-ChildItem -Path "$wordToComplete*" -ErrorAction SilentlyContinue + } + $items | ForEach-Object { + $completionText = if ($_.PSIsContainer) { $_.Name + "/" } else { $_.Name } + [System.Management.Automation.CompletionResult]::new( + $completionText, + $completionText, + 'ProviderItem', + $completionText + ) + } + } + 11 { + # No reasonable suggestions + [System.Management.Automation.CompletionResult]::new(' ', ' ', 'ParameterValue', ' ') + } + default { + # Default behavior - show command completions + $output | ForEach-Object { + [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_) + } + } + } + } +} diff --git a/internal/autocomplete/shellscripts/zsh_autocomplete.zsh b/internal/autocomplete/shellscripts/zsh_autocomplete.zsh new file mode 100644 index 0000000..d937171 --- /dev/null +++ b/internal/autocomplete/shellscripts/zsh_autocomplete.zsh @@ -0,0 +1,56 @@ +#compdef __APPNAME__ + +____APPNAME___zsh_autocomplete() { + + local -a opts + local temp + local exit_code + + temp=$(COMPLETION_STYLE=zsh "${words[1]}" __complete "${words[@]:1}") + exit_code=$? + + # Check for custom file completion patterns + # Patterns can appear anywhere in the word (e.g., inside quotes: 'my file is @file://path') + local cur="${words[CURRENT]}" + + if [[ "$cur" = *'@'* ]]; then + # Extract everything after the last @ + local after_last_at="${cur##*@}" + + if [[ $after_last_at =~ ^(file://|data://) ]]; then + compset -P "*$MATCH" + _files + else + compset -P '*@' + _files + fi + return + fi + + case $exit_code in + 10) + # File completion behavior + _files + ;; + 11) + # No completion behavior - return nothing + return 1 + ;; + 0) + # Default behavior - show command completions + opts=("${(@f)temp}") + _describe 'values' opts + ;; + esac +} + +# When installed in fpath (e.g., via Homebrew's zsh_completion stanza), this file +# is autoloaded as the function ___APPNAME__ and its body becomes that function's +# body. Detect that case via funcstack and dispatch to the completion function. +# When sourced (e.g., `source <(__APPNAME__ @completion zsh)`), register the +# function with compdef instead. +if [[ "${funcstack[1]}" = "___APPNAME__" ]]; then + ____APPNAME___zsh_autocomplete "$@" +else + compdef ____APPNAME___zsh_autocomplete __APPNAME__ +fi diff --git a/internal/binaryparam/binary_param.go b/internal/binaryparam/binary_param.go new file mode 100644 index 0000000..40d4ecf --- /dev/null +++ b/internal/binaryparam/binary_param.go @@ -0,0 +1,30 @@ +package binaryparam + +import ( + "io" + "os" +) + +const stdinGlyph = "-" + +// FileOrStdin opens the file at the given path for reading. If the path is "-", stdin is returned instead. +// +// It's the caller's responsibility to close the returned ReadCloser (usually with `defer`). +// +// Returns a boolean indicating whether stdin is being used. If true, no other components of the calling +// program should attempt to read from stdin for anything else. +func FileOrStdin(stdin io.ReadCloser, path string) (io.ReadCloser, bool, error) { + // When the special glyph "-" is used, read from stdin. Although probably less necessary, also support + // special Unix files that refer to stdin. + switch path { + case "", stdinGlyph, "/dev/fd/0", "/dev/stdin": + return stdin, true, nil + } + + readCloser, err := os.Open(path) + if err != nil { + return nil, false, err + } + + return readCloser, false, err +} diff --git a/internal/binaryparam/binary_param_test.go b/internal/binaryparam/binary_param_test.go new file mode 100644 index 0000000..7a66682 --- /dev/null +++ b/internal/binaryparam/binary_param_test.go @@ -0,0 +1,59 @@ +package binaryparam + +import ( + "io" + "os" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestFileOrStdin(t *testing.T) { + t.Parallel() + + const expectedContents = "test file contents" + + t.Run("WithFile", func(t *testing.T) { + tempFile := t.TempDir() + "/test_file.txt" + require.NoError(t, os.WriteFile(tempFile, []byte(expectedContents), 0600)) + + readCloser, stdinInUse, err := FileOrStdin(os.Stdin, tempFile) + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, readCloser.Close()) }) + + actualContents, err := io.ReadAll(readCloser) + require.NoError(t, err) + require.Equal(t, expectedContents, string(actualContents)) + + require.False(t, stdinInUse) + }) + + stdinTests := []struct { + testName string + path string + }{ + {"TestEmptyString", ""}, + {"TestDash", "-"}, + {"TestDevStdin", "/dev/stdin"}, + {"TestDevFD0", "/dev/fd/0"}, + } + for _, test := range stdinTests { + t.Run(test.testName, func(t *testing.T) { + tempFile := t.TempDir() + "/test_file.txt" + require.NoError(t, os.WriteFile(tempFile, []byte(expectedContents), 0600)) + + stubStdin, err := os.Open(tempFile) + require.NoError(t, err) + t.Cleanup(func() { require.NoError(t, stubStdin.Close()) }) + + readCloser, stdinInUse, err := FileOrStdin(stubStdin, test.path) + require.NoError(t, err) + + actualContents, err := io.ReadAll(readCloser) + require.NoError(t, err) + require.Equal(t, expectedContents, string(actualContents)) + + require.True(t, stdinInUse) + }) + } +} diff --git a/internal/debugmiddleware/debug_middleware.go b/internal/debugmiddleware/debug_middleware.go new file mode 100644 index 0000000..647f1de --- /dev/null +++ b/internal/debugmiddleware/debug_middleware.go @@ -0,0 +1,132 @@ +package debugmiddleware + +import ( + "bytes" + "io" + "log" + "net/http" + "net/http/httputil" + "reflect" + "strings" +) + +// For the time being these type definitions are duplicated here so that we can +// test this file in a non-generated context. +type ( + Middleware = func(*http.Request, MiddlewareNext) (*http.Response, error) + MiddlewareNext = func(*http.Request) (*http.Response, error) +) + +const redactedPlaceholder = "" + +// Headers known to contain sensitive information like an API key. Note that this exclude `Authorization`, +// which is handled specially in `redactRequest` below. +var sensitiveHeaders = []string{ + "api-key", + "x-api-key", + "cookie", + "set-cookie", +} + +// RequestLogger is a middleware that logs HTTP requests and responses. +type RequestLogger struct { + logger interface{ Printf(string, ...any) } // field for testability; usually log.Default() + sensitiveHeaders []string // field for testability; usually sensitiveHeaders +} + +// NewRequestLogger returns a new RequestLogger instance with default options. +func NewRequestLogger() *RequestLogger { + return &RequestLogger{ + logger: log.Default(), + sensitiveHeaders: sensitiveHeaders, + } +} + +func (m *RequestLogger) Middleware() Middleware { + return func(req *http.Request, mn MiddlewareNext) (*http.Response, error) { + redacted, err := m.redactRequest(req) + if err != nil { + return nil, err + } + if reqBytes, err := httputil.DumpRequest(redacted, true); err == nil { + m.logger.Printf("Request Content:\n%s\n", reqBytes) + } + + resp, err := mn(req) + if err != nil { + return resp, err + } + + if respBytes, err := httputil.DumpResponse(resp, true); err == nil { + m.logger.Printf("Response Content:\n%s\n", respBytes) + } + + return resp, err + } +} + +// redactRequest redacts sensitive information from the request for logging +// purposes. If redaction is necessary, the request is cloned before mutating +// the original and that clone is returned. As a small optimization, the +// original is request is returned unchanged if no redaction is necessary. +func (m *RequestLogger) redactRequest(req *http.Request) (*http.Request, error) { + redactedHeaders := req.Header.Clone() + + // Notably, the clauses below are written so they can redact multiple + // headers of the same name if necessary. + if values := redactedHeaders.Values("Authorization"); len(values) > 0 { + redactedHeaders.Del("Authorization") + + for _, value := range values { + // In case we're using something like a bearer token (e.g. `Bearer + // `), keep the `Bearer` part for more debugging + // information. + if authKind, _, ok := strings.Cut(value, " "); ok { + redactedHeaders.Add("Authorization", authKind+" "+redactedPlaceholder) + } else { + redactedHeaders.Add("Authorization", redactedPlaceholder) + } + } + } + + for _, header := range m.sensitiveHeaders { + values := redactedHeaders.Values(header) + if len(values) == 0 { + continue + } + + redactedHeaders.Del(header) + + for range values { + redactedHeaders.Add(header, redactedPlaceholder) + } + } + + if reflect.DeepEqual(req.Header, redactedHeaders) { + return req, nil + } + + redacted := req.Clone(req.Context()) + redacted.Header = redactedHeaders + var err error + redacted.Body, req.Body, err = cloneBody(req.Body) + return redacted, err +} + +// This function returns two copies of an HTTP request body that can each be +// read independently without affecting the other. +// This logic is taken from `drainBody` in net/http/httputil. +func cloneBody(b io.ReadCloser) (r1, r2 io.ReadCloser, err error) { + if b == nil || b == http.NoBody { + // No copying needed. Preserve the magic sentinel meaning of NoBody. + return http.NoBody, http.NoBody, nil + } + var buf bytes.Buffer + if _, err = buf.ReadFrom(b); err != nil { + return nil, b, err + } + if err = b.Close(); err != nil { + return nil, b, err + } + return io.NopCloser(&buf), io.NopCloser(bytes.NewReader(buf.Bytes())), nil +} diff --git a/internal/debugmiddleware/debug_middleware_test.go b/internal/debugmiddleware/debug_middleware_test.go new file mode 100644 index 0000000..4e46fbc --- /dev/null +++ b/internal/debugmiddleware/debug_middleware_test.go @@ -0,0 +1,201 @@ +package debugmiddleware + +import ( + "bytes" + "io" + "log" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestDebugMiddleware(t *testing.T) { + t.Parallel() + + setup := func() (*RequestLogger, *bytes.Buffer) { + var ( + logBuf bytes.Buffer + middleware = NewRequestLogger() + ) + middleware.logger = log.New(&logBuf, "", 0) + return middleware, &logBuf + } + + t.Run("DoesNotRedactMostHeaders", func(t *testing.T) { + t.Parallel() + + middleware, logBuf := setup() + + const stainlessUserAgent = "Stainless" + + req := httptest.NewRequest("GET", "https://example.com", nil) + req.Header.Set("User-Agent", stainlessUserAgent) + + var nextMiddlewareRan bool + middleware.Middleware()(req, func(req *http.Request) (*http.Response, error) { + nextMiddlewareRan = true + + // The request sent down through middleware shouldn't be mutated. + require.Equal(t, stainlessUserAgent, req.Header.Get("User-Agent")) + + return &http.Response{}, nil + }) + + require.True(t, nextMiddlewareRan) + require.Contains(t, logBuf.String(), "User-Agent: "+stainlessUserAgent) + }) + + const secretToken = "secret-token" + + t.Run("RedactsAuthorizationHeader", func(t *testing.T) { + t.Parallel() + + middleware, logBuf := setup() + + req := httptest.NewRequest("GET", "https://example.com", nil) + req.Header.Set("Authorization", secretToken) + + var nextMiddlewareRan bool + middleware.Middleware()(req, func(req *http.Request) (*http.Response, error) { + nextMiddlewareRan = true + + // The request sent down through middleware shouldn't be mutated. + require.Equal(t, secretToken, req.Header.Get("Authorization")) + + return &http.Response{}, nil + }) + + require.True(t, nextMiddlewareRan) + require.Contains(t, logBuf.String(), "Authorization: "+redactedPlaceholder) + }) + + t.Run("RedactsOnlySecretInAuthorizationHeader", func(t *testing.T) { + t.Parallel() + + middleware, logBuf := setup() + + req := httptest.NewRequest("GET", "https://example.com", nil) + req.Header.Set("Authorization", "Bearer "+secretToken) + + var nextMiddlewareRan bool + middleware.Middleware()(req, func(req *http.Request) (*http.Response, error) { + nextMiddlewareRan = true + + return &http.Response{}, nil + }) + + require.True(t, nextMiddlewareRan) + require.Contains(t, logBuf.String(), "Authorization: Bearer "+redactedPlaceholder) + }) + + t.Run("RedactsMultipleAuthorizationHeaders", func(t *testing.T) { + t.Parallel() + + middleware, logBuf := setup() + + req := httptest.NewRequest("GET", "https://example.com", nil) + req.Header.Add("Authorization", secretToken+"1") + req.Header.Add("Authorization", secretToken+"2") + + var nextMiddlewareRan bool + middleware.Middleware()(req, func(req *http.Request) (*http.Response, error) { + nextMiddlewareRan = true + + // The request sent down through middleware shouldn't be mutated. + require.Equal(t, []string{secretToken + "1", secretToken + "2"}, req.Header.Values("Authorization")) + + return &http.Response{}, nil + }) + + require.True(t, nextMiddlewareRan) + + if strings.Count(logBuf.String(), "Authorization: "+redactedPlaceholder) != 2 { + t.Error("expected exactly two redacted placeholders in authorization headers") + } + }) + + const customAPIKeyHeader = "X-My-Api-Key" + + t.Run("RedactsSensitiveHeaders", func(t *testing.T) { + t.Parallel() + + middleware, logBuf := setup() + + middleware.sensitiveHeaders = []string{customAPIKeyHeader} + + req := httptest.NewRequest("GET", "https://example.com", nil) + req.Header.Set(customAPIKeyHeader, secretToken) + + var nextMiddlewareRan bool + middleware.Middleware()(req, func(req *http.Request) (*http.Response, error) { + nextMiddlewareRan = true + + // The request sent down through middleware shouldn't be mutated. + require.Equal(t, secretToken, req.Header.Get(customAPIKeyHeader)) + + return &http.Response{}, nil + }) + + require.True(t, nextMiddlewareRan) + require.Contains(t, logBuf.String(), customAPIKeyHeader+": "+redactedPlaceholder) + }) + + t.Run("RedactsMultipleSensitiveHeaders", func(t *testing.T) { + t.Parallel() + + middleware, logBuf := setup() + + middleware.sensitiveHeaders = []string{customAPIKeyHeader} + + req := httptest.NewRequest("GET", "https://example.com", nil) + req.Header.Add(customAPIKeyHeader, secretToken+"1") + req.Header.Add(customAPIKeyHeader, secretToken+"2") + + var nextMiddlewareRan bool + middleware.Middleware()(req, func(req *http.Request) (*http.Response, error) { + nextMiddlewareRan = true + + // The request sent down through middleware shouldn't be mutated. + require.Equal(t, []string{secretToken + "1", secretToken + "2"}, req.Header.Values(customAPIKeyHeader)) + + return &http.Response{}, nil + }) + + require.True(t, nextMiddlewareRan) + require.Equal(t, 2, strings.Count(logBuf.String(), customAPIKeyHeader+": "+redactedPlaceholder)) + }) + + t.Run("DoesNotConsumeRequestBodyWhenIoReader", func(t *testing.T) { + t.Parallel() + + middleware, logBuf := setup() + middleware.sensitiveHeaders = []string{customAPIKeyHeader} + + const bodyContent = "test request body content" + bodyReader := strings.NewReader(bodyContent) + + req := httptest.NewRequest("POST", "https://example.com", bodyReader) + req.Header.Set("Authorization", secretToken) + + var nextMiddlewareRan bool + middleware.Middleware()(req, func(req *http.Request) (*http.Response, error) { + nextMiddlewareRan = true + + // The request body should still be fully readable after the middleware runs + body, err := io.ReadAll(req.Body) + require.NoError(t, err) + require.Equal(t, bodyContent, string(body)) + + // The request sent down through middleware shouldn't be mutated. + require.Equal(t, secretToken, req.Header.Get("Authorization")) + + return &http.Response{}, nil + }) + + require.True(t, nextMiddlewareRan) + require.Contains(t, logBuf.String(), "Authorization: "+redactedPlaceholder) + }) +} diff --git a/internal/jsonview/explorer.go b/internal/jsonview/explorer.go new file mode 100644 index 0000000..836bb2c --- /dev/null +++ b/internal/jsonview/explorer.go @@ -0,0 +1,807 @@ +package jsonview + +import ( + "bytes" + "encoding/json" + "errors" + "fmt" + "math" + "os" + "strings" + + "github.com/charmbracelet/bubbles/help" + "github.com/charmbracelet/bubbles/key" + "github.com/charmbracelet/bubbles/table" + "github.com/charmbracelet/bubbles/viewport" + tea "github.com/charmbracelet/bubbletea" + "github.com/charmbracelet/lipgloss" + "github.com/charmbracelet/x/term" + "github.com/muesli/reflow/truncate" + "github.com/muesli/reflow/wordwrap" + "github.com/tidwall/gjson" +) + +const ( + // UI layout constants + borderPadding = 2 + heightOffset = 5 + tableMinHeight = 2 + titlePaddingLeft = 2 + titlePaddingTop = 0 + footerPaddingLeft = 1 + + // Column width constants + defaultColumnWidth = 10 + keyColumnWidth = 3 + valueColumnWidth = 5 + + // String formatting constants + maxStringLength = 100 + maxPreviewLength = 24 + + arrayColor = lipgloss.Color("1") + stringColor = lipgloss.Color("5") + objectColor = lipgloss.Color("4") +) + +type keyMap struct { + Up key.Binding + Down key.Binding + Enter key.Binding + Back key.Binding + PrintValue key.Binding + Raw key.Binding + Quit key.Binding +} + +func (k keyMap) ShortHelp() []key.Binding { + return []key.Binding{k.Quit, k.Up, k.Down, k.Back, k.Enter, k.PrintValue, k.Raw} +} + +func (k keyMap) FullHelp() [][]key.Binding { + return [][]key.Binding{k.ShortHelp()} +} + +var keys = keyMap{ + Up: key.NewBinding( + key.WithKeys("up", "k"), + key.WithHelp("↑/k", "up"), + ), + Down: key.NewBinding( + key.WithKeys("down", "j"), + key.WithHelp("↓/j", "down"), + ), + Back: key.NewBinding( + key.WithKeys("left", "h", "backspace"), + key.WithHelp("←/h", "go back"), + ), + Enter: key.NewBinding( + key.WithKeys("right", "l"), + key.WithHelp("→/l", "expand"), + ), + PrintValue: key.NewBinding( + key.WithKeys("p"), + key.WithHelp("p", "print and exit"), + ), + Raw: key.NewBinding( + key.WithKeys("r"), + key.WithHelp("r", "toggle raw JSON"), + ), + Quit: key.NewBinding( + key.WithKeys("q", "esc", "ctrl+c", "enter"), + key.WithHelp("q/enter", "quit"), + ), +} + +var ( + titleStyle = lipgloss.NewStyle().Bold(true).PaddingLeft(titlePaddingLeft).PaddingTop(titlePaddingTop) + arrayStyle = lipgloss.NewStyle().BorderStyle(lipgloss.RoundedBorder()).BorderForeground(arrayColor) + stringStyle = lipgloss.NewStyle().BorderStyle(lipgloss.RoundedBorder()).BorderForeground(stringColor) + objectStyle = lipgloss.NewStyle().BorderStyle(lipgloss.RoundedBorder()).BorderForeground(objectColor) + stringLiteralStyle = lipgloss.NewStyle().Bold(true).Foreground(lipgloss.Color("2")) +) + +type JSONView interface { + GetPath() string + GetData() gjson.Result + Update(tea.Msg, bool) tea.Cmd + View() string + Resize(width, height int) +} + +type TableView struct { + width int + height int + path string + data gjson.Result + table table.Model + rowData []gjson.Result + iterator AnyIterator + isLoading bool + columns []table.Column +} + +func (tv *TableView) GetPath() string { return tv.path } +func (tv *TableView) GetData() gjson.Result { return tv.data } +func (tv *TableView) View() string { return tv.table.View() } + +func (tv *TableView) Update(msg tea.Msg, raw bool) tea.Cmd { + var cmd tea.Cmd + tv.table, cmd = tv.table.Update(msg) + + // Check if we need to load more data + if tv.iterator != nil && !tv.isLoading && tv.data.IsArray() { + cursor := tv.table.Cursor() + totalRows := len(tv.table.Rows()) + + // Load more when we're at the last row + if cursor == totalRows-1 { + tv.isLoading = true + return tv.loadMoreData(raw) + } + } + + return cmd +} + +func (tv *TableView) loadMoreData(raw bool) tea.Cmd { + return func() tea.Msg { + if tv.iterator == nil { + return nil + } + + if !tv.iterator.Next() { + tv.isLoading = false + return tv.iterator.Err() + } + + obj := tv.iterator.Current() + var result gjson.Result + if jsonBytes, err := json.Marshal(obj); err != nil { + return err + } else { + result = gjson.ParseBytes(jsonBytes) + } + + if !result.Exists() { + tv.isLoading = false + return nil + } + + // Add the new item to our data + tv.rowData = append(tv.rowData, result) + + // Add new row to the table + newRow := table.Row{formatValue(result, raw)} + + // For array of objects, we need to format according to columns + if len(tv.columns) > 1 && result.IsObject() { + newRow = make(table.Row, len(tv.columns)) + for i, col := range tv.columns { + newRow[i] = formatValue(result.Get(col.Title), raw) + } + } + + rows := tv.table.Rows() + rows = append(rows, newRow) + tv.table.SetRows(rows) + + // Resize columns to accommodate the new data + tv.Resize(tv.width, tv.height) + + tv.isLoading = false + return nil + } +} + +func (tv *TableView) Resize(width, height int) { + tv.width = width + tv.height = height + tv.updateColumnWidths(width) + tv.table.SetHeight(min(height-heightOffset, tableMinHeight+len(tv.table.Rows()))) +} + +func (tv *TableView) updateColumnWidths(width int) { + columns := tv.table.Columns() + widths := make([]int, len(columns)) + + // Calculate required widths from headers and content + for i, col := range columns { + widths[i] = lipgloss.Width(col.Title) + } + + for _, row := range tv.table.Rows() { + for i, cell := range row { + if i < len(widths) { + widths[i] = max(widths[i], lipgloss.Width(cell)) + } + } + } + + totalWidth := sum(widths) + available := width - borderPadding*len(columns) + + if totalWidth <= available { + for i, w := range widths { + columns[i].Width = w + } + return + } + + fairShare := float64(available) / float64(len(columns)) + shrinkable := 0.0 + + for _, w := range widths { + if float64(w) > fairShare { + shrinkable += float64(w) - fairShare + } + } + + if shrinkable > 0 { + excess := float64(totalWidth - available) + for i, w := range widths { + if float64(w) > fairShare { + reduction := (float64(w) - fairShare) * (excess / shrinkable) + widths[i] = int(math.Round(float64(w) - reduction)) + } + } + } + + for i, w := range widths { + columns[i].Width = w + } + + tv.table.SetColumns(columns) +} + +type TextView struct { + path string + data gjson.Result + viewport viewport.Model + ready bool +} + +func (tv *TextView) GetPath() string { return tv.path } +func (tv *TextView) GetData() gjson.Result { return tv.data } +func (tv *TextView) View() string { return tv.viewport.View() } + +func (tv *TextView) Update(msg tea.Msg, raw bool) tea.Cmd { + var cmd tea.Cmd + tv.viewport, cmd = tv.viewport.Update(msg) + return cmd +} + +func (tv *TextView) Resize(width, height int) { + h := height - heightOffset + if !tv.ready { + tv.viewport = viewport.New(width, h) + tv.viewport.SetContent(wordwrap.String(tv.data.String(), width)) + tv.ready = true + return + } + tv.viewport.Width = width + tv.viewport.Height = h +} + +type JSONViewer struct { + stack []JSONView + root string + width int + height int + rawMode bool + message string + help help.Model +} + +// ExploreJSON explores a single JSON value known ahead of time +func ExploreJSON(title string, json gjson.Result) error { + view, err := newView("", json, false) + if err != nil { + return err + } + + viewer := &JSONViewer{stack: []JSONView{view}, root: title, rawMode: false, help: help.New()} + + _, err = tea.NewProgram(viewer).Run() + if viewer.message != "" { + _, msgErr := fmt.Println("\n" + viewer.message) + err = errors.Join(err, msgErr) + } + return err +} + +type hasRawJSON interface { + RawJSON() string +} + +// ExploreJSONStream explores JSON data loaded incrementally via an iterator +func ExploreJSONStream[T any](title string, it Iterator[T]) error { + anyIt := genericToAnyIterator(it) + + preloadCount := 20 + if termHeight, _, err := term.GetSize(os.Stdout.Fd()); err == nil { + preloadCount = termHeight + } + + items := make([]any, 0, preloadCount) + for i := 0; i < preloadCount && anyIt.Next(); i++ { + items = append(items, anyIt.Current()) + } + + if err := anyIt.Err(); err != nil { + return err + } + + arrayJSONBytes, err := marshalItemsToJSONArray(items) + if err != nil { + return err + } + + arrayJSON := gjson.ParseBytes(arrayJSONBytes) + view, err := newTableView("", arrayJSON, false) + if err != nil { + return err + } + + // Set iterator if there might be more data + if len(items) == preloadCount { + view.iterator = anyIt + } + + viewer := &JSONViewer{stack: []JSONView{view}, root: title, rawMode: false, help: help.New()} + _, err = tea.NewProgram(viewer).Run() + if viewer.message != "" { + _, msgErr := fmt.Println("\n" + viewer.message) + err = errors.Join(err, msgErr) + } + return err +} + +func marshalItemsToJSONArray(items []any) ([]byte, error) { + var buf bytes.Buffer + buf.WriteByte('[') + + for i, item := range items { + if i > 0 { + buf.WriteByte(',') + } + if hasRaw, ok := item.(hasRawJSON); ok { + buf.WriteString(hasRaw.RawJSON()) + } else { + jsonData, err := json.Marshal(item) + if err != nil { + return nil, err + } + buf.Write(jsonData) + } + } + + buf.WriteByte(']') + return buf.Bytes(), nil +} + +func (v *JSONViewer) current() JSONView { return v.stack[len(v.stack)-1] } +func (v *JSONViewer) Init() tea.Cmd { return nil } + +func (v *JSONViewer) resize(width, height int) { + v.width, v.height = width, height + v.help.Width = width + for i := range v.stack { + v.stack[i].Resize(width, height) + } +} + +func (v *JSONViewer) Update(msg tea.Msg) (tea.Model, tea.Cmd) { + switch msg := msg.(type) { + case tea.WindowSizeMsg: + v.resize(msg.Width-borderPadding, msg.Height) + return v, nil + case tea.KeyMsg: + switch { + case key.Matches(msg, keys.Quit): + return v, tea.Quit + case key.Matches(msg, keys.Enter): + return v.navigateForward() + case key.Matches(msg, keys.Back): + return v.navigateBack() + case key.Matches(msg, keys.Raw): + return v.toggleRaw() + case key.Matches(msg, keys.PrintValue): + v.message = v.getSelectedContent() + return v, tea.Quit + } + } + + return v, v.current().Update(msg, v.rawMode) +} + +func (v *JSONViewer) getSelectedContent() string { + tableView, ok := v.current().(*TableView) + if !ok { + return v.current().GetData().Raw + } + + selected := tableView.rowData[tableView.table.Cursor()] + if selected.Type == gjson.String { + return selected.String() + } + return selected.Raw +} + +func (v *JSONViewer) navigateForward() (tea.Model, tea.Cmd) { + tableView, ok := v.current().(*TableView) + if !ok { + return v, nil + } + + if len(tableView.rowData) < 1 { + return v, nil + } + + cursor := tableView.table.Cursor() + selected := tableView.rowData[cursor] + if !v.canNavigateInto(selected) { + return v, nil + } + + path := v.buildNavigationPath(tableView, cursor) + forwardView, err := newView(path, selected, v.rawMode) + if err != nil { + return v, nil + } + + v.stack = append(v.stack, forwardView) + v.resize(v.width, v.height) + return v, nil +} + +func (v *JSONViewer) buildNavigationPath(tableView *TableView, cursor int) string { + if tableView.data.IsArray() { + return fmt.Sprintf("%s[%d]", tableView.path, cursor) + } + key := tableView.data.Get("@keys").Array()[cursor].Str + return fmt.Sprintf("%s[%s]", tableView.path, quoteString(key)) +} + +func quoteString(s string) string { + // Replace backslashes and quotes with escaped versions + s = strings.ReplaceAll(s, "\\", "\\\\") + s = strings.ReplaceAll(s, "\"", "\\\"") + return stringLiteralStyle.Render("\"" + s + "\"") +} + +func (v *JSONViewer) canNavigateInto(data gjson.Result) bool { + switch { + case data.IsArray(): + return len(data.Array()) > 0 + case data.IsObject(): + return len(data.Map()) > 0 + case data.Type == gjson.String: + str := data.String() + return strings.Contains(str, "\n") || lipgloss.Width(str) >= maxStringLength + } + return false +} + +func (v *JSONViewer) navigateBack() (tea.Model, tea.Cmd) { + if len(v.stack) > 1 { + v.stack = v.stack[:len(v.stack)-1] + } + return v, nil +} + +func (v *JSONViewer) toggleRaw() (tea.Model, tea.Cmd) { + v.rawMode = !v.rawMode + + for i, view := range v.stack { + viewWithRaw, err := newView(view.GetPath(), view.GetData(), v.rawMode) + if err != nil { + return v, tea.Printf("Error: %s", err) + } + if newTV, ok := viewWithRaw.(*TableView); ok { + if tv, ok := view.(*TableView); ok && tv.iterator != nil { + newTV.iterator = tv.iterator + } + } + v.stack[i] = viewWithRaw + } + + v.resize(v.width, v.height) + return v, nil +} + +func (v *JSONViewer) View() string { + view := v.current() + title := v.buildTitle(view) + content := titleStyle.Render(title) + style := v.getStyleForData(view.GetData()) + content += "\n" + style.Render(view.View()) + content += "\n" + v.help.View(keys) + return content +} + +func (v *JSONViewer) buildTitle(view JSONView) string { + title := v.root + if len(view.GetPath()) > 0 { + title += " → " + view.GetPath() + } + if v.rawMode { + title += " (JSON)" + } + return title +} + +func (v *JSONViewer) getStyleForData(data gjson.Result) lipgloss.Style { + switch { + case data.Type == gjson.String: + return stringStyle + case data.IsArray(): + return arrayStyle + default: + return objectStyle + } +} + +func newView(path string, data gjson.Result, raw bool) (JSONView, error) { + if data.Type == gjson.String { + return newTextView(path, data) + } + return newTableView(path, data, raw) +} + +func newTextView(path string, data gjson.Result) (*TextView, error) { + if !data.Exists() || data.Type != gjson.String { + return nil, fmt.Errorf("invalid text JSON") + } + return &TextView{path: path, data: data}, nil +} + +func newTableView(path string, data gjson.Result, raw bool) (*TableView, error) { + if !data.Exists() || data.Type != gjson.JSON { + return nil, fmt.Errorf("invalid table JSON") + } + + switch { + case data.IsArray(): + array := data.Array() + if isArrayOfObjects(array) { + return newArrayOfObjectsTableView(path, data, array, raw), nil + } else { + return newArrayTableView(path, data, array, raw), nil + } + case data.IsObject(): + return newObjectTableView(path, data, raw), nil + default: + return nil, fmt.Errorf("unsupported JSON type") + } +} + +func newArrayTableView(path string, data gjson.Result, array []gjson.Result, raw bool) *TableView { + columns := []table.Column{{Title: "Items", Width: defaultColumnWidth}} + rows := make([]table.Row, 0, len(array)) + rowData := make([]gjson.Result, 0, len(array)) + + for _, item := range array { + rows = append(rows, table.Row{formatValue(item, raw)}) + rowData = append(rowData, item) + } + + t := createTable(columns, rows, arrayColor) + return &TableView{ + path: path, + data: data, + table: t, + rowData: rowData, + columns: columns, + } +} + +func newArrayOfObjectsTableView(path string, data gjson.Result, array []gjson.Result, raw bool) *TableView { + // Collect unique keys + keySet := make(map[string]struct{}) + var columns []table.Column + + for _, item := range array { + for _, key := range item.Get("@keys").Array() { + if _, exists := keySet[key.Str]; !exists { + keySet[key.Str] = struct{}{} + title := key.Str + columns = append(columns, table.Column{Title: title, Width: defaultColumnWidth}) + } + } + } + + rows := make([]table.Row, 0, len(array)) + rowData := make([]gjson.Result, 0, len(array)) + + for _, item := range array { + row := make(table.Row, len(columns)) + for i, col := range columns { + row[i] = formatValue(item.Get(col.Title), raw) + } + rows = append(rows, row) + rowData = append(rowData, item) + } + + t := createTable(columns, rows, arrayColor) + return &TableView{ + path: path, + data: data, + table: t, + rowData: rowData, + columns: columns, + } +} + +func newObjectTableView(path string, data gjson.Result, raw bool) *TableView { + columns := []table.Column{{Title: "Object"}, {}} + + keys := data.Get("@keys").Array() + rows := make([]table.Row, 0, len(keys)) + rowData := make([]gjson.Result, 0, len(keys)) + + for _, key := range keys { + value := data.Get(key.Str) + title := key.Str + rows = append(rows, table.Row{title, formatValue(value, raw)}) + rowData = append(rowData, value) + } + + // Adjust column widths based on content + for _, row := range rows { + for i, cell := range row { + if i < len(columns) { + columns[i].Width = max(columns[i].Width, lipgloss.Width(cell)) + } + } + } + + t := createTable(columns, rows, objectColor) + return &TableView{ + path: path, + data: data, + table: t, + rowData: rowData, + columns: columns, + } +} + +func createTable(columns []table.Column, rows []table.Row, bgColor lipgloss.Color) table.Model { + t := table.New( + table.WithColumns(columns), + table.WithRows(rows), + table.WithFocused(true), + ) + + // Set common table styles + s := table.DefaultStyles() + s.Header = s.Header. + BorderStyle(lipgloss.NormalBorder()). + BorderForeground(lipgloss.Color("240")). + BorderBottom(true). + Bold(true) + s.Selected = s.Selected. + Foreground(lipgloss.Color("229")). + Background(bgColor). + Bold(false) + t.SetStyles(s) + + return t +} + +func formatValue(value gjson.Result, raw bool) string { + if raw { + return value.Get("@ugly").Raw + } + + switch { + case value.IsObject(): + return formatObject(value) + case value.IsArray(): + return formatArray(value) + case value.Type == gjson.String: + return value.Str + default: + return value.Raw + } +} + +func formatObject(value gjson.Result) string { + keys := value.Get("@keys").Array() + keyStrs := make([]string, len(keys)) + + for i, key := range keys { + val := value.Get(key.Str) + keyStrs[i] = formatObjectKey(key.Str, val) + } + + return "{" + strings.Join(keyStrs, ", ") + "}" +} + +func formatObjectKey(key string, val gjson.Result) string { + switch { + case val.IsObject(): + return key + ":{…}" + case val.IsArray(): + return key + ":[…]" + case val.Type == gjson.String: + str := val.Str + if lipgloss.Width(str) <= maxPreviewLength { + return fmt.Sprintf(`%s:"%s"`, key, str) + } + return fmt.Sprintf(`%s:"%s…"`, key, truncate.String(str, uint(maxPreviewLength))) + default: + return key + ":" + val.Raw + } +} + +func formatArray(value gjson.Result) string { + switch count := len(value.Array()); count { + case 0: + return "[]" + case 1: + return "[...1 item...]" + default: + return fmt.Sprintf("[...%d items...]", count) + } +} + +func isArrayOfObjects(array []gjson.Result) bool { + for _, item := range array { + if !item.IsObject() { + return false + } + } + return len(array) > 0 +} + +func sum(ints []int) int { + total := 0 + for _, n := range ints { + total += n + } + return total +} + +// An iterator over `any` values +type AnyIterator interface { + Next() bool + Err() error + Current() any +} + +// A generic iterator interface that is used by the `genericIterator` struct +// below to convert iterators over specific types to an AnyIterator +type Iterator[T any] interface { + Next() bool + Err() error + Current() T +} + +// genericIterator adapts a generic Iterator[T] to an AnyIterator. +type genericIterator[T any] struct { + iterator Iterator[T] + current any +} + +func (g *genericIterator[T]) Next() bool { + if !g.iterator.Next() { + return false + } + g.current = g.iterator.Current() + return true +} + +func (g *genericIterator[T]) Err() error { + return g.iterator.Err() +} + +func (g *genericIterator[T]) Current() any { + return g.current +} + +func genericToAnyIterator[T any](it Iterator[T]) AnyIterator { + return &genericIterator[T]{ + iterator: it, + } +} diff --git a/internal/jsonview/explorer_test.go b/internal/jsonview/explorer_test.go new file mode 100644 index 0000000..67ee730 --- /dev/null +++ b/internal/jsonview/explorer_test.go @@ -0,0 +1,66 @@ +package jsonview + +import ( + "testing" + + "github.com/charmbracelet/bubbles/help" + "github.com/tidwall/gjson" + + "github.com/stretchr/testify/require" +) + +func TestNavigateForward_EmptyRowData(t *testing.T) { + t.Parallel() + + // An empty JSON array produces a TableView with no rows. + emptyArray := gjson.Parse("[]") + view, err := newTableView("", emptyArray, false) + require.NoError(t, err) + + viewer := &JSONViewer{ + stack: []JSONView{view}, + root: "test", + help: help.New(), + } + + // Should return without panicking despite the empty data set. + model, cmd := viewer.navigateForward() + require.Equal(t, model, viewer, "expected same viewer model returned") + require.Nil(t, cmd) + + // Stack should remain unchanged (no new view pushed). + require.Equal(t, 1, len(viewer.stack), "expected stack length 1, got %d", len(viewer.stack)) +} + +// rawJSONItem implements HasRawJSON, returning pre-built JSON. +type rawJSONItem struct { + raw string +} + +func (r rawJSONItem) RawJSON() string { return r.raw } + +func TestMarshalItemsToJSONArray_WithHasRawJSON(t *testing.T) { + t.Parallel() + + items := []any{ + rawJSONItem{raw: `{"id":1,"name":"alice"}`}, + rawJSONItem{raw: `{"id":2,"name":"bob"}`}, + } + + got, err := marshalItemsToJSONArray(items) + require.NoError(t, err) + require.JSONEq(t, `[{"id":1,"name":"alice"},{"id":2,"name":"bob"}]`, string(got)) +} + +func TestMarshalItemsToJSONArray_WithoutHasRawJSON(t *testing.T) { + t.Parallel() + + items := []any{ + map[string]any{"id": 1, "name": "alice"}, + map[string]any{"id": 2, "name": "bob"}, + } + + got, err := marshalItemsToJSONArray(items) + require.NoError(t, err) + require.JSONEq(t, `[{"id":1,"name":"alice"},{"id":2,"name":"bob"}]`, string(got)) +} diff --git a/internal/jsonview/staticdisplay.go b/internal/jsonview/staticdisplay.go new file mode 100644 index 0000000..768ea34 --- /dev/null +++ b/internal/jsonview/staticdisplay.go @@ -0,0 +1,135 @@ +package jsonview + +import ( + "fmt" + "os" + "strings" + + "github.com/charmbracelet/lipgloss" + "github.com/charmbracelet/x/term" + "github.com/muesli/reflow/truncate" + "github.com/tidwall/gjson" +) + +const ( + tabWidth = 2 +) + +var ( + keyStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("75")).Bold(false) + stringValueStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("113")) + numberValueStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("215")) + boolValueStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("207")) + nullValueStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("245")).Italic(true) + bulletStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("242")) + containerStyle = lipgloss.NewStyle(). + Border(lipgloss.RoundedBorder()). + BorderForeground(lipgloss.Color("63")). + Padding(0, 1) +) + +func formatJSON(json gjson.Result, width int) string { + if !json.Exists() { + return nullValueStyle.Render("Invalid JSON") + } + return formatResult(json, 0, width) +} + +func formatResult(result gjson.Result, indent, width int) string { + switch result.Type { + case gjson.String: + str := result.Str + if str == "" { + return nullValueStyle.Render("(empty)") + } + if lipgloss.Width(str) > width { + str = truncate.String(str, uint(width-1)) + "…" + } + return stringValueStyle.Render(str) + case gjson.Number: + return numberValueStyle.Render(result.Raw) + case gjson.True: + return boolValueStyle.Render("yes") + case gjson.False: + return boolValueStyle.Render("no") + case gjson.Null: + return nullValueStyle.Render("null") + case gjson.JSON: + if result.IsArray() { + return formatJSONArray(result, indent, width) + } + return formatJSONObject(result, indent, width) + default: + return stringValueStyle.Render(result.String()) + } +} + +func isSingleLine(result gjson.Result, indent int) bool { + return !(result.IsObject() || result.IsArray()) +} + +func formatJSONArray(result gjson.Result, indent, width int) string { + items := result.Array() + if len(items) == 0 { + return nullValueStyle.Render(" (none)") + } + + numberWidth := lipgloss.Width(fmt.Sprintf("%d. ", len(items))) + + var formattedItems []string + for i, item := range items { + number := fmt.Sprintf("%d.", i+1) + numbering := getIndent(indent) + bulletStyle.Render(number) + + // If the item will be a one-liner, put it inline after the numbering, + // otherwise it starts with a newline and goes below the numbering. + itemWidth := width + if isSingleLine(item, indent+1) { + // Add right-padding: + numbering += strings.Repeat(" ", numberWidth-lipgloss.Width(number)) + itemWidth = width - lipgloss.Width(numbering) + } + value := formatResult(item, indent+1, itemWidth) + formattedItems = append(formattedItems, numbering+value) + } + return "\n" + strings.Join(formattedItems, "\n") +} + +func formatJSONObject(result gjson.Result, indent, width int) string { + keys := result.Get("@keys").Array() + if len(keys) == 0 { + return nullValueStyle.Render("(empty)") + } + + var items []string + for _, key := range keys { + value := result.Get(key.String()) + keyStr := getIndent(indent) + keyStyle.Render(key.String()+":") + // If item will be a one-liner, put it inline after the key, otherwise + // it starts with a newline and goes below the key. + itemWidth := width + if isSingleLine(value, indent+1) { + keyStr += " " + itemWidth = width - lipgloss.Width(keyStr) + } + formattedValue := formatResult(value, indent+1, itemWidth) + items = append(items, keyStr+formattedValue) + } + + return "\n" + strings.Join(items, "\n") +} + +func getIndent(indent int) string { + return strings.Repeat(" ", indent*tabWidth) +} + +func RenderJSON(title string, json gjson.Result) string { + width, _, err := term.GetSize(os.Stdout.Fd()) + if err != nil { + width = 80 + } + width -= containerStyle.GetBorderLeftSize() + containerStyle.GetBorderRightSize() + + containerStyle.GetPaddingLeft() + containerStyle.GetPaddingRight() + content := strings.TrimLeft(formatJSON(json, width), "\n") + return titleStyle.Render(title) + "\n" + containerStyle.Render(content) +} diff --git a/internal/mocktest/mocktest.go b/internal/mocktest/mocktest.go new file mode 100644 index 0000000..40b5fe5 --- /dev/null +++ b/internal/mocktest/mocktest.go @@ -0,0 +1,101 @@ +package mocktest + +import ( + "bytes" + "context" + "fmt" + "net" + "net/http" + "net/url" + "os" + "os/exec" + "path/filepath" + "runtime" + "strings" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +var mockServerURL *url.URL + +func init() { + mockServerURL, _ = url.Parse("http://localhost:4010") + if testURL := os.Getenv("TEST_API_BASE_URL"); testURL != "" { + if parsed, err := url.Parse(testURL); err == nil { + mockServerURL = parsed + } + } +} + +// OnlyMockServerDialer only allows network connections to the mock server +type OnlyMockServerDialer struct{} + +func (d *OnlyMockServerDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) { + if address == mockServerURL.Host { + return (&net.Dialer{}).DialContext(ctx, network, address) + } + + return nil, fmt.Errorf("BLOCKED: connection to %s not allowed (only allowed: %s)", address, mockServerURL.Host) +} + +func blockNetworkExceptMockServer() (http.RoundTripper, http.RoundTripper) { + restricted := &http.Transport{ + DialContext: (&OnlyMockServerDialer{}).DialContext, + } + + origClient, origDefault := http.DefaultClient.Transport, http.DefaultTransport + http.DefaultClient.Transport, http.DefaultTransport = restricted, restricted + return origClient, origDefault +} + +func restoreNetwork(origClient, origDefault http.RoundTripper) { + http.DefaultClient.Transport, http.DefaultTransport = origClient, origDefault +} + +// TestRunMockTestWithFlags runs a test against a mock server with the provided +// CLI args and ensures it succeeds +func TestRunMockTestWithFlags(t *testing.T, args ...string) { + TestRunMockTestWithPipeAndFlags(t, nil, args...) +} + +// TestRunMockTestWithPipeAndFlags runs a test against a mock server with the provided +// data piped over stdin and CLI args and ensures it succeeds +func TestRunMockTestWithPipeAndFlags(t *testing.T, pipeData []byte, args ...string) { + origClient, origDefault := blockNetworkExceptMockServer() + defer restoreNetwork(origClient, origDefault) + + // Check if mock server is running + conn, err := net.DialTimeout("tcp", mockServerURL.Host, 2*time.Second) + if err != nil { + require.Fail(t, "Mock server is not running on "+mockServerURL.Host+". Please start the mock server before running tests.") + } else { + conn.Close() + } + + // Get the path to the main command + _, filename, _, ok := runtime.Caller(0) + require.True(t, ok, "Could not get current file path") + dirPath := filepath.Dir(filename) + project := filepath.Join(dirPath, "..", "..", "cmd", "context-dev") + + args = append([]string{"run", project, "--base-url", mockServerURL.String()}, args...) + + t.Logf("Testing command: go run ./cmd/context-dev %s", strings.Join(args[2:], " ")) + + cmd := exec.Command("go", args...) + cmd.Stdin = bytes.NewReader(pipeData) + output, err := cmd.CombinedOutput() + assert.NoError(t, err, "Test failed\nError: %v\nOutput: %s", err, output) + + t.Logf("Test passed successfully\nOutput:\n%s", string(output)) +} + +func TestFile(t *testing.T, contents string) string { + tmpDir := t.TempDir() + filename := filepath.Join(tmpDir, "file.txt") + require.NoError(t, os.WriteFile(filename, []byte(contents), 0644)) + return filename +} diff --git a/internal/requestflag/innerflag.go b/internal/requestflag/innerflag.go new file mode 100644 index 0000000..528915f --- /dev/null +++ b/internal/requestflag/innerflag.go @@ -0,0 +1,289 @@ +package requestflag + +import ( + "fmt" + "reflect" + "strings" + + "github.com/urfave/cli/v3" +) + +// InnerFlag[T] represents a CLI flag for the urfave/cli package that allows setting +// nested fields within other flags. For example, using `--foo.baz` will set the "baz" +// field on a parent flag named `--foo`. +type InnerFlag[ + T []any | []map[string]any | []DateTimeValue | []DateValue | []TimeValue | []string | + []float64 | []int64 | []bool | any | map[string]any | DateTimeValue | DateValue | TimeValue | + string | float64 | int64 | bool | + *string | *float64 | *int64 | *bool | *DateTimeValue | *DateValue | *TimeValue, +] struct { + Name string // name of the flag + DefaultText string // default text of the flag for usage purposes + Usage string // usage string for help output + Aliases []string // aliases that are allowed for this flag + Validator func(T) error // custom function to validate this flag value + + OuterFlag cli.Flag // The flag on which this inner flag will set values + InnerField string // The inner field which this flag will set + DataAliases []string // alternate names recognized in YAML values passed as the outer flag + + // OuterIsArrayOfObjects tells an untyped outer flag (Flag[any], used for nullable + // complex schemas) to seed its underlying value as []map[string]any rather than + // map[string]any before SetInnerField runs. The hint is ignored for typed outer + // flags whose zero value already carries a dispatchable reflect.Kind. + OuterIsArrayOfObjects bool +} + +// GetDataAliases returns the aliases recognized when parsing inner field keys from piped or flag YAML. +func (f *InnerFlag[T]) GetDataAliases() []string { + return f.DataAliases +} + +// GetInnerField returns the API field name that this inner flag sets on its outer flag's value. +// For example, the flag --parent.foo targeting a parameter whose OpenAPI property name is "foo" +// would return "foo". This is distinct from the flag's CLI name and from any DataAliases entries. +func (f *InnerFlag[T]) GetInnerField() string { + return f.InnerField +} + +type HasOuterFlag interface { + cli.Flag + SetOuterFlag(cli.Flag) + GetOuterFlag() cli.Flag + GetInnerField() string + GetDataAliases() []string +} + +func (f *InnerFlag[T]) SetOuterFlag(flag cli.Flag) { + f.OuterFlag = flag +} + +func (f *InnerFlag[T]) GetOuterFlag() cli.Flag { + return f.OuterFlag +} + +// Implementation of the cli.Flag interface +var _ cli.Flag = (*InnerFlag[any])(nil) // Type assertion to ensure interface compliance + +func (f *InnerFlag[T]) PreParse() error { + return nil +} + +func (f *InnerFlag[T]) PostParse() error { + return nil +} + +func (f *InnerFlag[T]) Set(name string, rawVal string) error { + if parsedValue, err := parseCLIArg[T](rawVal); err != nil { + return err + } else { + if f.Validator != nil { + if err := f.Validator(parsedValue); err != nil { + return err + } + } + + if seeder, ok := f.OuterFlag.(InnerFieldSeeder); ok { + seeder.SeedInnerCollection(f.OuterIsArrayOfObjects) + } + + if settableInnerField, ok := f.OuterFlag.(SettableInnerField); ok { + settableInnerField.SetInnerField(f.InnerField, parsedValue) + } else { + return fmt.Errorf("Cannot set inner field on %v", f.OuterFlag) + } + return nil + } +} + +func (f *InnerFlag[T]) Get() any { + var zeroValue T + return zeroValue +} + +func (f *InnerFlag[T]) String() string { + return cli.FlagStringer(f) +} + +func (f *InnerFlag[T]) IsSet() bool { + return false +} + +func (f *InnerFlag[T]) Names() []string { + return cli.FlagNames(f.Name, f.Aliases) +} + +// Implementation for the cli.DocGenerationFlag interface +var _ cli.DocGenerationFlag = (*InnerFlag[any])(nil) // Type assertion to ensure interface compliance + +func (f *InnerFlag[T]) TakesValue() bool { + var t T + return reflect.TypeOf(t) == nil || reflect.TypeOf(t).Kind() != reflect.Bool +} + +func (f *InnerFlag[T]) GetUsage() string { + return f.Usage +} + +func (f *InnerFlag[T]) GetValue() string { + return "" +} + +func (f *InnerFlag[T]) GetDefaultText() string { + return f.DefaultText +} + +func (f *InnerFlag[T]) GetEnvVars() []string { + return nil +} + +func (f *InnerFlag[T]) IsDefaultVisible() bool { + return false +} + +func (f *InnerFlag[T]) TypeName() string { + var zeroValue T + ty := reflect.TypeOf(zeroValue) + if ty == nil { + return "" + } + if ty.Kind() == reflect.Pointer { + ty = ty.Elem() + } + + // Get base type name with special handling for built-in types + getTypeName := func(t reflect.Type) string { + switch t.Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + return "int" + case reflect.Float32, reflect.Float64: + return "float" + case reflect.Bool: + return "boolean" + case reflect.String: + switch t.Name() { + case "DateTimeValue": + return "datetime" + case "DateValue": + return "date" + case "TimeValue": + return "time" + default: + return "string" + } + default: + if t.Name() == "" { + return "any" + } + return strings.ToLower(t.Name()) + } + } + + switch ty.Kind() { + case reflect.Slice: + elemType := ty.Elem() + return getTypeName(elemType) + case reflect.Map: + keyType := ty.Key() + valueType := ty.Elem() + return fmt.Sprintf("%s=%s", getTypeName(keyType), getTypeName(valueType)) + default: + return getTypeName(ty) + } +} + +// Implementation for the cli.DocGenerationMultiValueFlag interface +var _ cli.DocGenerationMultiValueFlag = (*InnerFlag[any])(nil) // Type assertion to ensure interface compliance + +func (f *InnerFlag[T]) IsMultiValueFlag() bool { + return false +} + +func (f *InnerFlag[T]) IsBoolFlag() bool { + var zeroValue T + _, isBool := any(zeroValue).(bool) + return isBool +} + +// WithInnerFlags takes a command and a map of flag names to inner flags, +// and returns a modified command with the appropriate inner flags set. +func WithInnerFlags(cmd cli.Command, innerFlagMap map[string][]HasOuterFlag) cli.Command { + if len(innerFlagMap) == 0 { + return cmd + } + + // If any keys are unused by the end, we know that they were not valid + unusedInnerFlagKeys := make(map[string]struct{}) + for name := range innerFlagMap { + unusedInnerFlagKeys[name] = struct{}{} + } + + updatedFlags := make([]cli.Flag, 0, len(cmd.Flags)) + for _, flag := range cmd.Flags { + updatedFlags = append(updatedFlags, flag) + for _, name := range flag.Names() { + // Check if this flag has inner flags in our map + innerFlags, hasInnerFlags := innerFlagMap[name] + if !hasInnerFlags { + continue + } + + // Mark this inner flag key as used + delete(unusedInnerFlagKeys, name) + + for _, innerFlag := range innerFlags { + innerFlag.SetOuterFlag(flag) + updatedFlags = append(updatedFlags, innerFlag) + } + } + } + + // If there are inner flags that don't correspond to any valid outer flag + // names, then panic because the user probably made a typo or forgot to + // delete inner flags that correspond to missing outer flags. + if len(unusedInnerFlagKeys) > 0 { + unusedKeys := make([]string, 0, len(unusedInnerFlagKeys)) + for key := range unusedInnerFlagKeys { + unusedKeys = append(unusedKeys, key) + } + panic(fmt.Sprintf("Missing outer flags to use with inner flags: %v", unusedKeys)) + } + + result := cmd + result.Flags = updatedFlags + return result +} + +// Helper function to verify that all inner flags have an outer flag set and +// follow the --foo.baz prefix format +func CheckInnerFlags(cmd cli.Command) error { + var errors []string + for _, flag := range cmd.Flags { + if innerFlag, ok := flag.(HasOuterFlag); ok { + outerFlag := innerFlag.GetOuterFlag() + if outerFlag == nil { + errors = append(errors, fmt.Sprintf("inner flag %s is missing an outer flag", flag.Names())) + continue + } + + innerFlagName := flag.Names()[0] + valid := false + for _, outerName := range outerFlag.Names() { + if strings.HasPrefix(innerFlagName, outerName+".") { + valid = true + break + } + } + + if !valid { + errors = append(errors, fmt.Sprintf("inner flag %s must start with one of its outer flag's names followed by a dot", innerFlagName)) + } + } + } + + if len(errors) > 0 { + return fmt.Errorf("%s", strings.Join(errors, "; ")) + } + return nil +} diff --git a/internal/requestflag/innerflag_test.go b/internal/requestflag/innerflag_test.go new file mode 100644 index 0000000..133e8b4 --- /dev/null +++ b/internal/requestflag/innerflag_test.go @@ -0,0 +1,347 @@ +package requestflag + +import ( + "testing" + + "github.com/stretchr/testify/assert" + "github.com/urfave/cli/v3" +) + +func TestInnerFlagSet(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + flagType string + inputVal string + expected any + expectErr bool + }{ + {"string", "string", "hello", "hello", false}, + {"int64", "int64", "42", int64(42), false}, + {"float64", "float64", "3.14", float64(3.14), false}, + {"bool", "bool", "true", true, false}, + {"invalid int", "int64", "not-a-number", nil, true}, + {"invalid float", "float64", "not-a-float", nil, true}, + {"invalid bool", "bool", "not-a-bool", nil, true}, + {"yaml map", "map", "key: value", map[string]any{"key": "value"}, false}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + outerFlag := &Flag[map[string]any]{ + Name: "test-flag", + } + + var innerFlag cli.Flag + switch tt.flagType { + case "string": + innerFlag = &InnerFlag[string]{ + Name: "test-flag.test-field", + OuterFlag: outerFlag, + InnerField: "test_field", + } + case "int64": + innerFlag = &InnerFlag[int64]{ + Name: "test-flag.test-field", + OuterFlag: outerFlag, + InnerField: "test_field", + } + case "float64": + innerFlag = &InnerFlag[float64]{ + Name: "test-flag.test-field", + OuterFlag: outerFlag, + InnerField: "test_field", + } + case "bool": + innerFlag = &InnerFlag[bool]{ + Name: "test-flag.test-field", + OuterFlag: outerFlag, + InnerField: "test_field", + } + case "map": + innerFlag = &InnerFlag[map[string]any]{ + Name: "test-flag.test-field", + OuterFlag: outerFlag, + InnerField: "test_field", + } + } + + err := innerFlag.Set(innerFlag.Names()[0], tt.inputVal) + + if tt.expectErr { + assert.Error(t, err) + return + } + + assert.NoError(t, err) + actual, ok := outerFlag.Get().(map[string]any)["test_field"] + assert.True(t, ok, "Field 'test_field' should exist in the map") + assert.Equal(t, tt.expected, actual, "Expected %v (%T), got %v (%T)", tt.expected, tt.expected, actual, actual) + }) + } +} + +func TestInnerFlagValidator(t *testing.T) { + t.Parallel() + + outerFlag := &Flag[map[string]any]{Name: "test-flag"} + + innerFlag := &InnerFlag[int64]{ + Name: "test-flag.test-field", + OuterFlag: outerFlag, + InnerField: "test_field", + Validator: func(val int64) error { + if val < 0 { + return cli.Exit("Value must be non-negative", 1) + } + return nil + }, + } + + // Valid case + err := innerFlag.Set(innerFlag.Name, "42") + assert.NoError(t, err, "Expected no error for valid value, got: %v", err) + + // Should trigger validator error + err = innerFlag.Set(innerFlag.Name, "-5") + assert.Error(t, err, "Expected error for invalid value, got none") +} + +func TestWithInnerFlags(t *testing.T) { + t.Parallel() + + outerFlag := &Flag[map[string]any]{Name: "outer"} + innerFlag := &InnerFlag[string]{ + Name: "outer.baz", + InnerField: "baz", + } + + cmd := WithInnerFlags(cli.Command{ + Name: "test-command", + Flags: []cli.Flag{outerFlag}, + }, map[string][]HasOuterFlag{ + "outer": {innerFlag}, + }) + + // Verify that the command now has both the original flag and inner flag + assert.Len(t, cmd.Flags, 2, "Expected 2 flags, got %d", len(cmd.Flags)) + assert.Equal(t, outerFlag, cmd.Flags[0], "First flag should be outerFlag") + assert.Equal(t, innerFlag, cmd.Flags[1], "Second flag should be innerFlag") + assert.Same(t, outerFlag, innerFlag.OuterFlag, "innerFlag.OuterFlag should point to outerFlag") +} + +func TestInnerFlagTypeNames(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + flag cli.DocGenerationFlag + expected string + }{ + {"string", &InnerFlag[string]{}, "string"}, + {"int64", &InnerFlag[int64]{}, "int"}, + {"float64", &InnerFlag[float64]{}, "float"}, + {"bool", &InnerFlag[bool]{}, "boolean"}, + {"string slice", &InnerFlag[[]string]{}, "string"}, + {"date", &InnerFlag[DateValue]{}, "date"}, + {"datetime", &InnerFlag[DateTimeValue]{}, "datetime"}, + {"time", &InnerFlag[TimeValue]{}, "time"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + typeName := tt.flag.TypeName() + assert.Equal(t, tt.expected, typeName, "Expected type name %q, got %q", tt.expected, typeName) + }) + } +} + +func TestInnerYamlHandling(t *testing.T) { + t.Parallel() + + // Test with map value + t.Run("Parse YAML to map", func(t *testing.T) { + t.Parallel() + + outerFlag := &Flag[map[string]any]{Name: "outer"} + innerFlag := &InnerFlag[map[string]any]{ + Name: "outer.baz", + OuterFlag: outerFlag, + InnerField: "baz", + } + + err := innerFlag.Set(innerFlag.Name, "{name: test, value: 42}") + assert.NoError(t, err) + + // Retrieve and check the parsed YAML map + result, ok := outerFlag.Get().(map[string]any) + assert.True(t, ok, "Expected map[string]any from outerFlag.Get()") + yamlField, ok := result["baz"].(map[string]any) + assert.True(t, ok, "Expected map[string]any, got %T", result["baz"]) + val := yamlField + + if ok { + assert.Equal(t, map[string]any{"name": "test", "value": uint64(42)}, val) + } + }) + + // Test with invalid YAML + t.Run("Parse invalid YAML", func(t *testing.T) { + t.Parallel() + + outerFlag := &Flag[map[string]any]{Name: "outer"} + innerFlag := &InnerFlag[map[string]any]{ + Name: "outer.baz", + OuterFlag: outerFlag, + InnerField: "baz", + } + + invalidYaml := `[not closed` + err := innerFlag.Set(innerFlag.Name, invalidYaml) + assert.Error(t, err) + }) + + // Test setting inner flags on a map multiple times + t.Run("Set inner flags on map multiple times", func(t *testing.T) { + t.Parallel() + + outerFlag := &Flag[map[string]any]{Name: "outer"} + + // Set first inner flag + firstInnerFlag := &InnerFlag[string]{ + Name: "outer.first-flag", + OuterFlag: outerFlag, + InnerField: "first_field", + } + + err := firstInnerFlag.Set(firstInnerFlag.Name, "first-value") + assert.NoError(t, err) + + // Set second inner flag + secondInnerFlag := &InnerFlag[int64]{ + Name: "outer.second-flag", + OuterFlag: outerFlag, + InnerField: "second_field", + } + + err = secondInnerFlag.Set(secondInnerFlag.Name, "42") + assert.NoError(t, err) + + // Verify both fields are set correctly + result := outerFlag.Get().(map[string]any) + assert.Equal(t, map[string]any{"first_field": "first-value", "second_field": int64(42)}, result) + }) + + // Test setting YAML and then an inner flag + t.Run("Set YAML and then inner flag", func(t *testing.T) { + t.Parallel() + + outerFlag := &Flag[map[string]any]{Name: "outer"} + + // First set the outer flag with YAML + err := outerFlag.Set(outerFlag.Name, `{existing: value, another: field}`) + assert.NoError(t, err) + + // Then set an inner flag + innerFlag := &InnerFlag[string]{ + Name: "outer.inner-flag", + OuterFlag: outerFlag, + InnerField: "new_field", + } + + err = innerFlag.Set(innerFlag.Name, "inner-value") + assert.NoError(t, err) + + // Verify both the YAML content and inner flag value + result := outerFlag.Get().(map[string]any) + assert.Equal(t, map[string]any{ + "existing": "value", + "another": "field", + "new_field": "inner-value", + }, result) + }) +} + +func TestInnerFlagWithSliceType(t *testing.T) { + t.Parallel() + + t.Run("Setting inner flags on slice of maps", func(t *testing.T) { + t.Parallel() + + outerFlag := &Flag[[]map[string]any]{Name: "outer"} + + // Set first inner flag (should create first item) + firstInnerFlag := &InnerFlag[string]{ + Name: "outer.name-flag", + OuterFlag: outerFlag, + InnerField: "name", + } + + err := firstInnerFlag.Set(firstInnerFlag.Name, "item1") + assert.NoError(t, err) + + // Set second inner flag (should modify first item) + secondInnerFlag := &InnerFlag[int64]{ + Name: "outer.count-flag", + OuterFlag: outerFlag, + InnerField: "count", + } + + err = secondInnerFlag.Set(secondInnerFlag.Name, "42") + assert.NoError(t, err) + + // Set name flag again (should create second item) + err = firstInnerFlag.Set(firstInnerFlag.Name, "item2") + assert.NoError(t, err) + + // Verify the slice has two items with correct values + result := outerFlag.Get().([]map[string]any) + + assert.Equal(t, []map[string]any{ + {"name": "item1", "count": int64(42)}, + {"name": "item2"}, + }, result) + assert.Nil(t, result[1]["count"], "Second item should not have count field") + }) + + t.Run("Appending to existing slice", func(t *testing.T) { + t.Parallel() + + // Initialize with existing items + outerFlag := &Flag[[]map[string]any]{Name: "outer"} + err := outerFlag.Set(outerFlag.Name, `{name: initial}`) + assert.NoError(t, err) + + // Set inner flag to modify existing item + modifyFlag := &InnerFlag[string]{ + Name: "outer.value-flag", + OuterFlag: outerFlag, + InnerField: "value", + } + + err = modifyFlag.Set(modifyFlag.Name, "updated") + assert.NoError(t, err) + + // Set inner flag to create new item + newItemFlag := &InnerFlag[string]{ + Name: "outer.name-flag", + OuterFlag: outerFlag, + InnerField: "name", + } + + err = newItemFlag.Set(newItemFlag.Name, "second") + assert.NoError(t, err) + + // Verify both items + result := outerFlag.Get().([]map[string]any) + assert.Equal(t, []map[string]any{ + {"name": "initial", "value": "updated"}, + {"name": "second"}, + }, result) + }) +} diff --git a/internal/requestflag/requestflag.go b/internal/requestflag/requestflag.go new file mode 100644 index 0000000..77c4f1f --- /dev/null +++ b/internal/requestflag/requestflag.go @@ -0,0 +1,992 @@ +package requestflag + +import ( + "encoding/json" + "fmt" + "reflect" + "strconv" + "strings" + "time" + "unicode" + + "github.com/goccy/go-yaml" + "github.com/urfave/cli/v3" +) + +// formatForFlagSet converts a Go value parsed from YAML/JSON stdin data into a string +// that flag.Set (and thus parseCLIArg) can parse correctly for each flag type. +// Strings are returned as-is (parseCLIArg[string] assigns the raw value directly, so +// JSON-quoting must be avoided). Scalars use %v. Complex types (maps, slices) are +// JSON-encoded, which the yaml.Unmarshal default branch in parseCLIArg can parse. +func formatForFlagSet(val any) (string, error) { + switch v := val.(type) { + case string: + return v, nil + case bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64: + return fmt.Sprintf("%v", val), nil + default: + b, err := json.Marshal(val) + if err != nil { + return "", fmt.Errorf("cannot format value %T for flag.Set: %w", val, err) + } + return string(b), nil + } +} + +// Flag [T] is a generic flag base which can be used to implement the most +// common interfaces used by urfave/cli. Additionally, it allows specifying +// where in an HTTP request the flag values should be placed (e.g. query, body, etc.). +// +// Pointer-to-primitive type parameters (e.g. *string) are used for flags whose underlying +// schema is nullable. They give flags a tri-state: unset (excluded from the request), +// set to the literal "null" (nil pointer → JSON null), or set to a value (*v → JSON value). +type Flag[ + T []any | []map[string]any | []DateTimeValue | []DateValue | []TimeValue | []string | + []float64 | []int64 | []bool | any | map[string]any | DateTimeValue | DateValue | TimeValue | + string | float64 | int64 | bool | + *string | *float64 | *int64 | *bool | *DateTimeValue | *DateValue | *TimeValue, +] struct { + Name string // name of the flag + Category string // category of the flag, if any + DefaultText string // default text of the flag for usage purposes + HideDefault bool // whether to hide the default value in output + Usage string // usage string for help output + Sources cli.ValueSourceChain // sources to load flag value from + Required bool // whether the flag is required or not + Hidden bool // whether to hide the flag in help output + Default T // default value for this flag if not set by from any source + Aliases []string // aliases that are allowed for this flag + Validator func(T) error // custom function to validate this flag value + + QueryPath string // location in the request query string to put this flag's value + HeaderPath string // location in the request header to put this flag's value + BodyPath string // location in the request body to put this flag's value + BodyRoot bool // if true, then use this value as the entire request body + PathParam string // name of the URL path parameter this flag's value maps to + + // Const, when true, marks this flag as a constant. The flag's Default value is used as the fixed value + // and always included in the request (IsSet returns true). The user can still see and override the flag, + // but isn't required to provide it. This is used for single-value enums and `x-stainless-const` + // parameters. + Const bool + + // FileInput, when true, indicates that the flag value is always treated as a file path. The file is read + // automatically without requiring the "@" prefix. This is used for parameters with `type: string, format: + // binary` in the OpenAPI spec. + FileInput bool + + // DataAliases is a list of alternate names for this parameter recognized when parsing piped YAML/JSON + // input. Values keyed by any alias are translated to the canonical API name before being sent. + DataAliases []string + + // unexported fields for internal use + count int // number of times the flag has been set + hasBeenSet bool // whether the flag has been set from env or file + applied bool // whether the flag has been applied to a flag set already + value cli.Value // value representing this flag's value +} + +// Type assertions to verify we implement the relevant urfave/cli interfaces +var _ cli.CategorizableFlag = (*Flag[any])(nil) + +// InRequest interface for flags that should be included in HTTP requests +type InRequest interface { + GetQueryPath() string + GetHeaderPath() string + GetBodyPath() string + GetPathParam() string + IsBodyRoot() bool + IsFileInput() bool + GetDataAliases() []string +} + +func (f Flag[T]) GetQueryPath() string { + return f.QueryPath +} + +func (f Flag[T]) GetHeaderPath() string { + return f.HeaderPath +} + +func (f Flag[T]) GetBodyPath() string { + return f.BodyPath +} + +func (f Flag[T]) GetPathParam() string { + return f.PathParam +} + +func (f Flag[T]) IsBodyRoot() bool { + return f.BodyRoot +} + +func (f Flag[T]) IsFileInput() bool { + return f.FileInput +} + +func (f Flag[T]) GetDataAliases() []string { + return f.DataAliases +} + +// The values that will be sent in different parts of a request. +type RequestContents struct { + Queries map[string]any + Headers map[string]any + Body any +} + +// ApplyStdinDataToFlags sets flag values from a parsed stdin data map for flags that have not already been +// set via the command line. This allows piped YAML/JSON data to satisfy path, query, and header parameters. +// Body parameters are excluded: they are already handled by the maps.Copy merge in flagOptions. +// For each unset flag, if the parsed data map contains a key matching the flag's QueryPath, HeaderPath, or +// PathParam (or any of its DataAliases), the flag is set to that value via flag.Set. +// +// Inner flags (those with an outer flag) are also handled: if the outer flag's body path key exists in the +// data map and contains a nested map with a key matching the inner flag's field (or aliases), the inner +// flag is set from that nested value. +func ApplyStdinDataToFlags(cmd *cli.Command, data map[string]any) error { + for _, flag := range cmd.Flags { + if flag.IsSet() { + continue + } + + // Handle inner flags: look for their value nested under the outer flag's body path. + if inner, ok := flag.(HasOuterFlag); ok { + outer, outerOk := inner.GetOuterFlag().(InRequest) + if !outerOk || outer.GetBodyPath() == "" { + continue + } + nested, ok := data[outer.GetBodyPath()].(map[string]any) + if !ok { + continue + } + innerField := inner.GetInnerField() + val, found := nested[innerField] + if !found { + for _, alias := range inner.GetDataAliases() { + if alias != "" && alias != innerField { + if v, ok := nested[alias]; ok { + val, found = v, true + break + } + } + } + } + if !found { + continue + } + setVal, err := formatForFlagSet(val) + if err != nil { + return fmt.Errorf("cannot format piped value for flag %q: %w", flag.Names()[0], err) + } + if err := flag.Set(flag.Names()[0], setVal); err != nil { + return fmt.Errorf("cannot set flag %q from piped data: %w", flag.Names()[0], err) + } + continue + } + + inReq, ok := flag.(InRequest) + if !ok { + continue + } + + // Try each request location in turn, checking the canonical path key and all aliases. + // Body params are excluded: they are already handled by the maps.Copy merge in flagOptions. + for _, path := range []string{inReq.GetQueryPath(), inReq.GetHeaderPath(), inReq.GetPathParam()} { + if path == "" { + continue + } + var val any + var found bool + for _, key := range append([]string{path}, inReq.GetDataAliases()...) { + if v, ok := data[key]; ok { + val, found = v, true + break + } + } + if !found { + continue + } + setVal, err := formatForFlagSet(val) + if err != nil { + return fmt.Errorf("cannot format piped value for flag %q: %w", flag.Names()[0], err) + } + if err := flag.Set(flag.Names()[0], setVal); err != nil { + return fmt.Errorf("cannot set flag %q from piped data: %w", flag.Names()[0], err) + } + break + } + } + return nil +} + +func ExtractRequestContents(cmd *cli.Command) RequestContents { + bodyMap := make(map[string]any) + res := RequestContents{ + Queries: make(map[string]any), + Headers: make(map[string]any), + Body: bodyMap, + } + + for _, flag := range cmd.Flags { + if !flag.IsSet() { + continue + } + + value := flag.Get() + if toSend, ok := flag.(InRequest); ok { + if queryPath := toSend.GetQueryPath(); queryPath != "" { + res.Queries[queryPath] = value + } + if headerPath := toSend.GetHeaderPath(); headerPath != "" { + res.Headers[headerPath] = value + } + if toSend.IsBodyRoot() { + res.Body = value + } else if bodyPath := toSend.GetBodyPath(); bodyPath != "" { + bodyMap[bodyPath] = value + } + } + } + return res +} + +func GetMissingRequiredFlags(cmd *cli.Command, body any) []cli.Flag { + missing := []cli.Flag{} + for _, flag := range cmd.Flags { + if flag.IsSet() { + continue + } + + if required, ok := flag.(cli.RequiredFlag); ok && required.IsRequired() { + missing = append(missing, flag) + continue + } + + if r, ok := flag.(RequiredFlagOrStdin); !ok || !r.IsRequiredAsFlagOrStdin() { + continue + } + + if toSend, ok := flag.(InRequest); ok { + if toSend.IsBodyRoot() { + if body != nil { + continue + } + } else if bodyPath := toSend.GetBodyPath(); bodyPath != "" { + if bodyMap, ok := body.(map[string]any); ok { + if _, found := bodyMap[bodyPath]; found { + continue + } + } + } + } + missing = append(missing, flag) + } + return missing +} + +// Implementation of the cli.Flag interface +var _ cli.Flag = (*Flag[any])(nil) // Type assertion to ensure interface compliance + +func (f *Flag[T]) PreParse() error { + newVal := f.Default + f.value = &cliValue[T]{newVal} + + // Validate the given default or values set from external sources as well + if f.Validator != nil { + if err := f.Validator(f.value.Get().(T)); err != nil { + return err + } + } + f.applied = true + return nil +} + +func (f *Flag[T]) PostParse() error { + if !f.hasBeenSet { + if val, source, found := f.Sources.LookupWithSource(); found { + if val != "" || reflect.TypeOf(f.value).Kind() == reflect.String { + if err := f.Set(f.Name, val); err != nil { + return fmt.Errorf( + "could not parse %[1]q as %[2]T value from %[3]s for flag %[4]s: %[5]s", + val, f.value, source, f.Name, err, + ) + } + } else if val == "" && reflect.TypeOf(f.value).Kind() == reflect.Bool { + _ = f.Set(f.Name, "false") + } + + f.hasBeenSet = true + } + } + return nil +} + +func (f *Flag[T]) Set(name string, val string) error { + // Initialize flag if needed + if !f.applied { + if err := f.PreParse(); err != nil { + return err + } + f.applied = true + } + + f.count++ + + // If this is the first time setting a slice type, reset it to empty + // to avoid appending to the default value + if f.count == 1 && f.value != nil { + typ := reflect.TypeOf(f.Default) + if typ != nil && typ.Kind() == reflect.Slice { + // Create a new empty slice of the same type and set it + emptySlice := reflect.MakeSlice(typ, 0, 0).Interface() + f.value = &cliValue[T]{emptySlice.(T)} + } + } + + if err := f.value.Set(val); err != nil { + return err + } + + f.hasBeenSet = true + + if f.Validator != nil { + if err := f.Validator(f.value.Get().(T)); err != nil { + return err + } + } + return nil +} + +func (f *Flag[T]) Get() any { + if f.value != nil { + return f.value.Get() + } + return f.Default +} + +func (f *Flag[T]) String() string { + return cli.FlagStringer(f) +} + +func (f *Flag[T]) IsSet() bool { + return f.hasBeenSet || f.Const +} + +func (f *Flag[T]) Names() []string { + return cli.FlagNames(f.Name, f.Aliases) +} + +// Implementation for the cli.VisibleFlag interface +var _ cli.VisibleFlag = (*Flag[any])(nil) // Type assertion to ensure interface compliance + +func (f *Flag[T]) IsVisible() bool { + return !f.Hidden +} + +func (f *Flag[T]) GetCategory() string { + return f.Category +} + +func (f *Flag[T]) SetCategory(c string) { + f.Category = c +} + +// Implementation for the cli.RequiredFlag interface +var _ cli.RequiredFlag = (*Flag[any])(nil) // Type assertion to ensure interface compliance + +func (f *Flag[T]) IsRequired() bool { + // Const flags are always auto-set, so never required from the user. + if f.Const { + return false + } + // Intentionally don't use `f.Required`, because request flags may be passed + // over stdin as well as by flag. + if f.BodyPath != "" || f.BodyRoot || f.PathParam != "" || f.QueryPath != "" || f.HeaderPath != "" { + return false + } + return f.Required +} + +type RequiredFlagOrStdin interface { + IsRequiredAsFlagOrStdin() bool +} + +func (f *Flag[T]) IsRequiredAsFlagOrStdin() bool { + // Const flags are always auto-set, so never required from the user. + if f.Const { + return false + } + return f.Required +} + +// Implementation for the cli.DocGenerationFlag interface +var _ cli.DocGenerationFlag = (*Flag[any])(nil) // Type assertion to ensure interface compliance + +func (f *Flag[T]) TakesValue() bool { + var t T + return reflect.TypeOf(t) == nil || reflect.TypeOf(t).Kind() != reflect.Bool +} + +func (f *Flag[T]) GetUsage() string { + return f.Usage +} + +func (f *Flag[T]) GetValue() string { + if f.value == nil { + return "" + } + return f.value.String() +} + +func (f *Flag[T]) GetDefaultText() string { + return f.DefaultText +} + +// GetEnvVars returns the env vars for this flag +func (f *Flag[T]) GetEnvVars() []string { + return f.Sources.EnvKeys() +} + +func (f *Flag[T]) IsDefaultVisible() bool { + return !f.HideDefault +} + +func (f *Flag[T]) TypeName() string { + ty := reflect.TypeOf(f.Default) + if ty == nil { + return "" + } + // Deref pointer-typed flags so --help surfaces the pointee kind (e.g. "string"), not + // Go's pointer syntax. + if ty.Kind() == reflect.Pointer { + ty = ty.Elem() + } + + // Get base type name with special handling for built-in types + getTypeName := func(t reflect.Type) string { + switch t.Kind() { + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, + reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: + return "int" + case reflect.Float32, reflect.Float64: + return "float" + case reflect.Bool: + return "boolean" + case reflect.String: + switch t.Name() { + case "DateTimeValue": + return "datetime" + case "DateValue": + return "date" + case "TimeValue": + return "time" + default: + return "string" + } + default: + if t.Name() == "" { + return "any" + } + return strings.ToLower(t.Name()) + } + } + + switch ty.Kind() { + case reflect.Slice: + elemType := ty.Elem() + return getTypeName(elemType) + case reflect.Map: + keyType := ty.Key() + valueType := ty.Elem() + return fmt.Sprintf("%s=%s", getTypeName(keyType), getTypeName(valueType)) + default: + return getTypeName(ty) + } +} + +// Implementation for the cli.DocGenerationMultiValueFlag interface +var _ cli.DocGenerationMultiValueFlag = (*Flag[any])(nil) // Type assertion to ensure interface compliance + +func (f *Flag[T]) IsMultiValueFlag() bool { + if reflect.TypeOf(f.Default) == nil { + return false + } + kind := reflect.TypeOf(f.Default).Kind() + return kind == reflect.Slice || kind == reflect.Map +} + +func (f *Flag[T]) IsBoolFlag() bool { + // Flag[*bool] is deliberately not treated as a bool flag — the pointer form needs an + // explicit value (`--foo true`, `--foo null`) to disambiguate the tri-state. + _, isBool := any(f.Default).(bool) + return isBool +} + +// Implementation for the cli.Countable interface +var _ cli.Countable = (*Flag[any])(nil) // Type assertion to ensure interface compliance + +func (f *Flag[T]) Count() int { + return f.count +} + +// Implementation for the cli.LocalFlag interface +var _ cli.LocalFlag = (*Flag[any])(nil) // Type assertion to ensure interface compliance + +func (f Flag[T]) IsLocal() bool { + // By default, all request flags are local, i.e. can be provided at any part of the CLI command. + return true +} + +// cliValue is a generic implementation of cli.Value for common types +type cliValue[ + T []any | []map[string]any | []DateTimeValue | []DateValue | []TimeValue | []string | []float64 | + []int64 | []bool | any | map[string]any | DateTimeValue | DateValue | TimeValue | string | + float64 | int64 | bool | + *string | *float64 | *int64 | *bool | *DateTimeValue | *DateValue | *TimeValue, +] struct { + value T +} + +// Take an argument string for a single argument and convert it into a typed +// value for one of the supported CLI argument types +func parseCLIArg[ + T []any | []map[string]any | []DateTimeValue | []DateValue | []TimeValue | []string | []float64 | + []int64 | []bool | any | map[string]any | DateTimeValue | DateValue | TimeValue | string | + float64 | int64 | bool | + *string | *float64 | *int64 | *bool | *DateTimeValue | *DateValue | *TimeValue, +](value string) (T, error) { + var parsedValue any + var err error + + var empty T + + if value == "null" { + switch any(empty).(type) { + // Pointer-to-primitive: explicit nil gives the tri-state its "null" state + // (unset / null / value). Without this, numeric flags would fail to parse + // "null" and string flags would accept the literal word as a raw value. + case *string, *int64, *float64, *bool, *DateValue, *DateTimeValue, *TimeValue: + return empty, nil + // Maps marshal nil as JSON null natively; short-circuit avoids a YAML round-trip. + case map[string]any: + return empty, nil + } + } + + switch any(empty).(type) { + case string: + parsedValue = value + case int64: + parsedValue, err = strconv.ParseInt(value, 0, 64) + case float64: + parsedValue, err = strconv.ParseFloat(value, 64) + case bool: + parsedValue, err = strconv.ParseBool(value) + case DateTimeValue: + var dt DateTimeValue + err = (&dt).Parse(value) + if err == nil { + parsedValue = dt + } + + case DateValue: + var d DateValue + err = (&d).Parse(value) + if err == nil { + parsedValue = d + } + + case TimeValue: + var t TimeValue + err = (&t).Parse(value) + if err == nil { + parsedValue = t + } + + // Pointer-to-primitive flags reach here only when `value != "null"`; we parse the + // pointee type and return its address so JSON marshaling emits the underlying value. + case *string: + v := value + parsedValue = &v + case *int64: + var v int64 + v, err = strconv.ParseInt(value, 0, 64) + if err == nil { + parsedValue = &v + } + case *float64: + var v float64 + v, err = strconv.ParseFloat(value, 64) + if err == nil { + parsedValue = &v + } + case *bool: + var v bool + v, err = strconv.ParseBool(value) + if err == nil { + parsedValue = &v + } + case *DateTimeValue: + var dt DateTimeValue + err = (&dt).Parse(value) + if err == nil { + parsedValue = &dt + } + case *DateValue: + var d DateValue + err = (&d).Parse(value) + if err == nil { + parsedValue = &d + } + case *TimeValue: + var t TimeValue + err = (&t).Parse(value) + if err == nil { + parsedValue = &t + } + + default: + if strings.HasPrefix(value, "@") { + // File literals like @file.txt should work here + parsedValue = value + } else { + var yamlValue T + err = yaml.Unmarshal([]byte(value), &yamlValue) + if err == nil { + parsedValue = yamlValue + } else if allowAsLiteralString(value) { + parsedValue = value + } else { + parsedValue = nil + err = fmt.Errorf("failed to parse as YAML: %w", err) + } + } + } + + // Nil needs to be handled specially because unmarshalling a YAML `null` + // causes problems when doing type assertions. + if parsedValue == nil { + parsedValue = (*struct{})(nil) + } + + if err == nil { + if typedValue, ok := parsedValue.(T); ok { + return typedValue, nil + } else { + expectedType := reflect.TypeFor[T]() + err = fmt.Errorf("Couldn't convert %q (%v) to expected type %v", value, parsedValue, expectedType) + } + } + return empty, err + +} + +// Ptr returns a pointer to its argument. It is used to initialize `Default` on pointer-typed +// Flag values, since Go does not allow taking the address of a composite literal's element +// or of an untyped constant. +func Ptr[T any](v T) *T { + return &v +} + +// Assuming this string failed to parse as valid YAML, this function will +// return true for strings that can reasonably be interpreted as a string literal, +// like identifiers (`foo_bar`), UUIDs (`945b2f0c-8e89-487a-b02c-f851c69ea459`), +// base64 (`aGVsbG8=`), and qualified identifiers (`color.Red`). This should +// not include strings that look like mistyped YAML (e.g. `{key:`) +func allowAsLiteralString(s string) bool { + for _, c := range s { + if !unicode.IsLetter(c) && !unicode.IsDigit(c) && + c != '_' && c != '-' && c != '.' && c != '=' { + return false + } + } + return true +} + +// Parse the input string and set result as the cliValue's value +func (c *cliValue[T]) Set(value string) error { + valueType := reflect.TypeOf(c.value) + // When setting slice values, we append to the existing values + // e.g. --foo 10 --foo 20 --foo 30 => [10, 20, 30] + if valueType != nil && valueType.Kind() == reflect.Slice { + elemType := valueType.Elem() + + var singleElem any + var err error + switch elemType.Kind() { + case reflect.String: + singleElem, err = parseCLIArg[string](value) + case reflect.Int64: + singleElem, err = parseCLIArg[int64](value) + case reflect.Float64: + singleElem, err = parseCLIArg[float64](value) + case reflect.Bool: + singleElem, err = parseCLIArg[bool](value) + default: + // Check for special types by name + switch elemType.Name() { + case "DateTimeValue": + singleElem, err = parseCLIArg[DateTimeValue](value) + case "DateValue": + singleElem, err = parseCLIArg[DateValue](value) + case "TimeValue": + singleElem, err = parseCLIArg[TimeValue](value) + default: + // This handles []map[string]any + if elemType.Kind() == reflect.Map && elemType.Key().Kind() == reflect.String { + singleElem, err = parseCLIArg[map[string]any](value) + } else { + singleElem, err = parseCLIArg[any](value) + } + } + } + + if err != nil { + return err + } + + // Append the new element to the slice + sliceValue := reflect.ValueOf(c.value) + if !sliceValue.IsValid() || sliceValue.IsNil() { + // Create a new slice if the current one is nil + sliceValue = reflect.MakeSlice(valueType, 0, 1) + } + + // Append the new element + newElem := reflect.ValueOf(singleElem) + sliceValue = reflect.Append(sliceValue, newElem) + + // Set the updated slice back to c.value + c.value = sliceValue.Interface().(T) + } else { + // For non-slice types, simply parse and set the value + if parsedValue, err := parseCLIArg[T](value); err != nil { + return err + } else { + c.value = parsedValue + } + } + + return nil +} + +func (c *cliValue[T]) Get() any { + return c.value +} + +func (c *cliValue[T]) String() string { + switch v := any(c.value).(type) { + case string, int, int64, float64, bool, DateTimeValue, DateValue, TimeValue, + []string, []int, []int64, []float64, []bool, []DateTimeValue, []DateValue, []TimeValue: + // For basic types, use standard string representation + return fmt.Sprintf("%v", v) + + case *string, *int64, *float64, *bool, *DateTimeValue, *DateValue, *TimeValue: + // Pointer-to-primitive: nil renders as "null" (the CLI literal that produces it); + // non-nil derefs to the pointee's standard representation. + rv := reflect.ValueOf(v) + if rv.IsNil() { + return "null" + } + return fmt.Sprintf("%v", rv.Elem().Interface()) + + default: + // For complex types, convert to YAML + yamlBytes, err := yaml.MarshalWithOptions(c.value, yaml.Flow(true)) + if err != nil { + // Fall back to standard format if YAML conversion fails + return fmt.Sprintf("%v", c.value) + } + return string(yamlBytes) + } +} + +func (c *cliValue[T]) IsBoolFlag() bool { + _, ok := any(c.value).(bool) + return ok +} + +// Time-related value types +type DateValue string +type DateTimeValue string +type TimeValue string + +// String methods for time-related types +func (d DateValue) String() string { + return string(d) +} + +func (d DateTimeValue) String() string { + return string(d) +} + +func (t TimeValue) String() string { + return string(t) +} + +// parseTimeWithFormats attempts to parse a string using multiple formats +func parseTimeWithFormats(s string, formats []string) (time.Time, error) { + var lastErr error + for _, format := range formats { + t, err := time.Parse(format, s) + if err == nil { + return t, nil + } + lastErr = err + } + return time.Time{}, lastErr +} + +// Parse methods for time-related types +func (d *DateValue) Parse(s string) error { + formats := []string{ + "2006-01-02", + "01/02/2006", + "Jan 2, 2006", + "January 2, 2006", + "2-Jan-2006", + } + + t, err := parseTimeWithFormats(s, formats) + if err != nil { + return fmt.Errorf("unable to parse date: %v", err) + } + + *d = DateValue(t.Format("2006-01-02")) + return nil +} + +func (d *DateTimeValue) Parse(s string) error { + formats := []string{ + time.RFC3339, + time.RFC3339Nano, + "2006-01-02T15:04:05", + "2006-01-02 15:04:05", + time.RFC1123, + time.RFC822, + time.ANSIC, + } + + t, err := parseTimeWithFormats(s, formats) + if err != nil { + return fmt.Errorf("unable to parse datetime: %v", err) + } + + *d = DateTimeValue(t.Format(time.RFC3339)) + return nil +} + +func (t *TimeValue) Parse(s string) error { + formats := []string{ + "15:04:05", + "15:04:05.999999999Z07:00", + "3:04:05PM", + "3:04 PM", + "15:04", + time.Kitchen, + } + + parsedTime, err := parseTimeWithFormats(s, formats) + if err != nil { + return fmt.Errorf("unable to parse time: %v", err) + } + + *t = TimeValue(parsedTime.Format("15:04:05")) + return nil +} + +// Allow setting inner fields on other flags (e.g. --foo.baz can set the "baz" +// field on the --foo flag) +type SettableInnerField interface { + SetInnerField(string, any) +} + +// InnerFieldSeeder lets an InnerFlag prepare its outer flag's underlying value +// before dispatching SetInnerField. This is only meaningful for Flag[any] — +// the codegen output for nullable complex schemas — whose untyped-nil zero +// value would otherwise have no reflect.Kind for the inner-field switch to +// dispatch on. +type InnerFieldSeeder interface { + SeedInnerCollection(isArrayOfObjects bool) +} + +func (f *Flag[T]) SetInnerField(field string, val any) { + if f.value == nil { + f.value = &cliValue[T]{} + } + + if settableInnerField, ok := f.value.(SettableInnerField); ok { + settableInnerField.SetInnerField(field, val) + f.hasBeenSet = true + } else { + panic(fmt.Sprintf("Cannot set inner field: %v", f.value)) + } +} + +// SeedInnerCollection initializes a Flag[any]'s underlying value as an empty +// map[string]any or []map[string]any so subsequent SetInnerField calls have a +// dispatchable reflect.Kind. For typed Flag[T] this is a no-op: the type +// assertion fails and the existing reflect.Kind on the typed-nil zero value +// already routes correctly. +func (f *Flag[T]) SeedInnerCollection(isArrayOfObjects bool) { + if f.value == nil { + f.value = &cliValue[T]{} + } + cv, ok := f.value.(*cliValue[T]) + if !ok { + return + } + if reflect.ValueOf(cv.value).Kind() != reflect.Invalid { + return + } + if isArrayOfObjects { + if seed, ok := any([]map[string]any{}).(T); ok { + cv.value = seed + } + return + } + if seed, ok := any(map[string]any{}).(T); ok { + cv.value = seed + } +} + +func (c *cliValue[T]) SetInnerField(field string, val any) { + flagVal := c.value + flagValReflect := reflect.ValueOf(flagVal) + switch flagValReflect.Kind() { + case reflect.Slice: + if flagValReflect.Type().Elem().Kind() != reflect.Map { + return + } + + sliceLen := flagValReflect.Len() + if sliceLen > 0 { + // Check if the last element already has the InnerField + lastElement := flagValReflect.Index(sliceLen - 1).Interface().(map[string]any) + if _, hasInnerField := lastElement[field]; !hasInnerField { + // Last element doesn't have the field, set it + lastElement[field] = val + return + } + } + + // Create a new map and append it to the slice + newMap := map[string]any{field: val} + switch sliceVal := any(c.value).(type) { + case []map[string]any: + c.value = any(append(sliceVal, newMap)).(T) + case []any: + c.value = any(append(sliceVal, newMap)).(T) + } + + case reflect.Map: + mapVal, ok := any(flagVal).(map[string]any) + if !ok || mapVal == nil { + mapVal = map[string]any{field: val} + c.value = any(mapVal).(T) + } else { + mapVal[field] = val + } + } +} diff --git a/internal/requestflag/requestflag_test.go b/internal/requestflag/requestflag_test.go new file mode 100644 index 0000000..779bd57 --- /dev/null +++ b/internal/requestflag/requestflag_test.go @@ -0,0 +1,1227 @@ +package requestflag + +import ( + "encoding/json" + "fmt" + "testing" + "time" + + "github.com/goccy/go-yaml" + "github.com/stretchr/testify/assert" + "github.com/urfave/cli/v3" +) + +func TestDateValueParse(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + input string + want string + wantErr bool + }{ + { + name: "ISO format", + input: "2023-05-15", + want: "2023-05-15", + wantErr: false, + }, + { + name: "US format", + input: "05/15/2023", + want: "2023-05-15", + wantErr: false, + }, + { + name: "Short month format", + input: "May 15, 2023", + want: "2023-05-15", + wantErr: false, + }, + { + name: "Long month format", + input: "January 15, 2023", + want: "2023-01-15", + wantErr: false, + }, + { + name: "British format", + input: "15-Jan-2023", + want: "2023-01-15", + wantErr: false, + }, + { + name: "Invalid format", + input: "not a date", + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + var d DateValue + err := d.Parse(tt.input) + + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tt.want, d.String()) + } + }) + } +} + +func TestDateTimeValueParse(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + input string + wantErr bool + }{ + { + name: "RFC3339", + input: "2023-05-15T14:30:45Z", + wantErr: false, + }, + { + name: "ISO with timezone", + input: "2023-05-15T14:30:45+02:00", + wantErr: false, + }, + { + name: "ISO without timezone", + input: "2023-05-15T14:30:45", + wantErr: false, + }, + { + name: "Space separated", + input: "2023-05-15 14:30:45", + wantErr: false, + }, + { + name: "RFC1123", + input: "Mon, 15 May 2023 14:30:45 GMT", + wantErr: false, + }, + { + name: "RFC822", + input: "15 May 23 14:30 GMT", + wantErr: false, + }, + { + name: "ANSIC", + input: "Mon Jan 2 15:04:05 2006", + wantErr: false, + }, + { + name: "Invalid format", + input: "not a datetime", + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + var d DateTimeValue + err := d.Parse(tt.input) + + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + + // Parse the string back to ensure it's valid RFC3339 + _, parseErr := time.Parse(time.RFC3339, d.String()) + assert.NoError(t, parseErr) + } + }) + } +} + +func TestTimeValueParse(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + input string + want string + wantErr bool + }{ + { + name: "24-hour format", + input: "14:30:45", + want: "14:30:45", + wantErr: false, + }, + { + name: "12-hour format with seconds", + input: "2:30:45PM", + want: "14:30:45", + wantErr: false, + }, + { + name: "12-hour format without seconds", + input: "2:30 PM", + want: "14:30:00", + wantErr: false, + }, + { + name: "24-hour without seconds", + input: "14:30", + want: "14:30:00", + wantErr: false, + }, + { + name: "Kitchen format", + input: "2:30PM", + want: "14:30:00", + wantErr: false, + }, + { + name: "Invalid format", + input: "not a time", + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + var tv TimeValue + err := tv.Parse(tt.input) + + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tt.want, tv.String()) + } + }) + } +} + +func TestRequestParams(t *testing.T) { + t.Parallel() + + t.Run("map body type", func(t *testing.T) { + t.Parallel() + + // Create a mock command with flags + cmd := &cli.Command{ + Name: "test", + } + + // Create string flag with body path + stringFlag := &Flag[string]{ + Name: "string-flag", + Default: "default-string", + BodyPath: "string_field", + value: &cliValue[string]{value: "test-value"}, + hasBeenSet: true, + } + + // Create int flag with header path + intFlag := &Flag[int64]{ + Name: "int-flag", + Default: 42, + HeaderPath: "X-Int-Value", + value: &cliValue[int64]{value: 99}, + hasBeenSet: true, + } + + // Create bool flag with query path + boolFlag := &Flag[bool]{ + Name: "bool-flag", + Default: false, + QueryPath: "include_details", + value: &cliValue[bool]{value: true}, + hasBeenSet: true, + } + + // Create date flag with multiple paths + dateFlag := &Flag[DateValue]{ + Name: "date-flag", + Default: DateValue("2023-01-01"), + BodyPath: "effective_date", + HeaderPath: "X-Effective-Date", + QueryPath: "as_of_date", + value: &cliValue[DateValue]{value: DateValue("2023-05-15")}, + hasBeenSet: true, + } + + // Create flag with no path + noPathFlag := &Flag[string]{ + Name: "no-path-flag", + Default: "no-path", + value: &cliValue[string]{value: "no-path-value"}, + hasBeenSet: true, + } + + // Create unset flag + unsetFlag := &Flag[string]{ + Name: "unset-flag", + Default: "unset", + BodyPath: "should_not_appear", + value: &cliValue[string]{value: "unset-value"}, + hasBeenSet: false, + } + + cmd.Flags = []cli.Flag{stringFlag, intFlag, boolFlag, dateFlag, noPathFlag, unsetFlag} + + // Test the RequestParams function + contents := ExtractRequestContents(cmd) + + // Verify query parameters + assert.Equal(t, true, contents.Queries["include_details"]) + assert.Equal(t, DateValue("2023-05-15"), contents.Queries["as_of_date"]) + assert.Len(t, contents.Queries, 2) + + // Verify headers + assert.Equal(t, int64(99), contents.Headers["X-Int-Value"]) + assert.Equal(t, DateValue("2023-05-15"), contents.Headers["X-Effective-Date"]) + assert.Len(t, contents.Headers, 2) + + // Verify body + bodyMap, ok := contents.Body.(map[string]any) + assert.True(t, ok, "Expected body to be map[string]any, got %T", contents.Body) + assert.Equal(t, "test-value", bodyMap["string_field"]) + assert.Equal(t, DateValue("2023-05-15"), bodyMap["effective_date"]) + assert.Len(t, bodyMap, 2) + + // Verify the unset flag didn't make it into the maps + assert.NotContains(t, contents.Body, "should_not_appear") + }) + + t.Run("non-map body type", func(t *testing.T) { + t.Parallel() + + // Create a mock command with flags + cmd := &cli.Command{ + Name: "test", + Flags: []cli.Flag{ + &Flag[int64]{ + Name: "int-body-flag", + Default: 0, + BodyRoot: true, + }, + }, + } + cmd.Set("int-body-flag", "42") + + contents := ExtractRequestContents(cmd) + intBody, ok := contents.Body.(int64) + assert.True(t, ok, "Expected body to be int64, got %T", contents.Body) + assert.Equal(t, int64(42), intBody) + }) +} + +func TestFlagSet(t *testing.T) { + t.Parallel() + + strFlag := &Flag[string]{ + Name: "string-flag", + Default: "default-string", + } + + superstitiousIntFlag := &Flag[int64]{ + Name: "int-flag", + Default: 42, + Validator: func(val int64) error { + if val == 13 { + return fmt.Errorf("Unlucky number!") + } + return nil + }, + } + + boolFlag := &Flag[bool]{ + Name: "bool-flag", + Default: false, + } + + // Test initialization and setting + t.Run("PreParse initialization", func(t *testing.T) { + t.Parallel() + + assert.NoError(t, strFlag.PreParse()) + assert.True(t, strFlag.applied) + assert.Equal(t, "default-string", strFlag.Get()) + }) + + t.Run("Set string flag", func(t *testing.T) { + t.Parallel() + + assert.NoError(t, strFlag.Set("string-flag", "new-value")) + assert.Equal(t, "new-value", strFlag.Get()) + assert.True(t, strFlag.IsSet()) + }) + + t.Run("Set int flag with valid value", func(t *testing.T) { + t.Parallel() + + assert.NoError(t, superstitiousIntFlag.Set("int-flag", "100")) + assert.Equal(t, int64(100), superstitiousIntFlag.Get()) + assert.True(t, superstitiousIntFlag.IsSet()) + }) + + t.Run("Set int flag with invalid value", func(t *testing.T) { + t.Parallel() + + assert.Error(t, superstitiousIntFlag.Set("int-flag", "not-an-int")) + }) + + t.Run("Set int flag with validator failing", func(t *testing.T) { + t.Parallel() + + assert.Error(t, superstitiousIntFlag.Set("int-flag", "13")) + }) + + t.Run("Set bool flag", func(t *testing.T) { + t.Parallel() + + assert.NoError(t, boolFlag.Set("bool-flag", "true")) + assert.Equal(t, true, boolFlag.Get()) + assert.True(t, boolFlag.IsSet()) + }) + + t.Run("Set slice flag with multiple values", func(t *testing.T) { + t.Parallel() + + sliceFlag := &Flag[[]int64]{ + Name: "slice-flag", + Default: []int64{}, + } + + // Initialize the flag + assert.NoError(t, sliceFlag.PreParse()) + + // First set + assert.NoError(t, sliceFlag.Set("slice-flag", "10")) + + // Subsequent setting should append, not replace + assert.NoError(t, sliceFlag.Set("slice-flag", "20")) + assert.NoError(t, sliceFlag.Set("slice-flag", "30")) + + // Verify that we have both values in the slice + result := sliceFlag.Get() + assert.Equal(t, []int64{10, 20, 30}, result) + assert.True(t, sliceFlag.IsSet()) + }) + + t.Run("Set slice flag with a nonempty default", func(t *testing.T) { + t.Parallel() + + sliceFlag := &Flag[[]int64]{ + Name: "slice-flag", + Default: []int64{99, 100}, + } + + assert.NoError(t, sliceFlag.PreParse()) + assert.NoError(t, sliceFlag.Set("slice-flag", "10")) + assert.NoError(t, sliceFlag.Set("slice-flag", "20")) + assert.NoError(t, sliceFlag.Set("slice-flag", "30")) + + // Verify that we have clobbered the default value instead of appending + // to it. + result := sliceFlag.Get() + assert.Equal(t, []int64{10, 20, 30}, result) + assert.True(t, sliceFlag.IsSet()) + }) +} + +func TestParseTimeWithFormats(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + input string + formats []string + wantTime time.Time + wantErr bool + }{ + { + name: "RFC3339 format", + input: "2023-05-15T14:30:45Z", + formats: []string{time.RFC3339}, + wantTime: time.Date(2023, 5, 15, 14, 30, 45, 0, time.UTC), + wantErr: false, + }, + { + name: "Multiple formats - first matches", + input: "2023-05-15", + formats: []string{"2006-01-02", time.RFC3339}, + wantTime: time.Date(2023, 5, 15, 0, 0, 0, 0, time.UTC), + wantErr: false, + }, + { + name: "Multiple formats - second matches", + input: "15/05/2023", + formats: []string{"2006-01-02", "02/01/2006"}, + wantTime: time.Date(2023, 5, 15, 0, 0, 0, 0, time.UTC), + wantErr: false, + }, + { + name: "No matching format", + input: "not a date", + formats: []string{"2006-01-02", time.RFC3339}, + wantTime: time.Time{}, + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + got, err := parseTimeWithFormats(tt.input, tt.formats) + + if tt.wantErr { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.True(t, tt.wantTime.Equal(got), "Expected %v, got %v", tt.wantTime, got) + } + }) + } +} + +func TestYamlHandling(t *testing.T) { + t.Parallel() + + // Test with any value + t.Run("Parse YAML to any", func(t *testing.T) { + t.Parallel() + + cv := &cliValue[any]{} + err := cv.Set("name: test\nvalue: 42\n") + assert.NoError(t, err) + + // The value should be a map + val, ok := cv.Get().(map[string]any) + assert.True(t, ok, "Expected map[string]any, got %T", cv.Get()) + + if ok { + assert.Equal(t, "test", val["name"]) + assert.Equal(t, uint64(42), val["value"]) + } + + // The string representation should be valid YAML + strVal := cv.String() + var parsed map[string]any + err = yaml.Unmarshal([]byte(strVal), &parsed) + assert.NoError(t, err) + assert.Equal(t, "test", parsed["name"]) + assert.Equal(t, uint64(42), parsed["value"]) + }) + + // Test with array + t.Run("Parse YAML array", func(t *testing.T) { + t.Parallel() + + cv := &cliValue[any]{} + err := cv.Set("- item1\n- item2\n- item3\n") + assert.NoError(t, err) + + // The value should be a slice + val, ok := cv.Get().([]any) + assert.True(t, ok, "Expected []any, got %T", cv.Get()) + + if ok { + assert.Len(t, val, 3) + assert.Equal(t, "item1", val[0]) + assert.Equal(t, "item2", val[1]) + assert.Equal(t, "item3", val[2]) + } + }) + + t.Run("Parse @file.txt as YAML", func(t *testing.T) { + t.Parallel() + + flag := &Flag[any]{ + Name: "file-flag", + Default: nil, + } + assert.NoError(t, flag.PreParse()) + assert.NoError(t, flag.Set("file-flag", "@file.txt")) + + val := flag.Get() + assert.Equal(t, "@file.txt", val) + }) + + t.Run("Parse @file.txt list as YAML", func(t *testing.T) { + t.Parallel() + + flag := &Flag[[]any]{ + Name: "file-flag", + Default: nil, + } + assert.NoError(t, flag.PreParse()) + assert.NoError(t, flag.Set("file-flag", "@file1.txt")) + assert.NoError(t, flag.Set("file-flag", "@file2.txt")) + + val := flag.Get() + assert.Equal(t, []any{"@file1.txt", "@file2.txt"}, val) + }) + + t.Run("Parse identifiers as YAML", func(t *testing.T) { + t.Parallel() + + tests := []string{ + "hello", + "e4e355fa-b03b-4c57-a73d-25c9733eec79", + "foo_bar", + "Color.Red", + "aGVsbG8=", + } + for _, test := range tests { + flag := &Flag[any]{ + Name: "flag", + Default: nil, + } + assert.NoError(t, flag.PreParse()) + assert.NoError(t, flag.Set("flag", test)) + + val := flag.Get() + assert.Equal(t, test, val) + } + + for _, test := range tests { + flag := &Flag[[]any]{ + Name: "identifier", + Default: nil, + } + assert.NoError(t, flag.PreParse()) + assert.NoError(t, flag.Set("identifier", test)) + assert.NoError(t, flag.Set("identifier", test)) + + val := flag.Get() + assert.Equal(t, []any{test, test}, val) + } + }) + + // Test with invalid YAML + t.Run("Parse invalid YAML", func(t *testing.T) { + t.Parallel() + + invalidYaml := `[not closed` + cv := &cliValue[any]{} + err := cv.Set(invalidYaml) + assert.Error(t, err) + }) +} + +// TestNullLiteralHandling pins how each Flag[T] type handles the literal value "null" +// when passed via the CLI. Pointer-typed flags serialize nil as JSON null, which is how +// nullable body fields (`anyOf: [T, null]` / `{nullable: true}`) let users clear a field +// via `--foo null`. Non-pointer primitive flags treat "null" as a raw value — these are +// non-nullable schemas where explicit null has no API semantics anyway. +func TestNullLiteralHandling(t *testing.T) { + t.Parallel() + + assertJSONBody := func(t *testing.T, value any, expected string) { + t.Helper() + body, err := json.Marshal(map[string]any{"foo": value}) + assert.NoError(t, err) + assert.JSONEq(t, expected, string(body)) + } + + t.Run("Flag[any] null sends JSON null", func(t *testing.T) { + t.Parallel() + cv := &cliValue[any]{} + assert.NoError(t, cv.Set("null")) + assertJSONBody(t, cv.Get(), `{"foo":null}`) + }) + + t.Run("Flag[string] null is the raw string \"null\"", func(t *testing.T) { + t.Parallel() + cv := &cliValue[string]{} + assert.NoError(t, cv.Set("null")) + assertJSONBody(t, cv.Get(), `{"foo":"null"}`) + }) + + t.Run("Flag[int64] null errors", func(t *testing.T) { + t.Parallel() + cv := &cliValue[int64]{} + assert.Error(t, cv.Set("null")) + }) + + t.Run("Flag[*string] null sends JSON null", func(t *testing.T) { + t.Parallel() + cv := &cliValue[*string]{} + assert.NoError(t, cv.Set("null")) + assertJSONBody(t, cv.Get(), `{"foo":null}`) + }) + + t.Run("Flag[*string] value sends the string", func(t *testing.T) { + t.Parallel() + cv := &cliValue[*string]{} + assert.NoError(t, cv.Set("1.1")) + assertJSONBody(t, cv.Get(), `{"foo":"1.1"}`) + }) + + t.Run("Flag[*int64] null sends JSON null", func(t *testing.T) { + t.Parallel() + cv := &cliValue[*int64]{} + assert.NoError(t, cv.Set("null")) + assertJSONBody(t, cv.Get(), `{"foo":null}`) + }) + + t.Run("Flag[*int64] value sends the integer", func(t *testing.T) { + t.Parallel() + cv := &cliValue[*int64]{} + assert.NoError(t, cv.Set("42")) + assertJSONBody(t, cv.Get(), `{"foo":42}`) + }) + + t.Run("Flag[*int64] invalid value errors", func(t *testing.T) { + t.Parallel() + cv := &cliValue[*int64]{} + assert.Error(t, cv.Set("not-an-int")) + }) + + t.Run("Flag[*bool] null sends JSON null", func(t *testing.T) { + t.Parallel() + cv := &cliValue[*bool]{} + assert.NoError(t, cv.Set("null")) + assertJSONBody(t, cv.Get(), `{"foo":null}`) + }) + + t.Run("Flag[*bool] value sends the boolean", func(t *testing.T) { + t.Parallel() + cv := &cliValue[*bool]{} + assert.NoError(t, cv.Set("true")) + assertJSONBody(t, cv.Get(), `{"foo":true}`) + }) + + t.Run("Flag[*float64] null sends JSON null", func(t *testing.T) { + t.Parallel() + cv := &cliValue[*float64]{} + assert.NoError(t, cv.Set("null")) + assertJSONBody(t, cv.Get(), `{"foo":null}`) + }) + + t.Run("Flag[*float64] value sends the float", func(t *testing.T) { + t.Parallel() + cv := &cliValue[*float64]{} + assert.NoError(t, cv.Set("1.5")) + assertJSONBody(t, cv.Get(), `{"foo":1.5}`) + }) + + t.Run("Flag[*float64] invalid value errors", func(t *testing.T) { + t.Parallel() + cv := &cliValue[*float64]{} + assert.Error(t, cv.Set("not-a-float")) + }) + + t.Run("Flag[*DateValue] null sends JSON null", func(t *testing.T) { + t.Parallel() + cv := &cliValue[*DateValue]{} + assert.NoError(t, cv.Set("null")) + assertJSONBody(t, cv.Get(), `{"foo":null}`) + }) + + t.Run("Flag[*DateValue] value sends the date", func(t *testing.T) { + t.Parallel() + cv := &cliValue[*DateValue]{} + assert.NoError(t, cv.Set("2023-05-15")) + assertJSONBody(t, cv.Get(), `{"foo":"2023-05-15"}`) + }) + + t.Run("Flag[*DateValue] invalid value errors", func(t *testing.T) { + t.Parallel() + cv := &cliValue[*DateValue]{} + assert.Error(t, cv.Set("not-a-date")) + }) + + t.Run("Flag[*DateTimeValue] null sends JSON null", func(t *testing.T) { + t.Parallel() + cv := &cliValue[*DateTimeValue]{} + assert.NoError(t, cv.Set("null")) + assertJSONBody(t, cv.Get(), `{"foo":null}`) + }) + + t.Run("Flag[*DateTimeValue] value sends the datetime", func(t *testing.T) { + t.Parallel() + cv := &cliValue[*DateTimeValue]{} + assert.NoError(t, cv.Set("2023-05-15T14:30:45Z")) + assertJSONBody(t, cv.Get(), `{"foo":"2023-05-15T14:30:45Z"}`) + }) + + t.Run("Flag[*DateTimeValue] invalid value errors", func(t *testing.T) { + t.Parallel() + cv := &cliValue[*DateTimeValue]{} + assert.Error(t, cv.Set("not-a-datetime")) + }) + + t.Run("Flag[*TimeValue] null sends JSON null", func(t *testing.T) { + t.Parallel() + cv := &cliValue[*TimeValue]{} + assert.NoError(t, cv.Set("null")) + assertJSONBody(t, cv.Get(), `{"foo":null}`) + }) + + t.Run("Flag[*TimeValue] value sends the time", func(t *testing.T) { + t.Parallel() + cv := &cliValue[*TimeValue]{} + assert.NoError(t, cv.Set("14:30:45")) + assertJSONBody(t, cv.Get(), `{"foo":"14:30:45"}`) + }) + + t.Run("Flag[*TimeValue] invalid value errors", func(t *testing.T) { + t.Parallel() + cv := &cliValue[*TimeValue]{} + assert.Error(t, cv.Set("not-a-time")) + }) + + // Nullable maps don't need pointer wrapping — a nil map already marshals as JSON null. + t.Run("Flag[map[string]any] null sends JSON null", func(t *testing.T) { + t.Parallel() + cv := &cliValue[map[string]any]{} + assert.NoError(t, cv.Set("null")) + assertJSONBody(t, cv.Get(), `{"foo":null}`) + }) +} + +func TestFlagTypeNames(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + flag cli.DocGenerationFlag + expected string + }{ + {"string", &Flag[string]{}, "string"}, + {"int64", &Flag[int64]{}, "int"}, + {"float64", &Flag[float64]{}, "float"}, + {"bool", &Flag[bool]{}, "boolean"}, + {"string slice", &Flag[[]string]{}, "string"}, + {"date", &Flag[DateValue]{}, "date"}, + {"datetime", &Flag[DateTimeValue]{}, "datetime"}, + {"time", &Flag[TimeValue]{}, "time"}, + {"date slice", &Flag[[]DateValue]{}, "date"}, + {"datetime slice", &Flag[[]DateTimeValue]{}, "datetime"}, + {"time slice", &Flag[[]TimeValue]{}, "time"}, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + typeName := tt.flag.TypeName() + assert.Equal(t, tt.expected, typeName, "Expected type name %q, got %q", tt.expected, typeName) + }) + } +} + +// TestInnerFlagDispatchOnUntypedFlag pins inner-flag behavior for `Flag[any]`, +// which is the codegen output for nullable complex schemas (`anyOf: [T, null]` +// or `{nullable: true}`). The untyped-nil zero value carries no reflect.Kind, +// so SetInnerField has nowhere to dispatch the assignment — without explicit +// help the inner-field value silently drops. +func TestInnerFlagDispatchOnUntypedFlag(t *testing.T) { + t.Parallel() + + t.Run("nullable array of objects appends element from inner flag", func(t *testing.T) { + t.Parallel() + outer := &Flag[any]{Name: "mcp-server"} + assert.NoError(t, outer.PreParse()) + + nameFlag := &InnerFlag[string]{ + Name: "mcp-server.name", InnerField: "name", + OuterFlag: outer, OuterIsArrayOfObjects: true, + } + assert.NoError(t, nameFlag.Set("mcp-server.name", "first")) + + body, err := json.Marshal(map[string]any{"foo": outer.Get()}) + assert.NoError(t, err) + assert.JSONEq(t, `{"foo":[{"name":"first"}]}`, string(body)) + }) + + t.Run("nullable object sets field from inner flag", func(t *testing.T) { + t.Parallel() + outer := &Flag[any]{Name: "metadata"} + assert.NoError(t, outer.PreParse()) + + keyFlag := &InnerFlag[string]{ + Name: "metadata.key", InnerField: "key", OuterFlag: outer, + } + assert.NoError(t, keyFlag.Set("metadata.key", "value")) + + body, err := json.Marshal(map[string]any{"foo": outer.Get()}) + assert.NoError(t, err) + assert.JSONEq(t, `{"foo":{"key":"value"}}`, string(body)) + }) + + t.Run("multiple inner flags merge into the trailing element", func(t *testing.T) { + t.Parallel() + outer := &Flag[any]{Name: "mcp-server"} + assert.NoError(t, outer.PreParse()) + + nameFlag := &InnerFlag[string]{ + Name: "mcp-server.name", InnerField: "name", + OuterFlag: outer, OuterIsArrayOfObjects: true, + } + urlFlag := &InnerFlag[string]{ + Name: "mcp-server.url", InnerField: "url", + OuterFlag: outer, OuterIsArrayOfObjects: true, + } + assert.NoError(t, nameFlag.Set("mcp-server.name", "first")) + assert.NoError(t, urlFlag.Set("mcp-server.url", "https://example.com")) + + body, err := json.Marshal(map[string]any{"foo": outer.Get()}) + assert.NoError(t, err) + assert.JSONEq(t, `{"foo":[{"name":"first","url":"https://example.com"}]}`, string(body)) + }) +} + +func TestApplyStdinDataToFlags(t *testing.T) { + t.Parallel() + + t.Run("sets query path flag from piped data", func(t *testing.T) { + t.Parallel() + + flag := &Flag[string]{ + Name: "account-id", + QueryPath: "account_id", + } + assert.NoError(t, flag.PreParse()) + + data := map[string]any{"account_id": "acct_123"} + cmd := &cli.Command{Flags: []cli.Flag{flag}} + assert.NoError(t, ApplyStdinDataToFlags(cmd, data)) + + assert.True(t, flag.IsSet()) + assert.Equal(t, "acct_123", flag.Get()) + }) + + t.Run("sets header path flag from piped data", func(t *testing.T) { + t.Parallel() + + flag := &Flag[string]{ + Name: "idempotency-key", + HeaderPath: "Idempotency-Key", + } + assert.NoError(t, flag.PreParse()) + + data := map[string]any{"Idempotency-Key": "key-xyz"} + cmd := &cli.Command{Flags: []cli.Flag{flag}} + assert.NoError(t, ApplyStdinDataToFlags(cmd, data)) + + assert.True(t, flag.IsSet()) + assert.Equal(t, "key-xyz", flag.Get()) + }) + + t.Run("does not set body path flag from piped data", func(t *testing.T) { + t.Parallel() + + // Body params are handled by the maps.Copy merge in flagOptions, not by ApplyStdinDataToFlags. + flag := &Flag[string]{ + Name: "message", + BodyPath: "message", + } + assert.NoError(t, flag.PreParse()) + + data := map[string]any{"message": "hello world"} + cmd := &cli.Command{Flags: []cli.Flag{flag}} + assert.NoError(t, ApplyStdinDataToFlags(cmd, data)) + + assert.False(t, flag.IsSet()) + }) + + t.Run("does not override flag already set via CLI", func(t *testing.T) { + t.Parallel() + + flag := &Flag[string]{ + Name: "account-id", + QueryPath: "account_id", + } + assert.NoError(t, flag.PreParse()) + assert.NoError(t, flag.Set("account-id", "explicit_value")) + + data := map[string]any{"account_id": "piped_value"} + cmd := &cli.Command{Flags: []cli.Flag{flag}} + assert.NoError(t, ApplyStdinDataToFlags(cmd, data)) + + // The explicitly-set value should win. + assert.Equal(t, "explicit_value", flag.Get()) + }) + + t.Run("sets integer query flag from piped data", func(t *testing.T) { + t.Parallel() + + flag := &Flag[int64]{ + Name: "page-size", + QueryPath: "page_size", + } + assert.NoError(t, flag.PreParse()) + + data := map[string]any{"page_size": int64(50)} + cmd := &cli.Command{Flags: []cli.Flag{flag}} + assert.NoError(t, ApplyStdinDataToFlags(cmd, data)) + + assert.True(t, flag.IsSet()) + assert.Equal(t, int64(50), flag.Get()) + }) + + t.Run("sets boolean query flag from piped data", func(t *testing.T) { + t.Parallel() + + flag := &Flag[bool]{ + Name: "include-deleted", + QueryPath: "include_deleted", + } + assert.NoError(t, flag.PreParse()) + + data := map[string]any{"include_deleted": true} + cmd := &cli.Command{Flags: []cli.Flag{flag}} + assert.NoError(t, ApplyStdinDataToFlags(cmd, data)) + + assert.True(t, flag.IsSet()) + assert.Equal(t, true, flag.Get()) + }) + + t.Run("resolves query path flag via data alias", func(t *testing.T) { + t.Parallel() + + flag := &Flag[string]{ + Name: "account-id", + QueryPath: "account_id", + DataAliases: []string{"accountId", "account"}, + } + assert.NoError(t, flag.PreParse()) + + // Use one of the aliases as the key in piped data. + data := map[string]any{"accountId": "acct_alias"} + cmd := &cli.Command{Flags: []cli.Flag{flag}} + assert.NoError(t, ApplyStdinDataToFlags(cmd, data)) + + assert.True(t, flag.IsSet()) + assert.Equal(t, "acct_alias", flag.Get()) + }) + + t.Run("does not set body path flag via data alias", func(t *testing.T) { + t.Parallel() + + // Body params are handled by the maps.Copy merge in flagOptions, not by ApplyStdinDataToFlags. + flag := &Flag[string]{ + Name: "user-name", + BodyPath: "user_name", + DataAliases: []string{"userName", "username"}, + } + assert.NoError(t, flag.PreParse()) + + data := map[string]any{"userName": "alice"} + cmd := &cli.Command{Flags: []cli.Flag{flag}} + assert.NoError(t, ApplyStdinDataToFlags(cmd, data)) + + assert.False(t, flag.IsSet()) + }) + + t.Run("ignores flags with no matching key in piped data", func(t *testing.T) { + t.Parallel() + + flag := &Flag[string]{ + Name: "account-id", + QueryPath: "account_id", + } + assert.NoError(t, flag.PreParse()) + + data := map[string]any{"other_key": "value"} + cmd := &cli.Command{Flags: []cli.Flag{flag}} + assert.NoError(t, ApplyStdinDataToFlags(cmd, data)) + + assert.False(t, flag.IsSet()) + }) + + t.Run("ignores flags with no path set", func(t *testing.T) { + t.Parallel() + + flag := &Flag[string]{ + Name: "some-flag", + // No QueryPath, HeaderPath, or BodyPath + } + assert.NoError(t, flag.PreParse()) + + data := map[string]any{"some-flag": "value"} + cmd := &cli.Command{Flags: []cli.Flag{flag}} + assert.NoError(t, ApplyStdinDataToFlags(cmd, data)) + + assert.False(t, flag.IsSet()) + }) + + t.Run("handles multiple flags from piped data", func(t *testing.T) { + t.Parallel() + + accountFlag := &Flag[string]{ + Name: "account-id", + QueryPath: "account_id", + } + limitFlag := &Flag[int64]{ + Name: "limit", + QueryPath: "limit", + } + assert.NoError(t, accountFlag.PreParse()) + assert.NoError(t, limitFlag.PreParse()) + + data := map[string]any{ + "account_id": "acct_abc", + "limit": int64(25), + } + cmd := &cli.Command{Flags: []cli.Flag{accountFlag, limitFlag}} + assert.NoError(t, ApplyStdinDataToFlags(cmd, data)) + + assert.True(t, accountFlag.IsSet()) + assert.Equal(t, "acct_abc", accountFlag.Get()) + assert.True(t, limitFlag.IsSet()) + assert.Equal(t, int64(25), limitFlag.Get()) + }) + + t.Run("sets inner flag from nested piped data under outer body path", func(t *testing.T) { + t.Parallel() + + outer := &Flag[map[string]any]{ + Name: "address", + BodyPath: "address", + } + assert.NoError(t, outer.PreParse()) + + cityInner := &InnerFlag[string]{ + Name: "address.city", + InnerField: "city", + OuterFlag: outer, + } + + data := map[string]any{ + "address": map[string]any{"city": "San Francisco"}, + } + cmd := &cli.Command{Flags: []cli.Flag{outer, cityInner}} + assert.NoError(t, ApplyStdinDataToFlags(cmd, data)) + + // InnerFlag.IsSet() is always false by design; verify the value was written + // into the outer flag's underlying map instead. + outerVal, ok := outer.Get().(map[string]any) + assert.True(t, ok, "expected outer flag value to be map[string]any, got %T", outer.Get()) + assert.Equal(t, "San Francisco", outerVal["city"]) + }) + + t.Run("sets inner flag via data alias in nested piped data", func(t *testing.T) { + t.Parallel() + + outer := &Flag[map[string]any]{ + Name: "address", + BodyPath: "address", + } + assert.NoError(t, outer.PreParse()) + + cityInner := &InnerFlag[string]{ + Name: "address.city", + InnerField: "city", + DataAliases: []string{"cityName"}, + OuterFlag: outer, + } + + // Use the alias in piped data. + data := map[string]any{ + "address": map[string]any{"cityName": "Portland"}, + } + cmd := &cli.Command{Flags: []cli.Flag{outer, cityInner}} + assert.NoError(t, ApplyStdinDataToFlags(cmd, data)) + + // InnerFlag.IsSet() is always false by design; verify the value was written + // into the outer flag's underlying map instead. + outerVal, ok := outer.Get().(map[string]any) + assert.True(t, ok, "expected outer flag value to be map[string]any, got %T", outer.Get()) + assert.Equal(t, "Portland", outerVal["city"]) + }) + + t.Run("does not set inner flag when outer flag has no body path", func(t *testing.T) { + t.Parallel() + + outer := &Flag[map[string]any]{ + Name: "options", + // No BodyPath set + } + assert.NoError(t, outer.PreParse()) + + inner := &InnerFlag[string]{ + Name: "options.key", + InnerField: "key", + OuterFlag: outer, + } + + data := map[string]any{ + "options": map[string]any{"key": "value"}, + } + cmd := &cli.Command{Flags: []cli.Flag{outer, inner}} + assert.NoError(t, ApplyStdinDataToFlags(cmd, data)) + + assert.False(t, inner.IsSet()) + }) + + t.Run("does not set inner flag when piped data has no nested map for outer path", func(t *testing.T) { + t.Parallel() + + outer := &Flag[map[string]any]{ + Name: "address", + BodyPath: "address", + } + assert.NoError(t, outer.PreParse()) + + inner := &InnerFlag[string]{ + Name: "address.city", + InnerField: "city", + OuterFlag: outer, + } + + // The outer body path key is missing from the piped data. + data := map[string]any{"other": "value"} + cmd := &cli.Command{Flags: []cli.Flag{outer, inner}} + assert.NoError(t, ApplyStdinDataToFlags(cmd, data)) + + assert.False(t, inner.IsSet()) + }) + + t.Run("canonical path key takes precedence over alias when both are present", func(t *testing.T) { + t.Parallel() + + flag := &Flag[string]{ + Name: "account-id", + QueryPath: "account_id", + DataAliases: []string{"accountId"}, + } + assert.NoError(t, flag.PreParse()) + + // Both canonical and alias present — canonical should win because it's checked first. + data := map[string]any{ + "account_id": "canonical_value", + "accountId": "alias_value", + } + cmd := &cli.Command{Flags: []cli.Flag{flag}} + assert.NoError(t, ApplyStdinDataToFlags(cmd, data)) + + assert.True(t, flag.IsSet()) + assert.Equal(t, "canonical_value", flag.Get()) + }) + + t.Run("empty data map does not set any flags", func(t *testing.T) { + t.Parallel() + + flag := &Flag[string]{ + Name: "account-id", + QueryPath: "account_id", + } + assert.NoError(t, flag.PreParse()) + + cmd := &cli.Command{Flags: []cli.Flag{flag}} + assert.NoError(t, ApplyStdinDataToFlags(cmd, map[string]any{})) + + assert.False(t, flag.IsSet()) + }) +} diff --git a/pkg/cmd/ai.go b/pkg/cmd/ai.go new file mode 100644 index 0000000..34a436a --- /dev/null +++ b/pkg/cmd/ai.go @@ -0,0 +1,170 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "context" + "fmt" + + "github.com/context-dot-dev/context-dev-cli/internal/apiquery" + "github.com/context-dot-dev/context-dev-cli/internal/requestflag" + "github.com/context-dot-dev/context-go-sdk/v2" + "github.com/context-dot-dev/context-go-sdk/v2/option" + "github.com/tidwall/gjson" + "github.com/urfave/cli/v3" +) + +var aiExtractProduct = cli.Command{ + Name: "extract-product", + Usage: "Given a single URL, determines if it is a product page and extracts the product\ninformation.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "url", + Usage: "The product page URL to extract product data from.", + Required: true, + BodyPath: "url", + }, + &requestflag.Flag[int64]{ + Name: "max-age-ms", + Usage: "Return a cached result if a prior scrape for the same parameters exists and is younger than this many milliseconds. Defaults to 7 days (604800000 ms) when omitted. Max is 30 days (2592000000 ms). Set to 0 to always scrape fresh.", + Default: 604800000, + BodyPath: "maxAgeMs", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional tags for tracking usage. Up to 20 tags, each 1 to 50 characters.", + BodyPath: "tags", + }, + &requestflag.Flag[int64]{ + Name: "timeout-ms", + Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).", + BodyPath: "timeoutMS", + }, + }, + Action: handleAIExtractProduct, + HideHelpCommand: true, +} + +var aiExtractProducts = cli.Command{ + Name: "extract-products", + Usage: "Extract product information from a brand's website. We will analyze the website\nand return a list of products with details such as name, description, image,\npricing, features, and more.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "domain", + Usage: "The domain name to analyze.", + BodyPath: "domain", + }, + &requestflag.Flag[int64]{ + Name: "max-age-ms", + Usage: "Return a cached result if a prior scrape for the same parameters exists and is younger than this many milliseconds. Defaults to 7 days (604800000 ms) when omitted. Max is 30 days (2592000000 ms). Set to 0 to always scrape fresh.", + Default: 604800000, + BodyPath: "maxAgeMs", + }, + &requestflag.Flag[int64]{ + Name: "max-products", + Usage: "Maximum number of products to extract.", + BodyPath: "maxProducts", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional tags for tracking usage. Up to 20 tags, each 1 to 50 characters.", + BodyPath: "tags", + }, + &requestflag.Flag[int64]{ + Name: "timeout-ms", + Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).", + BodyPath: "timeoutMS", + }, + &requestflag.Flag[string]{ + Name: "direct-url", + Usage: "A specific URL to use directly as the starting point for extraction without domain resolution.", + BodyPath: "directUrl", + }, + }, + Action: handleAIExtractProducts, + HideHelpCommand: true, +} + +func handleAIExtractProduct(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + ApplicationJSON, + false, + ) + if err != nil { + return err + } + + params := contextdev.AIExtractProductParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.AI.ExtractProduct(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "ai extract-product", + Transform: transform, + }) +} + +func handleAIExtractProducts(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + ApplicationJSON, + false, + ) + if err != nil { + return err + } + + params := contextdev.AIExtractProductsParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.AI.ExtractProducts(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "ai extract-products", + Transform: transform, + }) +} diff --git a/pkg/cmd/ai_test.go b/pkg/cmd/ai_test.go new file mode 100644 index 0000000..e6ed136 --- /dev/null +++ b/pkg/cmd/ai_test.go @@ -0,0 +1,75 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "testing" + + "github.com/context-dot-dev/context-dev-cli/internal/mocktest" +) + +func TestAIExtractProduct(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "ai", "extract-product", + "--url", "https://example.com", + "--max-age-ms", "0", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1000", + ) + }) + + t.Run("piping data", func(t *testing.T) { + // Test piping YAML data over stdin + pipeData := []byte("" + + "url: https://example.com\n" + + "maxAgeMs: 0\n" + + "tags:\n" + + " - production\n" + + " - team-alpha\n" + + "timeoutMS: 1000\n") + mocktest.TestRunMockTestWithPipeAndFlags( + t, pipeData, + "--api-key", "string", + "ai", "extract-product", + ) + }) +} + +func TestAIExtractProducts(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "ai", "extract-products", + "--domain", "domain", + "--max-age-ms", "0", + "--max-products", "1", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1000", + ) + }) + + t.Run("piping data", func(t *testing.T) { + // Test piping YAML data over stdin + pipeData := []byte("" + + "domain: domain\n" + + "maxAgeMs: 0\n" + + "maxProducts: 1\n" + + "tags:\n" + + " - production\n" + + " - team-alpha\n" + + "timeoutMS: 1000\n") + mocktest.TestRunMockTestWithPipeAndFlags( + t, pipeData, + "--api-key", "string", + "ai", "extract-products", + ) + }) +} diff --git a/pkg/cmd/batch.go b/pkg/cmd/batch.go new file mode 100644 index 0000000..1eff5c6 --- /dev/null +++ b/pkg/cmd/batch.go @@ -0,0 +1,417 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "context" + "fmt" + + "github.com/context-dot-dev/context-dev-cli/internal/apiquery" + "github.com/context-dot-dev/context-dev-cli/internal/requestflag" + "github.com/context-dot-dev/context-go-sdk/v2" + "github.com/context-dot-dev/context-go-sdk/v2/option" + "github.com/tidwall/gjson" + "github.com/urfave/cli/v3" +) + +var batchRetrieve = cli.Command{ + Name: "retrieve", + Usage: "Check progress, and get download links once the batch finishes.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "batch-id", + Usage: "ID of the batch to retrieve or cancel.", + Required: true, + PathParam: "batch_id", + }, + }, + Action: handleBatchRetrieve, + HideHelpCommand: true, +} + +var batchList = cli.Command{ + Name: "list", + Usage: "List your batches from newest to oldest. Filter by status or continue with a\ncursor.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "cursor", + Usage: "Cursor from the previous page.", + QueryPath: "cursor", + }, + &requestflag.Flag[int64]{ + Name: "limit", + Usage: "Batches per page. Defaults to 25.", + QueryPath: "limit", + }, + &requestflag.Flag[string]{ + Name: "q", + Usage: "Free-text search term, matched against the batch id, crawl source (start URL or sitemap domain), and tags.", + QueryPath: "q", + }, + &requestflag.Flag[string]{ + Name: "search-type", + Usage: "`prefix` for as-you-type prefix matching (default), `exact` for full-token matching.", + QueryPath: "search_type", + }, + &requestflag.Flag[string]{ + Name: "status", + Usage: "Filter by status.", + QueryPath: "status", + }, + &requestflag.Flag[string]{ + Name: "tags", + Usage: "Comma-separated list of tags to filter by (matches batches having any of them).", + QueryPath: "tags", + }, + }, + Action: handleBatchList, + HideHelpCommand: true, +} + +var batchDelete = cli.Command{ + Name: "delete", + Usage: "Permanently delete a finished batch and its stored results. Active batches must\nsettle first.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "batch-id", + Usage: "ID of the batch to retrieve or cancel.", + Required: true, + PathParam: "batch_id", + }, + }, + Action: handleBatchDelete, + HideHelpCommand: true, +} + +var batchCancel = cli.Command{ + Name: "cancel", + Usage: "Stop a batch from starting new pages. In-progress pages finish, and unused\ncredits are refunded.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "batch-id", + Usage: "ID of the batch to retrieve or cancel.", + Required: true, + PathParam: "batch_id", + }, + }, + Action: handleBatchCancel, + HideHelpCommand: true, +} + +var batchGetResults = cli.Command{ + Name: "get-results", + Usage: "Page through a finished batch's results as JSON instead of downloading the\nNDJSON files.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "batch-id", + Usage: "ID of the batch to retrieve or cancel.", + Required: true, + PathParam: "batch_id", + }, + &requestflag.Flag[string]{ + Name: "cursor", + Usage: "next_cursor from the previous page.", + QueryPath: "cursor", + }, + &requestflag.Flag[int64]{ + Name: "limit", + Usage: "Records per page. Defaults to 25. A page can close early so its payload stays under ~8 MB; rely on next_cursor rather than counting records.", + QueryPath: "limit", + }, + }, + Action: handleBatchGetResults, + HideHelpCommand: true, +} + +var batchSubmit = cli.Command{ + Name: "submit", + Usage: "Scrape 25K URLs or crawl large websites asynchronously.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[map[string]any]{ + Name: "input", + Usage: "Choose a URL list or a site crawl.", + Required: true, + BodyPath: "input", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Tags stored on the batch. Filter the batch list by them later.", + BodyPath: "tags", + }, + &requestflag.Flag[string]{ + Name: "webhook-url", + Usage: "URL notified when the batch finishes.", + BodyPath: "webhookUrl", + }, + &requestflag.Flag[string]{ + Name: "idempotency-key", + Usage: "Any string unique to this submission. Retries with the same key return the original batch.", + HeaderPath: "Idempotency-Key", + }, + }, + Action: handleBatchSubmit, + HideHelpCommand: true, +} + +func handleBatchRetrieve(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + if !cmd.IsSet("batch-id") && len(unusedArgs) > 0 { + cmd.Set("batch-id", unusedArgs[0]) + unusedArgs = unusedArgs[1:] + } + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Batch.Get(ctx, cmd.Value("batch-id").(string), options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "batch retrieve", + Transform: transform, + }) +} + +func handleBatchList(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + params := contextdev.BatchListParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Batch.List(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "batch list", + Transform: transform, + }) +} + +func handleBatchDelete(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + if !cmd.IsSet("batch-id") && len(unusedArgs) > 0 { + cmd.Set("batch-id", unusedArgs[0]) + unusedArgs = unusedArgs[1:] + } + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Batch.Delete(ctx, cmd.Value("batch-id").(string), options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "batch delete", + Transform: transform, + }) +} + +func handleBatchCancel(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + if !cmd.IsSet("batch-id") && len(unusedArgs) > 0 { + cmd.Set("batch-id", unusedArgs[0]) + unusedArgs = unusedArgs[1:] + } + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Batch.Cancel(ctx, cmd.Value("batch-id").(string), options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "batch cancel", + Transform: transform, + }) +} + +func handleBatchGetResults(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + if !cmd.IsSet("batch-id") && len(unusedArgs) > 0 { + cmd.Set("batch-id", unusedArgs[0]) + unusedArgs = unusedArgs[1:] + } + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + params := contextdev.BatchGetResultsParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Batch.GetResults( + ctx, + cmd.Value("batch-id").(string), + params, + options..., + ) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "batch get-results", + Transform: transform, + }) +} + +func handleBatchSubmit(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + ApplicationJSON, + false, + ) + if err != nil { + return err + } + + params := contextdev.BatchSubmitParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Batch.Submit(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "batch submit", + Transform: transform, + }) +} diff --git a/pkg/cmd/batch_test.go b/pkg/cmd/batch_test.go new file mode 100644 index 0000000..82a5e97 --- /dev/null +++ b/pkg/cmd/batch_test.go @@ -0,0 +1,139 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "testing" + + "github.com/context-dot-dev/context-dev-cli/internal/mocktest" +) + +func TestBatchRetrieve(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "batch", "retrieve", + "--batch-id", "batch_9f2c8a", + ) + }) +} + +func TestBatchList(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "batch", "list", + "--cursor", "cursor", + "--limit", "1", + "--q", "batch_1a2b", + "--search-type", "exact", + "--status", "queued", + "--tags", "docs,competitor", + ) + }) +} + +func TestBatchDelete(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "batch", "delete", + "--batch-id", "batch_9f2c8a", + ) + }) +} + +func TestBatchCancel(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "batch", "cancel", + "--batch-id", "batch_9f2c8a", + ) + }) +} + +func TestBatchGetResults(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "batch", "get-results", + "--batch-id", "batch_9f2c8a", + "--cursor", "cursor", + "--limit", "1", + ) + }) +} + +func TestBatchSubmit(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "batch", "submit", + "--input", "{data: {format: markdown, urls: [{url: https://example.com/products/anvil, itemId: sku-1, meta: {category: bar}}, {url: https://example.com/products/hammer, itemId: sku-2, meta: {foo: bar}}], options: {country: de, excludeSelectors: [x], includeHTML: true, includeImages: true, includeLinks: true, includeSelectors: [x], maxAgeMs: 0, pdf: {end: 1, ocr: true, shouldParse: true, start: 1}, settleAnimations: true, shortenBase64Images: true, useMainContentOnly: true, waitForMs: 0}}, mode: scrape}", + "--tag", "docs", + "--tag", "competitor", + "--webhook-url", "webhookUrl", + "--idempotency-key", "Idempotency-Key", + ) + }) + + t.Run("piping data", func(t *testing.T) { + // Test piping YAML data over stdin + pipeData := []byte("" + + "input:\n" + + " data:\n" + + " format: markdown\n" + + " urls:\n" + + " - url: https://example.com/products/anvil\n" + + " itemId: sku-1\n" + + " meta:\n" + + " category: bar\n" + + " - url: https://example.com/products/hammer\n" + + " itemId: sku-2\n" + + " meta:\n" + + " foo: bar\n" + + " options:\n" + + " country: de\n" + + " excludeSelectors:\n" + + " - x\n" + + " includeHTML: true\n" + + " includeImages: true\n" + + " includeLinks: true\n" + + " includeSelectors:\n" + + " - x\n" + + " maxAgeMs: 0\n" + + " pdf:\n" + + " end: 1\n" + + " ocr: true\n" + + " shouldParse: true\n" + + " start: 1\n" + + " settleAnimations: true\n" + + " shortenBase64Images: true\n" + + " useMainContentOnly: true\n" + + " waitForMs: 0\n" + + " mode: scrape\n" + + "tags:\n" + + " - docs\n" + + " - competitor\n" + + "webhookUrl: webhookUrl\n") + mocktest.TestRunMockTestWithPipeAndFlags( + t, pipeData, + "--api-key", "string", + "batch", "submit", + "--idempotency-key", "Idempotency-Key", + ) + }) +} diff --git a/pkg/cmd/brand.go b/pkg/cmd/brand.go new file mode 100644 index 0000000..249d26f --- /dev/null +++ b/pkg/cmd/brand.go @@ -0,0 +1,315 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "context" + "fmt" + + "github.com/context-dot-dev/context-dev-cli/internal/apiquery" + "github.com/context-dot-dev/context-dev-cli/internal/requestflag" + "github.com/context-dot-dev/context-go-sdk/v2" + "github.com/context-dot-dev/context-go-sdk/v2/option" + "github.com/tidwall/gjson" + "github.com/urfave/cli/v3" +) + +var brandRetrieve = cli.Command{ + Name: "retrieve", + Usage: "Retrieve logos, backdrops, colors, industry, description, and more. Provide\nexactly one lookup identifier in the request body: a domain, company name, email\naddress, stock ticker, transaction descriptor, or direct URL. Note:\n`by_direct_url` fetches brand data only from the provided URL — not from the\nentire internet.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "domain", + Usage: "Domain name to retrieve brand data for (e.g., 'stripe.com').", + BodyPath: "domain", + }, + &requestflag.Flag[string]{ + Name: "type", + Usage: "Discriminator for domain-based brand retrieval.", + Required: true, + BodyPath: "type", + }, + &requestflag.Flag[*string]{ + Name: "force-language", + Usage: `Allowed values: "afrikaans", "albanian", "amharic", "arabic", "armenian", "assamese", "aymara", "azeri", "basque", "belarusian", "bengali", "bosnian", "bulgarian", "burmese", "cantonese", "catalan", "cebuano", "chinese", "corsican", "croatian", "czech", "danish", "dutch", "english", "esperanto", "estonian", "farsi", "fijian", "finnish", "french", "galician", "georgian", "german", "greek", "guarani", "gujarati", "haitian-creole", "hausa", "hawaiian", "hebrew", "hindi", "hmong", "hungarian", "icelandic", "igbo", "indonesian", "irish", "italian", "japanese", "javanese", "kannada", "kazakh", "khmer", "kinyarwanda", "korean", "kurdish", "kyrgyz", "lao", "latin", "latvian", "lingala", "lithuanian", "luxembourgish", "macedonian", "malagasy", "malay", "malayalam", "maltese", "maori", "marathi", "mongolian", "nepali", "norwegian", "odia", "oromo", "pashto", "pidgin", "polish", "portuguese", "punjabi", "quechua", "romanian", "russian", "samoan", "scottish-gaelic", "serbian", "sesotho", "shona", "sindhi", "sinhala", "slovak", "slovene", "somali", "spanish", "sundanese", "swahili", "swedish", "tagalog", "tajik", "tamil", "tatar", "telugu", "thai", "tibetan", "tigrinya", "tongan", "tswana", "turkish", "turkmen", "ukrainian", "urdu", "uyghur", "uzbek", "vietnamese", "welsh", "wolof", "xhosa", "yiddish", "yoruba", "zulu".`, + BodyPath: "force_language", + }, + &requestflag.Flag[int64]{ + Name: "max-age-ms", + Usage: "Maximum age in milliseconds for cached brand data before the API performs a hard refresh. Defaults to 3 months (7776000000 ms). Values below 1 day (86400000 ms) are clamped to 1 day; values above 1 year (31536000000 ms) are clamped to 1 year.", + BodyPath: "maxAgeMs", + }, + &requestflag.Flag[bool]{ + Name: "max-speed", + Usage: "Optional parameter to optimize the API call for maximum speed. When set to true, the API will skip time-consuming operations for faster response at the cost of less comprehensive data.", + BodyPath: "maxSpeed", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional tags for tracking usage. Up to 20 tags, each 1 to 50 characters.", + BodyPath: "tags", + }, + &requestflag.Flag[int64]{ + Name: "timeout-ms", + Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).", + BodyPath: "timeoutMS", + }, + &requestflag.Flag[string]{ + Name: "name", + Usage: "Company name to retrieve brand data for (e.g., 'Apple Inc').", + BodyPath: "name", + }, + &requestflag.Flag[string]{ + Name: "country-gl", + Usage: "Optional country code hint (GL parameter) to specify the country when looking up by company name.", + BodyPath: "country_gl", + }, + &requestflag.Flag[string]{ + Name: "email", + Usage: "Email address to retrieve brand data for (e.g., 'jane@stripe.com').", + BodyPath: "email", + }, + &requestflag.Flag[string]{ + Name: "ticker", + Usage: "Stock ticker symbol to retrieve brand data for (e.g., 'AAPL').", + BodyPath: "ticker", + }, + &requestflag.Flag[string]{ + Name: "ticker-exchange", + Usage: "Optional stock exchange for the ticker. Defaults to NASDAQ if not specified.", + BodyPath: "ticker_exchange", + }, + &requestflag.Flag[string]{ + Name: "direct-url", + Usage: "Full http(s) URL to fetch brand data from (e.g., 'https://stripe.com/enterprise'). Only this URL is fetched — not the entire internet.", + BodyPath: "direct_url", + }, + &requestflag.Flag[string]{ + Name: "transaction-info", + Usage: "Transaction information to identify the brand.", + BodyPath: "transaction_info", + }, + &requestflag.Flag[string]{ + Name: "city", + Usage: "Optional city name to prioritize when searching for the brand.", + BodyPath: "city", + }, + &requestflag.Flag[bool]{ + Name: "high-confidence-only", + Usage: "When set to true, the API performs additional verification to ensure the identified brand matches the transaction with high confidence.", + BodyPath: "high_confidence_only", + }, + &requestflag.Flag[any]{ + Name: "mcc", + Usage: "Optional Merchant Category Code (MCC) to help identify the business category or industry.", + BodyPath: "mcc", + }, + &requestflag.Flag[any]{ + Name: "phone", + Usage: "Optional phone number from the transaction to help verify brand match.", + BodyPath: "phone", + }, + }, + Action: handleBrandRetrieve, + HideHelpCommand: true, +} + +var brandRetrieveSimplified = cli.Command{ + Name: "retrieve-simplified", + Usage: "Returns a simplified version of brand data containing only essential\ninformation: domain, title, colors, logos, and backdrops. Optimized for faster\nresponses and reduced data transfer.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "domain", + Usage: "Domain name to retrieve simplified brand data for", + Required: true, + QueryPath: "domain", + }, + &requestflag.Flag[*int64]{ + Name: "max-age-ms", + Usage: "Maximum age in milliseconds for cached brand data before the API performs a hard refresh. Defaults to 3 months (7776000000 ms). Values below 1 day (86400000 ms) are clamped to 1 day; values above 1 year (31536000000 ms) are clamped to 1 year.", + Default: requestflag.Ptr[int64](7776000000), + QueryPath: "maxAgeMs", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional comma-separated caller-defined tags for tracking this request. Tags are recorded on the request's usage log and can be used to filter usage on the dashboard usage page. Up to 20 tags, each 1-50 characters.", + QueryPath: "tags", + }, + &requestflag.Flag[string]{ + Name: "theme", + Usage: "Optional theme preference used when selecting brand assets.", + QueryPath: "theme", + }, + &requestflag.Flag[int64]{ + Name: "timeout-ms", + Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).", + QueryPath: "timeoutMS", + }, + }, + Action: handleBrandRetrieveSimplified, + HideHelpCommand: true, +} + +var brandSearch = cli.Command{ + Name: "search", + Usage: "Search indexed brands by name or domain", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "query", + Usage: "Search term, matched against the fields selected by queryBy (e.g. 'nike', 'nike.com', 'nik').", + Required: true, + QueryPath: "query", + }, + &requestflag.Flag[bool]{ + Name: "autocomplete", + Usage: "Whether the search term matches by prefix, so partial words match as they are typed (e.g. 'nik' matches Nike). Set to false to match whole words only.", + Default: true, + QueryPath: "autocomplete", + }, + &requestflag.Flag[[]string]{ + Name: "query-by", + Usage: "Fields to match the search term against, as a comma-separated list or repeated parameter: 'name', 'domain', or both. Defaults to both.", + Default: []string{"name", "domain"}, + QueryPath: "queryBy", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional comma-separated caller-defined tags for tracking this request. Tags are recorded on the request's usage log and can be used to filter usage on the dashboard usage page. Up to 20 tags, each 1-50 characters.", + QueryPath: "tags", + }, + &requestflag.Flag[int64]{ + Name: "typo-tolerance", + Usage: "Maximum number of typos tolerated when matching, from 0 to 2. Defaults to 0 (no typo tolerance).", + Default: 0, + QueryPath: "typoTolerance", + }, + }, + Action: handleBrandSearch, + HideHelpCommand: true, +} + +func handleBrandRetrieve(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + ApplicationJSON, + false, + ) + if err != nil { + return err + } + + params := contextdev.BrandGetParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Brand.Get(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "brand retrieve", + Transform: transform, + }) +} + +func handleBrandRetrieveSimplified(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + params := contextdev.BrandGetSimplifiedParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Brand.GetSimplified(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "brand retrieve-simplified", + Transform: transform, + }) +} + +func handleBrandSearch(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + params := contextdev.BrandSearchParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Brand.Search(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "brand search", + Transform: transform, + }) +} diff --git a/pkg/cmd/brand_test.go b/pkg/cmd/brand_test.go new file mode 100644 index 0000000..a46bd9c --- /dev/null +++ b/pkg/cmd/brand_test.go @@ -0,0 +1,81 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "testing" + + "github.com/context-dot-dev/context-dev-cli/internal/mocktest" +) + +func TestBrandRetrieve(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "brand", "retrieve", + "--domain", "xxx", + "--type", "by_domain", + "--force-language", "afrikaans", + "--max-age-ms", "0", + "--max-speed=true", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1000", + ) + }) + + t.Run("piping data", func(t *testing.T) { + // Test piping YAML data over stdin + pipeData := []byte("" + + "domain: xxx\n" + + "type: by_domain\n" + + "force_language: afrikaans\n" + + "maxAgeMs: 0\n" + + "maxSpeed: true\n" + + "tags:\n" + + " - production\n" + + " - team-alpha\n" + + "timeoutMS: 1000\n") + mocktest.TestRunMockTestWithPipeAndFlags( + t, pipeData, + "--api-key", "string", + "brand", "retrieve", + ) + }) +} + +func TestBrandRetrieveSimplified(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "brand", "retrieve-simplified", + "--domain", "xxx", + "--max-age-ms", "0", + "--tag", "production", + "--tag", "team-alpha", + "--theme", "light", + "--timeout-ms", "1000", + ) + }) +} + +func TestBrandSearch(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "brand", "search", + "--query", "x", + "--autocomplete=true", + "--query-by", "name", + "--tag", "production", + "--tag", "team-alpha", + "--typo-tolerance", "0", + ) + }) +} diff --git a/pkg/cmd/cmd.go b/pkg/cmd/cmd.go new file mode 100644 index 0000000..673b8eb --- /dev/null +++ b/pkg/cmd/cmd.go @@ -0,0 +1,278 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "bytes" + "compress/gzip" + "context" + "fmt" + "os" + "path/filepath" + "slices" + "strings" + + "github.com/context-dot-dev/context-dev-cli/internal/autocomplete" + "github.com/context-dot-dev/context-dev-cli/internal/requestflag" + docs "github.com/urfave/cli-docs/v3" + "github.com/urfave/cli/v3" +) + +var ( + Command *cli.Command + CommandErrorBuffer bytes.Buffer +) + +func init() { + Command = &cli.Command{ + Name: "context-dev", + Usage: "CLI for the context.dev API", + Suggest: true, + Version: Version, + ErrWriter: &CommandErrorBuffer, + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "debug", + Usage: "Enable debug logging", + }, + &cli.StringFlag{ + Name: "base-url", + DefaultText: "url", + Usage: "Override the base URL for API requests", + Validator: func(baseURL string) error { + return ValidateBaseURL(baseURL, "--base-url") + }, + }, + &cli.StringFlag{ + Name: "format", + Usage: "The format for displaying response data (one of: " + strings.Join(OutputFormats, ", ") + ")", + Value: "auto", + Validator: func(format string) error { + if !slices.Contains(OutputFormats, strings.ToLower(format)) { + return fmt.Errorf("format must be one of: %s", strings.Join(OutputFormats, ", ")) + } + return nil + }, + }, + &cli.StringFlag{ + Name: "format-error", + Usage: "The format for displaying error data (one of: " + strings.Join(OutputFormats, ", ") + ")", + Value: "auto", + Validator: func(format string) error { + if !slices.Contains(OutputFormats, strings.ToLower(format)) { + return fmt.Errorf("format must be one of: %s", strings.Join(OutputFormats, ", ")) + } + return nil + }, + }, + &cli.StringFlag{ + Name: "transform", + Usage: "The GJSON transformation for data output.", + }, + &cli.StringFlag{ + Name: "transform-error", + Usage: "The GJSON transformation for errors.", + }, + &cli.BoolFlag{ + Name: "raw-output", + Aliases: []string{"r"}, + Usage: "If the result is a string, print it without JSON quotes. This can be useful for making output transforms talk to non-JSON-based systems.", + }, + &requestflag.Flag[string]{ + Name: "api-key", + Sources: cli.EnvVars("CONTEXT_DEV_API_KEY"), + }, + }, + Commands: []*cli.Command{ + { + Name: "parse", + Category: "API RESOURCE", + Suggest: true, + Commands: []*cli.Command{ + &parseHandle, + }, + }, + { + Name: "web", + Category: "API RESOURCE", + Suggest: true, + Commands: []*cli.Command{ + &webExtract, + &webExtractCompetitors, + &webExtractFonts, + &webExtractStyleguide, + &webScreenshot, + &webSearch, + &webWebCrawlMd, + &webWebScrapeHTML, + &webWebScrapeImages, + &webWebScrapeMd, + &webWebScrapeSitemap, + }, + }, + { + Name: "ai", + Category: "API RESOURCE", + Suggest: true, + Commands: []*cli.Command{ + &aiExtractProduct, + &aiExtractProducts, + }, + }, + { + Name: "brand", + Category: "API RESOURCE", + Suggest: true, + Commands: []*cli.Command{ + &brandRetrieve, + &brandRetrieveSimplified, + &brandSearch, + }, + }, + { + Name: "industry", + Category: "API RESOURCE", + Suggest: true, + Commands: []*cli.Command{ + &industryRetrieveNaics, + &industryRetrieveSic, + }, + }, + { + Name: "utility", + Category: "API RESOURCE", + Suggest: true, + Commands: []*cli.Command{ + &utilityPrefetch, + }, + }, + { + Name: "monitors", + Category: "API RESOURCE", + Suggest: true, + Commands: []*cli.Command{ + &monitorsCreate, + &monitorsRetrieve, + &monitorsUpdate, + &monitorsList, + &monitorsDelete, + &monitorsGetCreditUsage, + &monitorsGetLimits, + &monitorsListAccountChanges, + &monitorsListAccountRuns, + &monitorsListChanges, + &monitorsListRuns, + &monitorsRetrieveChange, + &monitorsRun, + }, + }, + { + Name: "batch", + Category: "API RESOURCE", + Suggest: true, + Commands: []*cli.Command{ + &batchRetrieve, + &batchList, + &batchDelete, + &batchCancel, + &batchGetResults, + &batchSubmit, + }, + }, + { + Name: "people", + Category: "API RESOURCE", + Suggest: true, + Commands: []*cli.Command{ + &peopleEnrich, + }, + }, + { + Name: "news", + Category: "API RESOURCE", + Suggest: true, + Commands: []*cli.Command{ + &newsSearch, + }, + }, + { + Name: "@manpages", + Usage: "Generate documentation for 'man'", + UsageText: "context-dev @manpages [-o context-dev.1] [--gzip]", + Hidden: true, + Action: generateManpages, + HideHelpCommand: true, + Flags: []cli.Flag{ + &cli.StringFlag{ + Name: "output", + Aliases: []string{"o"}, + Usage: "write manpages to the given folder", + Value: "man", + }, + &cli.BoolFlag{ + Name: "gzip", + Aliases: []string{"z"}, + Usage: "output gzipped manpage files to .gz", + Value: true, + }, + &cli.BoolFlag{ + Name: "text", + Aliases: []string{"z"}, + Usage: "output uncompressed text files", + Value: false, + }, + }, + }, + { + Name: "__complete", + Hidden: true, + HideHelpCommand: true, + Action: autocomplete.ExecuteShellCompletion, + }, + { + Name: "@completion", + Hidden: true, + HideHelpCommand: true, + Action: autocomplete.OutputCompletionScript, + }, + }, + HideHelpCommand: true, + } +} + +func generateManpages(ctx context.Context, c *cli.Command) error { + manpage, err := docs.ToManWithSection(Command, 1) + if err != nil { + return err + } + dir := c.String("output") + err = os.MkdirAll(filepath.Join(dir, "man1"), 0755) + if err != nil { + // handle error + } + if c.Bool("text") { + file, err := os.Create(filepath.Join(dir, "man1", "context-dev.1")) + if err != nil { + return err + } + defer file.Close() + if _, err := file.WriteString(manpage); err != nil { + return err + } + } + if c.Bool("gzip") { + file, err := os.Create(filepath.Join(dir, "man1", "context-dev.1.gz")) + if err != nil { + return err + } + defer file.Close() + gzWriter := gzip.NewWriter(file) + defer gzWriter.Close() + _, err = gzWriter.Write([]byte(manpage)) + if err != nil { + return err + } + } + fmt.Printf("Wrote manpages to %s\n", dir) + return nil +} diff --git a/pkg/cmd/cmdutil.go b/pkg/cmd/cmdutil.go new file mode 100644 index 0000000..0867816 --- /dev/null +++ b/pkg/cmd/cmdutil.go @@ -0,0 +1,525 @@ +package cmd + +import ( + "bytes" + "encoding/json" + "fmt" + "io" + "log" + "mime" + "net/http" + "net/http/httputil" + "os" + "os/exec" + "os/signal" + "path/filepath" + "strings" + "syscall" + + "github.com/context-dot-dev/context-dev-cli/internal/jsonview" + "github.com/context-dot-dev/context-go-sdk/v2/option" + + "github.com/charmbracelet/x/term" + "github.com/itchyny/json2yaml" + "github.com/muesli/reflow/wrap" + "github.com/tidwall/gjson" + "github.com/tidwall/pretty" + "github.com/urfave/cli/v3" +) + +var OutputFormats = []string{"auto", "explore", "json", "jsonl", "pretty", "raw", "yaml"} + +// ValidateBaseURL checks that a base URL is correctly prefixed with a protocol scheme and produces a better +// error message than the person would see otherwise if it doesn't. +func ValidateBaseURL(value, source string) error { + if value != "" && !strings.HasPrefix(value, "http://") && !strings.HasPrefix(value, "https://") { + return fmt.Errorf("%s %q is missing a scheme (expected http:// or https://)", source, value) + } + return nil +} + +func getDefaultRequestOptions(cmd *cli.Command) []option.RequestOption { + opts := []option.RequestOption{ + option.WithHeader("User-Agent", fmt.Sprintf("ContextDev/CLI %s", Version)), + option.WithHeader("X-Stainless-Lang", "cli"), + option.WithHeader("X-Stainless-Package-Version", Version), + option.WithHeader("X-Stainless-Runtime", "cli"), + option.WithHeader("X-Stainless-CLI-Command", cmd.FullName()), + } + if cmd.IsSet("api-key") { + opts = append(opts, option.WithAPIKey(cmd.String("api-key"))) + } + + // Override base URL if the --base-url flag is provided + if baseURL := cmd.String("base-url"); baseURL != "" { + opts = append(opts, option.WithBaseURL(baseURL)) + } + + return opts +} + +var debugMiddlewareOption = option.WithMiddleware( + func(r *http.Request, mn option.MiddlewareNext) (*http.Response, error) { + logger := log.Default() + + if reqBytes, err := httputil.DumpRequest(r, true); err == nil { + logger.Printf("Request Content:\n%s\n", reqBytes) + } + + resp, err := mn(r) + if err != nil { + return resp, err + } + + if respBytes, err := httputil.DumpResponse(resp, true); err == nil { + logger.Printf("Response Content:\n%s\n", respBytes) + } + + return resp, err + }, +) + +// isInputPiped tries to check for input being piped into the CLI which tells us that we should try to read +// from stdin. This can be a bit tricky in some cases like when an stdin is connected to a pipe but nothing is +// being piped in (this may happen in some environments like Cursor's integration terminal or CI), which is +// why this function is a little more elaborate than it'd be otherwise. +func isInputPiped() bool { + stat, err := os.Stdin.Stat() + if err != nil { + return false + } + + mode := stat.Mode() + + // Regular file (redirect like < file.txt) — only if non-empty. + // + // Notably, on Unix the case like `< /dev/null` is handled below because `/dev/null` is not a regular + // file. On Windows, NUL appears as a regular file with size 0, so it's also handled correctly. + if mode.IsRegular() && stat.Size() > 0 { + return true + } + + // For pipes/sockets (e.g. `echo foo | stainlesscli`), use an OS-specific check to determine whether + // data is actually available. Some environments like Cursor's integrated terminal connect stdin as a + // pipe even when nothing is being piped. + if mode&(os.ModeNamedPipe|os.ModeSocket) != 0 { + // Defined in either cmdutil_unix.go or cmdutil_windows.go. + return isPipedDataAvailableOSSpecific() + } + + return false +} + +func isTerminal(w io.Writer) bool { + switch v := w.(type) { + case *os.File: + return term.IsTerminal(v.Fd()) + default: + return false + } +} + +func streamOutput(label string, generateOutput func(w *os.File) error) error { + // For non-tty output (probably a pipe), write directly to stdout + if !isTerminal(os.Stdout) { + return streamToStdout(generateOutput) + } + + // When streaming output on Unix-like systems, there's a special trick involving creating two socket pairs + // that we prefer because it supports small buffer sizes which results in less pagination per buffer. The + // constructs needed to run it don't exist on Windows builds, so we have this function broken up into + // OS-specific files with conditional build comments. Under Windows (and in case our fancy constructs fail + // on Unix), we fall back to using pipes (`streamToPagerWithPipe`), which are OS agnostic. + // + // Defined in either cmdutil_unix.go or cmdutil_windows.go. + return streamOutputOSSpecific(label, generateOutput) +} + +func streamToPagerWithPipe(label string, generateOutput func(w *os.File) error) error { + r, w, err := os.Pipe() + if err != nil { + return err + } + defer r.Close() + defer w.Close() + + pagerProgram := os.Getenv("PAGER") + if pagerProgram == "" { + pagerProgram = "less" + } + + if _, err := exec.LookPath(pagerProgram); err != nil { + return err + } + + cmd := exec.Command(pagerProgram) + cmd.Stdin = r + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Env = append(os.Environ(), + "LESS=-X -r -P "+label, + "MORE=-r -P "+label, + ) + + if err := cmd.Start(); err != nil { + return err + } + + if err := r.Close(); err != nil { + return err + } + + // If we would be streaming to a terminal and aren't forcing color one way + // or the other, we should configure things to use color so the pager gets + // colorized input. + if isTerminal(os.Stdout) && os.Getenv("FORCE_COLOR") == "" { + os.Setenv("FORCE_COLOR", "1") + } + + if err := generateOutput(w); err != nil && !strings.Contains(err.Error(), "broken pipe") { + return err + } + + w.Close() + return cmd.Wait() +} + +func streamToStdout(generateOutput func(w *os.File) error) error { + signal.Ignore(syscall.SIGPIPE) + err := generateOutput(os.Stdout) + if err != nil && strings.Contains(err.Error(), "broken pipe") { + return nil + } + return err +} + +// writeBinaryResponse writes a binary response to stdout or a file. +// +// Takes in a stdout reference so we can test this function without overriding os.Stdout in tests. +func writeBinaryResponse(response *http.Response, stdout io.Writer, outfile string) (string, error) { + defer response.Body.Close() + body, err := io.ReadAll(response.Body) + if err != nil { + return "", err + } + switch outfile { + case "-", "/dev/stdout": + _, err := stdout.Write(body) + return "", err + case "": + // If output file is unspecified, then print to stdout for plain text or + // if stdout is not a terminal: + if !isTerminal(os.Stdout) || isUTF8TextFile(body) { + _, err := stdout.Write(body) + return "", err + } + + // If response has a suggested filename in the content-disposition + // header, then use that (with an optional suffix to ensure uniqueness): + file, err := createDownloadFile(response, body) + if err != nil { + return "", err + } + defer file.Close() + if _, err := file.Write(body); err != nil { + return "", err + } + return fmt.Sprintf("Wrote output to: %s", file.Name()), nil + default: + if err := os.WriteFile(outfile, body, 0644); err != nil { + return "", err + } + return fmt.Sprintf("Wrote output to: %s", outfile), nil + } +} + +// Return a writable file handle to a new file, which attempts to choose a good filename +// based on the Content-Disposition header or sniffing the MIME filetype of the response. +func createDownloadFile(response *http.Response, data []byte) (*os.File, error) { + filename := "file" + // If the header provided an output filename, use that + disp := response.Header.Get("Content-Disposition") + _, params, err := mime.ParseMediaType(disp) + if err == nil { + if dispFilename, ok := params["filename"]; ok { + // Only use the last path component to prevent directory traversal + filename = filepath.Base(dispFilename) + // Try to create the file with exclusive flag to avoid race conditions + file, err := os.OpenFile(filename, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644) + if err == nil { + return file, nil + } + } + } + + // If file already exists, create a unique filename using CreateTemp + ext := filepath.Ext(filename) + if ext == "" { + ext = guessExtension(data) + } + base := strings.TrimSuffix(filename, ext) + return os.CreateTemp(".", base+"-*"+ext) +} + +func guessExtension(data []byte) string { + ct := http.DetectContentType(data) + + // Prefer common extensions over obscure ones + switch ct { + case "application/gzip": + return ".gz" + case "application/pdf": + return ".pdf" + case "application/zip": + return ".zip" + case "audio/mpeg": + return ".mp3" + case "image/bmp": + return ".bmp" + case "image/gif": + return ".gif" + case "image/jpeg": + return ".jpg" + case "image/png": + return ".png" + case "image/webp": + return ".webp" + case "video/mp4": + return ".mp4" + } + + exts, err := mime.ExtensionsByType(ct) + if err == nil && len(exts) > 0 { + return exts[0] + } else if isUTF8TextFile(data) { + return ".txt" + } else { + return ".bin" + } +} + +func shouldUseColors(w io.Writer) bool { + force, ok := os.LookupEnv("FORCE_COLOR") + if ok { + if force == "1" { + return true + } + if force == "0" { + return false + } + } + return isTerminal(w) +} + +func formatJSON(res gjson.Result, opts ShowJSONOpts) ([]byte, error) { + if opts.Transform != "" { + transformed := res.Get(opts.Transform) + if transformed.Exists() { + res = transformed + } + } + // Modeled after `jq -r` (`--raw-output`): if the result is a string, print it without JSON quotes so that + // it's easier to pipe into other programs. + if opts.RawOutput && res.Type == gjson.String { + return []byte(res.Str + "\n"), nil + } + switch strings.ToLower(opts.Format) { + case "auto": + autoOpts := opts + autoOpts.Format = "json" + autoOpts.Transform = "" + return formatJSON(res, autoOpts) + case "pretty": + return []byte(jsonview.RenderJSON(opts.Title, res) + "\n"), nil + case "json": + prettyJSON := pretty.Pretty([]byte(res.Raw)) + if shouldUseColors(opts.Stdout) { + return pretty.Color(prettyJSON, pretty.TerminalStyle), nil + } else { + return prettyJSON, nil + } + case "jsonl": + // @ugly is gjson syntax for "no whitespace", so it fits on one line + oneLineJSON := res.Get("@ugly").Raw + if shouldUseColors(opts.Stdout) { + bytes := append(pretty.Color([]byte(oneLineJSON), pretty.TerminalStyle), '\n') + return bytes, nil + } else { + return []byte(oneLineJSON + "\n"), nil + } + case "raw": + return []byte(res.Raw + "\n"), nil + case "yaml": + input := strings.NewReader(res.Raw) + var yaml strings.Builder + if err := json2yaml.Convert(&yaml, input); err != nil { + return nil, err + } + _, err := opts.Stdout.Write([]byte(yaml.String())) + return nil, err + default: + return nil, fmt.Errorf("Invalid format: %s, valid formats are: %s", opts.Format, strings.Join(OutputFormats, ", ")) + } +} + +const warningExploreNotSupported = "Warning: Output format 'explore' not supported for non-terminal output; falling back to 'json'\n" + +// ShowJSONOpts configures how JSON output is displayed. +type ShowJSONOpts struct { + ExplicitFormat bool // true if the user explicitly passed --format + Format string // output format (auto, explore, json, jsonl, pretty, raw, yaml) + RawOutput bool // like jq -r: print strings without JSON quotes + Stderr io.Writer // stderr for warnings; injectable for testing; defaults to os.Stderr + Stdout *os.File // stdout (or pager); injectable for testing; defaults to os.Stdout + Title string // display title + Transform string // GJSON path to extract before displaying +} + +func (o *ShowJSONOpts) setDefaults() { + if o.Stderr == nil { + o.Stderr = os.Stderr + } + if o.Stdout == nil { + o.Stdout = os.Stdout + } +} + +// ShowJSON displays a single JSON result to the user. +func ShowJSON(res gjson.Result, opts ShowJSONOpts) error { + opts.setDefaults() + + switch strings.ToLower(opts.Format) { + case "auto": + autoOpts := opts + autoOpts.Format = "json" + return ShowJSON(res, autoOpts) + case "explore": + if !isTerminal(opts.Stdout) { + if opts.ExplicitFormat { + fmt.Fprint(opts.Stderr, warningExploreNotSupported) + } + jsonOpts := opts + jsonOpts.Format = "json" + return ShowJSON(res, jsonOpts) + } + if opts.Transform != "" { + transformed := res.Get(opts.Transform) + if transformed.Exists() { + res = transformed + } + } + return jsonview.ExploreJSON(opts.Title, res) + default: + bytes, err := formatJSON(res, opts) + if err != nil { + return err + } + + _, err = opts.Stdout.Write(bytes) + return err + } +} + +// Get the number of lines that would be output by writing the data to the terminal +func countTerminalLines(data []byte, terminalWidth int) int { + return bytes.Count([]byte(wrap.String(string(data), terminalWidth)), []byte("\n")) +} + +type hasRawJSON interface { + RawJSON() string +} + +// ShowJSONIterator displays an iterator of values to the user. Use itemsToDisplay = -1 for no limit. +func ShowJSONIterator[T any](iter jsonview.Iterator[T], itemsToDisplay int64, opts ShowJSONOpts) error { + opts.setDefaults() + + if opts.Format == "explore" { + if isTerminal(opts.Stdout) { + return jsonview.ExploreJSONStream(opts.Title, iter) + } + if opts.ExplicitFormat { + fmt.Fprint(opts.Stderr, warningExploreNotSupported) + } + opts.Format = "json" + } + + terminalWidth, terminalHeight, err := term.GetSize(os.Stdout.Fd()) + if err != nil { + terminalWidth = 100 + terminalHeight = 40 + } + + // Decide whether or not to use a pager based on whether it's a short output or a long output + usePager := false + output := []byte{} + numberOfNewlines := 0 + // -1 is used to signal no limit of items to display + for itemsToDisplay != 0 && iter.Next() { + item := iter.Current() + var obj gjson.Result + if hasRaw, ok := any(item).(hasRawJSON); ok { + obj = gjson.Parse(hasRaw.RawJSON()) + } else { + jsonData, err := json.Marshal(item) + if err != nil { + return err + } + obj = gjson.ParseBytes(jsonData) + } + json, err := formatJSON(obj, opts) + if err != nil { + return err + } + + output = append(output, json...) + itemsToDisplay -= 1 + numberOfNewlines += countTerminalLines(json, terminalWidth) + + // If the output won't fit in the terminal window, stream it to a pager + if numberOfNewlines >= terminalHeight-3 { + usePager = true + break + } + } + + if !usePager { + _, err := opts.Stdout.Write(output) + if err != nil { + return err + } + + return iter.Err() + } + + return streamOutput(opts.Title, func(pager *os.File) error { + _, err := pager.Write(output) + if err != nil { + return err + } + + pagerOpts := opts + pagerOpts.Stdout = pager + + for iter.Next() { + if itemsToDisplay == 0 { + break + } + item := iter.Current() + var obj gjson.Result + if hasRaw, ok := any(item).(hasRawJSON); ok { + obj = gjson.Parse(hasRaw.RawJSON()) + } else { + jsonData, err := json.Marshal(item) + if err != nil { + return err + } + obj = gjson.ParseBytes(jsonData) + } + if err := ShowJSON(obj, pagerOpts); err != nil { + return err + } + itemsToDisplay -= 1 + } + return iter.Err() + }) +} diff --git a/pkg/cmd/cmdutil_test.go b/pkg/cmd/cmdutil_test.go new file mode 100644 index 0000000..1ecb22c --- /dev/null +++ b/pkg/cmd/cmdutil_test.go @@ -0,0 +1,388 @@ +package cmd + +import ( + "bytes" + "io" + "net/http" + "os" + "path/filepath" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "github.com/tidwall/gjson" + + "github.com/context-dot-dev/context-dev-cli/internal/jsonview" +) + +func TestStreamOutput(t *testing.T) { + t.Setenv("PAGER", "cat") + err := streamOutput("stream test", func(w *os.File) error { + _, writeErr := w.WriteString("Hello world\n") + return writeErr + }) + if err != nil { + t.Errorf("streamOutput failed: %v", err) + } +} + +func TestWriteBinaryResponse(t *testing.T) { + t.Run("write to explicit file", func(t *testing.T) { + tmpDir := t.TempDir() + outfile := tmpDir + "/output.txt" + body := []byte("test content") + resp := &http.Response{ + Body: io.NopCloser(bytes.NewReader(body)), + } + + msg, err := writeBinaryResponse(resp, os.Stdout, outfile) + + require.NoError(t, err) + assert.Contains(t, msg, outfile) + + content, err := os.ReadFile(outfile) + require.NoError(t, err) + assert.Equal(t, body, content) + }) + + t.Run("write to stdout", func(t *testing.T) { + t.Parallel() + + var buf bytes.Buffer + body := []byte("stdout content") + resp := &http.Response{ + Body: io.NopCloser(bytes.NewReader(body)), + } + msg, err := writeBinaryResponse(resp, &buf, "-") + + require.NoError(t, err) + assert.Empty(t, msg) + assert.Equal(t, body, buf.Bytes()) + }) +} + +func TestCreateDownloadFile(t *testing.T) { + t.Run("creates file with filename from header", func(t *testing.T) { + t.Chdir(t.TempDir()) + + resp := &http.Response{ + Header: http.Header{ + "Content-Disposition": []string{`attachment; filename="test.txt"`}, + }, + } + file, err := createDownloadFile(resp, []byte("test content")) + require.NoError(t, err) + defer file.Close() + assert.Equal(t, "test.txt", filepath.Base(file.Name())) + + // Create a second file with the same name to ensure it doesn't clobber the first + resp2 := &http.Response{ + Header: http.Header{ + "Content-Disposition": []string{`attachment; filename="test.txt"`}, + }, + } + file2, err := createDownloadFile(resp2, []byte("second content")) + require.NoError(t, err) + defer file2.Close() + assert.NotEqual(t, file.Name(), file2.Name(), "second file should have a different name") + assert.Contains(t, filepath.Base(file2.Name()), "test") + }) + + t.Run("creates temp file when no header", func(t *testing.T) { + t.Chdir(t.TempDir()) + + resp := &http.Response{Header: http.Header{}} + file, err := createDownloadFile(resp, []byte("test content")) + require.NoError(t, err) + defer file.Close() + assert.Contains(t, filepath.Base(file.Name()), "file-") + }) + + t.Run("prevents directory traversal", func(t *testing.T) { + t.Chdir(t.TempDir()) + + resp := &http.Response{ + Header: http.Header{ + "Content-Disposition": []string{`attachment; filename="../../../etc/passwd"`}, + }, + } + file, err := createDownloadFile(resp, []byte("test content")) + require.NoError(t, err) + defer file.Close() + assert.Equal(t, "passwd", filepath.Base(file.Name())) + }) +} + +func TestValidateBaseURL(t *testing.T) { + t.Parallel() + + t.Run("ValidHTTPS", func(t *testing.T) { + t.Parallel() + + require.NoError(t, ValidateBaseURL("https://api.example.com", "--base-url")) + }) + + t.Run("ValidHTTP", func(t *testing.T) { + t.Parallel() + + require.NoError(t, ValidateBaseURL("http://localhost:8080", "--base-url")) + }) + + t.Run("Empty", func(t *testing.T) { + t.Parallel() + + require.NoError(t, ValidateBaseURL("", "MY_BASE_URL")) + }) + + t.Run("MissingScheme", func(t *testing.T) { + t.Parallel() + + err := ValidateBaseURL("localhost:8080", "MY_BASE_URL") + require.Error(t, err) + assert.Contains(t, err.Error(), "MY_BASE_URL") + assert.Contains(t, err.Error(), "missing a scheme") + }) + + t.Run("HostOnly", func(t *testing.T) { + t.Parallel() + + err := ValidateBaseURL("api.example.com", "--base-url") + require.Error(t, err) + assert.Contains(t, err.Error(), "--base-url") + }) +} + +func TestFormatJSON(t *testing.T) { + t.Parallel() + + t.Run("RawWithTransform", func(t *testing.T) { + t.Parallel() + + res := gjson.Parse(`{"id":"abc123","name":"test"}`) + formatted, err := formatJSON(res, ShowJSONOpts{Format: "raw", Stdout: os.Stdout, Transform: "id"}) + require.NoError(t, err) + require.Equal(t, `"abc123"`+"\n", string(formatted)) + }) + + t.Run("RawWithoutTransform", func(t *testing.T) { + t.Parallel() + + res := gjson.Parse(`{"id":"abc123","name":"test"}`) + formatted, err := formatJSON(res, ShowJSONOpts{Format: "raw", Stdout: os.Stdout}) + require.NoError(t, err) + require.Equal(t, `{"id":"abc123","name":"test"}`+"\n", string(formatted)) + }) + + t.Run("RawWithNestedTransform", func(t *testing.T) { + t.Parallel() + + res := gjson.Parse(`{"data":{"items":[1,2,3]}}`) + formatted, err := formatJSON(res, ShowJSONOpts{Format: "raw", Stdout: os.Stdout, Transform: "data.items"}) + require.NoError(t, err) + require.Equal(t, "[1,2,3]\n", string(formatted)) + }) + + t.Run("RawWithNonexistentTransform", func(t *testing.T) { + t.Parallel() + + res := gjson.Parse(`{"id":"abc123"}`) + formatted, err := formatJSON(res, ShowJSONOpts{Format: "raw", Stdout: os.Stdout, Transform: "missing"}) + require.NoError(t, err) + // Transform path doesn't exist, so original result is returned + require.Equal(t, `{"id":"abc123"}`+"\n", string(formatted)) + }) + + t.Run("RawOutputString", func(t *testing.T) { + t.Parallel() + + res := gjson.Parse(`{"id":"abc123","name":"test"}`) + formatted, err := formatJSON(res, ShowJSONOpts{Format: "json", Stdout: os.Stdout, Transform: "id", RawOutput: true}) + require.NoError(t, err) + require.Equal(t, "abc123\n", string(formatted)) + }) + + t.Run("RawOutputNonString", func(t *testing.T) { + t.Parallel() + + // --raw-output has no effect on non-string values + res := gjson.Parse(`{"count":42}`) + formatted, err := formatJSON(res, ShowJSONOpts{Format: "raw", Stdout: os.Stdout, Transform: "count", RawOutput: true}) + require.NoError(t, err) + require.Equal(t, "42\n", string(formatted)) + }) + + t.Run("RawOutputObject", func(t *testing.T) { + t.Parallel() + + // --raw-output has no effect on objects + res := gjson.Parse(`{"nested":{"a":1}}`) + formatted, err := formatJSON(res, ShowJSONOpts{Format: "raw", Stdout: os.Stdout, Transform: "nested", RawOutput: true}) + require.NoError(t, err) + require.Equal(t, `{"a":1}`+"\n", string(formatted)) + }) +} + +func TestShowJSONIterator(t *testing.T) { + t.Parallel() + + t.Run("RawMultipleItems", func(t *testing.T) { + t.Parallel() + + iter := &sliceIterator[map[string]any]{items: []map[string]any{ + {"id": "abc", "name": "first"}, + {"id": "def", "name": "second"}, + }} + captured := captureShowJSONIterator(t, iter, "raw", "", -1) + assert.Equal(t, `{"id":"abc","name":"first"}`+"\n"+`{"id":"def","name":"second"}`+"\n", captured) + }) + + t.Run("RawWithTransform", func(t *testing.T) { + t.Parallel() + + iter := &sliceIterator[map[string]any]{items: []map[string]any{ + {"id": "abc", "name": "first"}, + {"id": "def", "name": "second"}, + }} + captured := captureShowJSONIterator(t, iter, "raw", "id", -1) + assert.Equal(t, `"abc"`+"\n"+`"def"`+"\n", captured) + }) + + t.Run("LimitItems", func(t *testing.T) { + t.Parallel() + + iter := &sliceIterator[map[string]any]{items: []map[string]any{ + {"id": "abc"}, + {"id": "def"}, + {"id": "ghi"}, + }} + captured := captureShowJSONIterator(t, iter, "raw", "", 2) + assert.Equal(t, `{"id":"abc"}`+"\n"+`{"id":"def"}`+"\n", captured) + }) +} + +func TestExploreFallback(t *testing.T) { + t.Parallel() + + t.Run("ShowJSONFallsBackToJsonOnNonTTY", func(t *testing.T) { + t.Parallel() + + // os.Pipe() produces a *os.File that isn't a terminal, so explore should fall back. + r, w, err := os.Pipe() + require.NoError(t, err) + defer r.Close() + + var stderr bytes.Buffer + res := gjson.Parse(`{"id":"abc"}`) + err = ShowJSON(res, ShowJSONOpts{ + Format: "explore", + Stderr: &stderr, + Stdout: w, + Title: "test", + }) + w.Close() + require.NoError(t, err) + + var buf bytes.Buffer + _, _ = buf.ReadFrom(r) + assert.Contains(t, buf.String(), `"id"`) + assert.Contains(t, buf.String(), `"abc"`) + }) + + t.Run("ShowJSONIteratorFallsBackToJsonOnNonTTY", func(t *testing.T) { + t.Parallel() + + iter := &sliceIterator[map[string]any]{items: []map[string]any{ + {"id": "abc"}, + }} + captured := captureShowJSONIterator(t, iter, "explore", "", -1) + assert.Contains(t, captured, `"id"`) + assert.Contains(t, captured, `"abc"`) + }) + + t.Run("ShowJSONWarnsWhenExplicitFormatOnNonTTY", func(t *testing.T) { + t.Parallel() + + r, w, err := os.Pipe() + require.NoError(t, err) + defer r.Close() + + var stderr bytes.Buffer + res := gjson.Parse(`{"id":"abc"}`) + err = ShowJSON(res, ShowJSONOpts{ + ExplicitFormat: true, + Format: "explore", + Stderr: &stderr, + Stdout: w, + Title: "test", + }) + w.Close() + require.NoError(t, err) + + assert.Equal(t, warningExploreNotSupported, stderr.String()) + }) + + t.Run("ShowJSONSilentWhenDefaultFormatOnNonTTY", func(t *testing.T) { + t.Parallel() + + r, w, err := os.Pipe() + require.NoError(t, err) + defer r.Close() + + var stderr bytes.Buffer + res := gjson.Parse(`{"id":"abc"}`) + err = ShowJSON(res, ShowJSONOpts{ + Format: "explore", + Stderr: &stderr, + Stdout: w, + Title: "test", + }) + w.Close() + require.NoError(t, err) + + assert.Empty(t, stderr.String(), "no warning expected when format was not explicit") + }) +} + +// sliceIterator is a simple iterator over a slice for testing. +type sliceIterator[T any] struct { + index int + items []T +} + +func (it *sliceIterator[T]) Next() bool { + it.index++ + return it.index <= len(it.items) +} + +func (it *sliceIterator[T]) Current() T { + return it.items[it.index-1] +} + +func (it *sliceIterator[T]) Err() error { + return nil +} + +var _ jsonview.Iterator[any] = (*sliceIterator[any])(nil) + +// captureShowJSONIterator runs ShowJSONIterator and captures the output written to a file. +func captureShowJSONIterator[T any](t *testing.T, iter jsonview.Iterator[T], format, transform string, itemsToDisplay int64) string { + t.Helper() + + r, w, err := os.Pipe() + require.NoError(t, err) + defer r.Close() + + err = ShowJSONIterator(iter, itemsToDisplay, ShowJSONOpts{ + Format: format, + Stderr: io.Discard, + Stdout: w, + Title: "test", + Transform: transform, + }) + w.Close() + require.NoError(t, err) + + var buf bytes.Buffer + _, _ = buf.ReadFrom(r) + return buf.String() +} diff --git a/pkg/cmd/cmdutil_unix.go b/pkg/cmd/cmdutil_unix.go new file mode 100644 index 0000000..edefcd7 --- /dev/null +++ b/pkg/cmd/cmdutil_unix.go @@ -0,0 +1,127 @@ +//go:build !windows + +package cmd + +import ( + "fmt" + "os" + "os/exec" + "strings" + "syscall" + + "golang.org/x/sys/unix" +) + +func isPipedDataAvailableOSSpecific() bool { + // Try to determine if there's non-empty data being piped into the command by polling for data for a short + // amount of time. This is necessary because some environments (e.g. Cursor's integrated terminal) connect + // stdin as a pipe even when nothing is being piped, which would cause the command to block indefinitely + // waiting for input that will never come. The 10 ms timeout is arbitrary -- designed to be long enough to + // allow data to be detected, but short enough that it shouldn't cause a noticeable delay in command runs. + fds := []unix.PollFd{{Fd: int32(os.Stdin.Fd()), Events: unix.POLLIN}} + n, _ := unix.Poll(fds, 10 /* ms */) + return n > 0 +} + +func streamOutputOSSpecific(label string, generateOutput func(w *os.File) error) error { + // Try to use socket pair for better buffer control + pagerInput, pid, err := openSocketPairPager(label) + if err != nil || pagerInput == nil { + // Fall back to pipe if socket setup fails + return streamToPagerWithPipe(label, generateOutput) + } + defer pagerInput.Close() + + // If we would be streaming to a terminal and aren't forcing color one way + // or the other, we should configure things to use color so the pager gets + // colorized input. + if isTerminal(os.Stdout) && os.Getenv("FORCE_COLOR") == "" { + os.Setenv("FORCE_COLOR", "1") + } + + // If the pager exits before reading all input, then generateOutput() will + // produce a broken pipe error, which is fine and we don't want to propagate it. + if err := generateOutput(pagerInput); err != nil && + !strings.Contains(err.Error(), "broken pipe") { + return err + } + + // Close the file NOW before we wait for the child process to terminate. + // This way, the child will receive the end-of-file signal and know that + // there is no more input. Otherwise the child process may block + // indefinitely waiting for another line (this can happen when streaming + // less than a screenful of data to a pager). + pagerInput.Close() + + // Wait for child process to exit + var wstatus syscall.WaitStatus + _, err = syscall.Wait4(pid, &wstatus, 0, nil) + if wstatus.ExitStatus() != 0 { + return fmt.Errorf("Pager exited with non-zero exit status: %d", wstatus.ExitStatus()) + } + return err +} + +func openSocketPairPager(label string) (*os.File, int, error) { + fds, err := unix.Socketpair(unix.AF_UNIX, unix.SOCK_STREAM, 0) + if err != nil { + return nil, 0, err + } + + // The child file descriptor will be sent to the child process through + // ProcAttr and ForkExec(), while the parent process will always close the + // child file descriptor. + // The parent file descriptor will be wrapped in an os.File wrapper and + // returned from this function, or closed if something goes wrong. + parentFd, childFd := fds[0], fds[1] + defer unix.Close(childFd) + + // Use small buffer sizes so we don't ask the server for more paginated + // values than we actually need. + if err := unix.SetsockoptInt(parentFd, unix.SOL_SOCKET, unix.SO_SNDBUF, 128); err != nil { + unix.Close(parentFd) + return nil, 0, err + } + if err := unix.SetsockoptInt(childFd, unix.SOL_SOCKET, unix.SO_RCVBUF, 128); err != nil { + unix.Close(parentFd) + return nil, 0, err + } + + // Set CLOEXEC on the parent file descriptor so it doesn't leak to child + syscall.CloseOnExec(parentFd) + + parentConn := os.NewFile(uintptr(parentFd), "parent-socket") + + pagerProgram := os.Getenv("PAGER") + if pagerProgram == "" { + pagerProgram = "less" + } + + pagerPath, err := exec.LookPath(pagerProgram) + if err != nil { + unix.Close(parentFd) + return nil, 0, err + } + + env := os.Environ() + env = append(env, "LESS=-r -P "+label) + env = append(env, "MORE=-r -P "+label) + + procAttr := &syscall.ProcAttr{ + Dir: "", + Env: env, + Files: []uintptr{ + uintptr(childFd), // stdin (fd 0) + uintptr(syscall.Stdout), // stdout (fd 1) + uintptr(syscall.Stderr), // stderr (fd 2) + }, + } + + pid, err := syscall.ForkExec(pagerPath, []string{pagerProgram}, procAttr) + if err != nil { + unix.Close(parentFd) + return nil, 0, err + } + + return parentConn, pid, nil +} diff --git a/pkg/cmd/cmdutil_windows.go b/pkg/cmd/cmdutil_windows.go new file mode 100644 index 0000000..49b025e --- /dev/null +++ b/pkg/cmd/cmdutil_windows.go @@ -0,0 +1,35 @@ +//go:build windows + +package cmd + +import ( + "os" + "syscall" + "unsafe" +) + +var ( + kernel32 = syscall.NewLazyDLL("kernel32.dll") + procPeekNamedPipe = kernel32.NewProc("PeekNamedPipe") +) + +func isPipedDataAvailableOSSpecific() bool { + // On Windows, unix.Poll is not available. Use PeekNamedPipe to check if data is available + // on the pipe without consuming it. + var available uint32 + r, _, _ := procPeekNamedPipe.Call( + os.Stdin.Fd(), + 0, + 0, + 0, + uintptr(unsafe.Pointer(&available)), + 0, + ) + return r != 0 && available > 0 +} + +func streamOutputOSSpecific(label string, generateOutput func(w *os.File) error) error { + // We have a trick with sockets that we use when possible on Unix-like systems. Those APIs aren't + // available on Windows, so we fall back to using pipes. + return streamToPagerWithPipe(label, generateOutput) +} diff --git a/pkg/cmd/flagoptions.go b/pkg/cmd/flagoptions.go new file mode 100644 index 0000000..1879fc1 --- /dev/null +++ b/pkg/cmd/flagoptions.go @@ -0,0 +1,692 @@ +package cmd + +import ( + "bytes" + "encoding/base64" + "encoding/json" + "fmt" + "io" + "maps" + "mime" + "mime/multipart" + "net/http" + "os" + "path/filepath" + "reflect" + "strings" + "unicode/utf8" + + "github.com/context-dot-dev/context-dev-cli/internal/apiform" + "github.com/context-dot-dev/context-dev-cli/internal/apiquery" + "github.com/context-dot-dev/context-dev-cli/internal/debugmiddleware" + "github.com/context-dot-dev/context-dev-cli/internal/requestflag" + "github.com/context-dot-dev/context-go-sdk/v2/option" + + "github.com/goccy/go-yaml" + "github.com/urfave/cli/v3" +) + +type BodyContentType int + +const ( + EmptyBody BodyContentType = iota + MultipartFormEncoded + ApplicationJSON + ApplicationOctetStream +) + +type FileEmbedStyle int + +const ( + // EmbedText reads referenced files fully into memory and substitutes the file's contents back into the + // value as a string. Binary files are base64-encoded. Used for JSON request bodies and for headers and + // query parameters, where the file contents need to be serialized inline. + EmbedText FileEmbedStyle = iota + + // EmbedIOReader replaces file references with an io.Reader that streams the file's contents. Used for + // `multipart/form-data` and `application/octet-stream` request bodies, where files are uploaded as binary + // parts rather than embedded into a text value. + EmbedIOReader +) + +// onceStdinReader wraps an io.Reader that can only be consumed once, used to ensure stdin is read by at most +// one parameter (or only for a body root parameter or only for YAML parameter input). If reason is set, stdin +// is unavailable and read() returns an error explaining why. +type onceStdinReader struct { + stdinReader io.Reader + failureReason string +} + +func (o *onceStdinReader) read() (io.Reader, error) { + if o.failureReason != "" { + return nil, fmt.Errorf("cannot read from stdin: %s", o.failureReason) + } + if o.stdinReader == nil { + return nil, fmt.Errorf("stdin has already been read by another parameter; it can only be read once") + } + r := o.stdinReader + o.stdinReader = nil + return r, nil +} + +func (o *onceStdinReader) readAll() ([]byte, error) { + r, err := o.read() + if err != nil { + return nil, err + } + return io.ReadAll(r) +} + +func isStdinPath(s string) bool { + switch s { + case "-", "/dev/fd/0", "/dev/stdin": + return true + } + return false +} + +func embedFiles(obj any, embedStyle FileEmbedStyle, stdin *onceStdinReader) (any, error) { + if obj == nil { + return obj, nil + } + v := reflect.ValueOf(obj) + result, err := embedFilesValue(v, embedStyle, stdin) + if err != nil { + return nil, err + } + return result.Interface(), nil +} + +// Replace "@file.txt" with the file's contents inside a value +func embedFilesValue(v reflect.Value, embedStyle FileEmbedStyle, stdin *onceStdinReader) (reflect.Value, error) { + // Unwrap interface values to get the concrete type + if v.Kind() == reflect.Interface { + if v.IsNil() { + return v, nil + } + v = v.Elem() + } + + switch v.Kind() { + case reflect.Map: + if v.Len() == 0 { + return v, nil + } + // Always create map[string]any to handle potential type changes when embedding files + result := reflect.MakeMap(reflect.TypeOf(map[string]any{})) + + iter := v.MapRange() + for iter.Next() { + key := iter.Key() + val := iter.Value() + newVal, err := embedFilesValue(val, embedStyle, stdin) + if err != nil { + return reflect.Value{}, err + } + result.SetMapIndex(key, newVal) + } + return result, nil + + case reflect.Slice, reflect.Array: + if v.Len() == 0 { + return v, nil + } + // Use `[]any` to allow for types to change when embedding files + result := reflect.MakeSlice(reflect.TypeOf([]any{}), v.Len(), v.Len()) + for i := 0; i < v.Len(); i++ { + newVal, err := embedFilesValue(v.Index(i), embedStyle, stdin) + if err != nil { + return reflect.Value{}, err + } + result.Index(i).Set(newVal) + } + return result, nil + + case reflect.String: + // FilePathValue is always treated as a file path without needing the "@" prefix. + // These only appear on binary upload parameters (multipart/octet-stream), which + // always use EmbedIOReader. + if v.Type() == reflect.TypeOf(FilePathValue("")) { + s := v.String() + if s == "" { + return v, nil + } + if embedStyle == EmbedIOReader { + if isStdinPath(s) { + r, err := stdin.read() + if err != nil { + return v, err + } + return reflect.ValueOf(io.NopCloser(r)), nil + } + upload, err := openFileUpload(s) + if err != nil { + return v, err + } + return reflect.ValueOf(upload), nil + } + if isStdinPath(s) { + content, err := stdin.readAll() + if err != nil { + return v, err + } + return reflect.ValueOf(string(content)), nil + } + content, err := os.ReadFile(s) + if err != nil { + return v, err + } + return reflect.ValueOf(string(content)), nil + } + + s := v.String() + if literal, ok := strings.CutPrefix(s, "\\@"); ok { + // Allow for escaped @ signs if you don't want them to be treated as files + return reflect.ValueOf("@" + literal), nil + } + + if embedStyle == EmbedText { + if filename, ok := strings.CutPrefix(s, "@data://"); ok { + // The "@data://" prefix is for files you explicitly want to upload + // as base64-encoded (even if the file itself is plain text) + if isStdinPath(filename) { + content, err := stdin.readAll() + if err != nil { + return v, err + } + return reflect.ValueOf(base64.StdEncoding.EncodeToString(content)), nil + } + content, err := os.ReadFile(filename) + if err != nil { + return v, err + } + return reflect.ValueOf(base64.StdEncoding.EncodeToString(content)), nil + } else if filename, ok := strings.CutPrefix(s, "@file://"); ok { + // The "@file://" prefix is for files that you explicitly want to + // upload as a string literal with backslash escapes (not base64 + // encoded) + if isStdinPath(filename) { + content, err := stdin.readAll() + if err != nil { + return v, err + } + return reflect.ValueOf(string(content)), nil + } + content, err := os.ReadFile(filename) + if err != nil { + return v, err + } + return reflect.ValueOf(string(content)), nil + } else if filename, ok := strings.CutPrefix(s, "@"); ok { + if isStdinPath(filename) { + content, err := stdin.readAll() + if err != nil { + return v, err + } + if isUTF8TextFile(content) { + return reflect.ValueOf(string(content)), nil + } + return reflect.ValueOf(base64.StdEncoding.EncodeToString(content)), nil + } + content, err := os.ReadFile(filename) + if err != nil { + // If the string is "@username", it's probably supposed to be a + // string literal and not a file reference. However, if the + // string looks like "@file.txt" or "@/tmp/file", then it's + // probably supposed to be a file. + probablyFile := strings.Contains(filename, ".") || strings.Contains(filename, "/") + if probablyFile { + // Give a useful error message if the user tried to upload a + // file, but the file couldn't be read (e.g. mistyped + // filename or permission error) + return v, err + } + // Fall back to the raw value if the user provided something + // like "@username" that's not intended to be a file. + return v, nil + } + // If the file looks like a plain text UTF8 file format, then use the contents directly. + if isUTF8TextFile(content) { + return reflect.ValueOf(string(content)), nil + } + // Otherwise it's a binary file, so encode it with base64 + return reflect.ValueOf(base64.StdEncoding.EncodeToString(content)), nil + } + } else { + if filename, ok := strings.CutPrefix(s, "@"); ok { + // Behavior is the same for @file, @data://file, and @file://file, except that + // @username will be treated as a literal string if no "username" file exists + expectsFile := true + if withoutPrefix, ok := strings.CutPrefix(filename, "data://"); ok { + filename = withoutPrefix + } else if withoutPrefix, ok := strings.CutPrefix(filename, "file://"); ok { + filename = withoutPrefix + } else { + expectsFile = strings.Contains(filename, ".") || strings.Contains(filename, "/") + } + + if isStdinPath(filename) { + r, err := stdin.read() + if err != nil { + return v, err + } + return reflect.ValueOf(io.NopCloser(r)), nil + } + + upload, err := openFileUpload(filename) + if err != nil { + if !expectsFile { + // For strings that start with "@" and don't look like a filename, return the string + return v, nil + } + return v, err + } + return reflect.ValueOf(upload), nil + } + } + return v, nil + + default: + return v, nil + } +} + +// Guess whether a file's contents are binary (e.g. a .jpg or .mp3), as opposed +// to plain text (e.g. .txt or .md). +func isUTF8TextFile(content []byte) bool { + // Go's DetectContentType follows https://mimesniff.spec.whatwg.org/ and + // these are the sniffable content types that are plain text: + textTypes := []string{ + "text/", + "application/json", + "application/xml", + "application/javascript", + "application/x-javascript", + "application/ecmascript", + "application/x-ecmascript", + } + + contentType := http.DetectContentType(content) + for _, prefix := range textTypes { + if strings.HasPrefix(contentType, prefix) { + return utf8.Valid(content) + } + } + return false +} + +func flagOptions( + cmd *cli.Command, + nestedFormat apiquery.NestedQueryFormat, + arrayFormat apiquery.ArrayQueryFormat, + bodyType BodyContentType, + + // This parameter is true if stdin is already in use to pass a binary parameter by using the special value + // "-". In this case, we won't attempt to read it as a JSON/YAML blob for options setting. + ignoreStdin bool, +) ([]option.RequestOption, error) { + var options []option.RequestOption + if cmd.Bool("debug") { + options = append(options, option.WithMiddleware(debugmiddleware.NewRequestLogger().Middleware())) + } + + requestContents := requestflag.ExtractRequestContents(cmd) + + // Translate inner-field aliases in YAML values that came from flags (e.g. + // `--parent '{"alias": val}'` resolving to the canonical inner field). + if bodyMap, ok := requestContents.Body.(map[string]any); ok { + applyDataAliases(cmd, bodyMap) + } + + stdinConsumedByPipe := false + if bodyType != ApplicationOctetStream && !ignoreStdin && isInputPiped() { + pipeData, err := io.ReadAll(os.Stdin) + if err != nil { + return nil, err + } + + if len(pipeData) > 0 { + stdinConsumedByPipe = true + var bodyData any + if err := yaml.Unmarshal(pipeData, &bodyData); err != nil { + return nil, fmt.Errorf("Failed to parse piped data as YAML/JSON:\n%w", err) + } + if bodyMap, ok := bodyData.(map[string]any); ok { + applyDataAliases(cmd, bodyMap) + // Apply any matching keys from the piped data to path, query, and header flags + // that have not already been set via the command line. + if err := requestflag.ApplyStdinDataToFlags(cmd, bodyMap); err != nil { + return nil, err + } + // Re-extract request contents now that flags may have been updated. + requestContents = requestflag.ExtractRequestContents(cmd) + // Remove keys that were consumed as query, header, or path params so they + // don't also leak into the request body via the maps.Copy merge below. + // We delete both the canonical key and any aliases since the user may have + // piped data using an alias name rather than the canonical API name. + for _, flag := range cmd.Flags { + inReq, ok := flag.(requestflag.InRequest) + if !ok || !flag.IsSet() { + continue + } + if inReq.GetQueryPath() != "" || inReq.GetHeaderPath() != "" || inReq.GetPathParam() != "" { + delete(bodyMap, inReq.GetQueryPath()) + delete(bodyMap, inReq.GetHeaderPath()) + delete(bodyMap, inReq.GetPathParam()) + for _, alias := range inReq.GetDataAliases() { + delete(bodyMap, alias) + } + } + } + if bodyType != EmptyBody { + if flagMap, ok := requestContents.Body.(map[string]any); ok { + maps.Copy(bodyMap, flagMap) + requestContents.Body = bodyMap + } else { + bodyData = requestContents.Body + } + } + } else if bodyType != EmptyBody { + if flagMap, ok := requestContents.Body.(map[string]any); ok && len(flagMap) > 0 { + return nil, fmt.Errorf("Cannot merge flags with a body that is not a map: %v", bodyData) + } else { + requestContents.Body = bodyData + } + } + } + } + + if missingFlags := requestflag.GetMissingRequiredFlags(cmd, requestContents.Body); len(missingFlags) > 0 { + if len(missingFlags) == 1 { + return nil, fmt.Errorf("Required flag %q not set\nRun '%s --help' for usage information", missingFlags[0].Names()[0], cmd.FullName()) + } else { + names := []string{} + for _, flag := range missingFlags { + names = append(names, flag.Names()[0]) + } + return nil, fmt.Errorf("Required flags %q not set\nRun '%s --help' for usage information", strings.Join(names, ", "), cmd.FullName()) + } + } + + // For flags marked as FileInput (type: string, format: binary), the value is always + // a file path. Wrap with FilePathValue so embedFiles reads the file automatically + // without requiring the user to type the "@" prefix. This handles both values set + // via explicit CLI flags and values that arrived via piped YAML/JSON data. + wrapFileInputValues(cmd, &requestContents) + + // Determine stdin availability for FileInput params that use "-". + var stdinReader onceStdinReader + if ignoreStdin { + stdinReader = onceStdinReader{failureReason: "stdin is already being used for the request body"} + } else if stdinConsumedByPipe { + stdinReader = onceStdinReader{failureReason: "stdin was already consumed by piped YAML/JSON input"} + } else { + stdinReader = onceStdinReader{stdinReader: os.Stdin} + } + + // Embed files passed as "@file.jpg" in the request body, headers, and query: + embedStyle := EmbedText + if bodyType == ApplicationOctetStream || bodyType == MultipartFormEncoded { + embedStyle = EmbedIOReader + } + + if embedded, err := embedFiles(requestContents.Body, embedStyle, &stdinReader); err != nil { + return nil, err + } else { + requestContents.Body = embedded + } + + if headersWithFiles, err := embedFiles(requestContents.Headers, EmbedText, &stdinReader); err != nil { + return nil, err + } else { + requestContents.Headers = headersWithFiles.(map[string]any) + } + if queriesWithFiles, err := embedFiles(requestContents.Queries, EmbedText, &stdinReader); err != nil { + return nil, err + } else { + requestContents.Queries = queriesWithFiles.(map[string]any) + } + + querySettings := apiquery.QuerySettings{ + NestedFormat: nestedFormat, + ArrayFormat: arrayFormat, + } + + // Add query parameters: + if values, err := apiquery.MarshalWithSettings(requestContents.Queries, querySettings); err != nil { + return nil, err + } else { + for k, vs := range values { + if len(vs) == 0 { + options = append(options, option.WithQueryDel(k)) + } else { + options = append(options, option.WithQuery(k, vs[0])) + for _, v := range vs[1:] { + options = append(options, option.WithQueryAdd(k, v)) + } + } + } + } + + // Add header parameters + headerSettings := apiquery.QuerySettings{ + NestedFormat: apiquery.NestedQueryFormatDots, + ArrayFormat: apiquery.ArrayQueryFormatRepeat, + } + if values, err := apiquery.MarshalWithSettings(requestContents.Headers, headerSettings); err != nil { + return nil, err + } else { + for k, vs := range values { + if len(vs) == 0 { + options = append(options, option.WithHeaderDel(k)) + } else { + options = append(options, option.WithHeader(k, vs[0])) + for _, v := range vs[1:] { + options = append(options, option.WithHeaderAdd(k, v)) + } + } + } + } + + switch bodyType { + case EmptyBody: + break + case MultipartFormEncoded: + buf := new(bytes.Buffer) + writer := multipart.NewWriter(buf) + + // For multipart/form-encoded, we need a map structure + bodyMap, ok := requestContents.Body.(map[string]any) + if !ok { + return nil, fmt.Errorf("Cannot send a non-map value to a form-encoded endpoint: %v\n", requestContents.Body) + } + encodingFormat := apiform.FormatComma + if err := apiform.MarshalWithSettings(bodyMap, writer, encodingFormat); err != nil { + return nil, err + } + if err := writer.Close(); err != nil { + return nil, err + } + options = append(options, option.WithRequestBody(writer.FormDataContentType(), buf)) + + case ApplicationJSON: + bodyBytes, err := json.Marshal(requestContents.Body) + if err != nil { + return nil, err + } + options = append(options, option.WithRequestBody("application/json", bodyBytes)) + + case ApplicationOctetStream: + // If there is a body root parameter, that will handle setting the request body, we don't need to do it here. + for _, flag := range cmd.Flags { + if toSend, ok := flag.(requestflag.InRequest); ok && toSend.IsBodyRoot() { + return options, nil + } + } + if bodyBytes, ok := requestContents.Body.([]byte); ok { + options = append(options, option.WithRequestBody("application/octet-stream", bodyBytes)) + } else if bodyStr, ok := requestContents.Body.(string); ok { + options = append(options, option.WithRequestBody("application/octet-stream", []byte(bodyStr))) + } else { + return nil, fmt.Errorf("Unsupported body for application/octet-stream: %v", requestContents.Body) + } + + default: + panic("Invalid body content type!") + } + + return options, nil +} + +// FilePathValue is a string wrapper that marks a value as a file path whose contents should be read +// and embedded in the request. Unlike a regular string, embedFilesValue always treats a FilePathValue +// as a file path without needing the "@" prefix. +type FilePathValue string + +// fileUpload wraps an io.Reader with filename and content-type metadata for +// use as a multipart form part. The apiform encoder detects the Filename and +// ContentType methods and uses them to populate the Content-Disposition +// filename and the Content-Type header on the part. +type fileUpload struct { + io.Reader // apiform checks for reader and reads its contents during encode + filename string + contentType string +} + +func (f fileUpload) Filename() string { return f.filename } +func (f fileUpload) ContentType() string { return f.contentType } +func (f fileUpload) Close() error { + if c, ok := f.Reader.(io.Closer); ok { + return c.Close() + } + return nil +} + +// openFileUpload opens the file at path and returns a fileUpload whose filename +// is the path's basename and whose content type is derived from the file +// extension (falling back to application/octet-stream when unknown). +func openFileUpload(path string) (fileUpload, error) { + file, err := os.Open(path) + if err != nil { + return fileUpload{}, err + } + contentType := mime.TypeByExtension(filepath.Ext(path)) + if contentType == "" { + contentType = "application/octet-stream" + } + return fileUpload{ + Reader: file, + filename: filepath.Base(path), + contentType: contentType, + }, nil +} + +// applyDataAliases rewrites keys in a body map based on flag `DataAliases` metadata. For top-level flags, +// `{alias: value}` becomes `{canonical: value}`. For inner flags (those registered under an outer flag +// via WithInnerFlags), the alias translation is also applied to the nested map under the outer flag's +// body path, so values like `--parent '{"alias": val}'` resolve to the canonical inner field name. +func applyDataAliases(cmd *cli.Command, bodyMap map[string]any) { + for _, flag := range cmd.Flags { + // Inner flags: rewrite aliases inside the nested map under the outer flag's body path. + if inner, ok := flag.(requestflag.HasOuterFlag); ok { + outer, outerOk := inner.GetOuterFlag().(requestflag.InRequest) + if !outerOk { + continue + } + if nested, ok := bodyMap[outer.GetBodyPath()].(map[string]any); ok && inner.GetInnerField() != "" { + rewriteAliases(nested, inner.GetInnerField(), inner.GetDataAliases()) + } + continue + } + // Top-level flags: rewrite aliases in the body map. + if inReq, ok := flag.(requestflag.InRequest); ok && inReq.GetBodyPath() != "" { + rewriteAliases(bodyMap, inReq.GetBodyPath(), inReq.GetDataAliases()) + } + } +} + +// rewriteAliases replaces each alias key in m with the canonical key, preserving the value. The +// "canonical" key is the name the API itself expects (the OpenAPI property/field name) — e.g. for +// a top-level flag, the parameter's BodyPath; for an inner flag, the inner field name. Aliases are +// the user-facing alternate names declared via x-stainless-cli-data-alias. +func rewriteAliases(m map[string]any, canonical string, aliases []string) { + for _, alias := range aliases { + if alias == "" || alias == canonical { + continue + } + if val, exists := m[alias]; exists { + m[canonical] = val + delete(m, alias) + } + } +} + +// wrapFileInputValues replaces string values for FileInput flags (type: string, format: binary) with +// FilePathValue sentinel values. embedFilesValue recognizes FilePathValue and reads the file contents +// directly, so the user doesn't need to type the "@" prefix. This handles both values set via explicit +// CLI flags and values that arrived via piped YAML/JSON data. +func wrapFileInputValues(cmd *cli.Command, contents *requestflag.RequestContents) { + bodyMap, _ := contents.Body.(map[string]any) + + for _, flag := range cmd.Flags { + inReq, ok := flag.(requestflag.InRequest) + if !ok || !inReq.IsFileInput() || inReq.IsBodyRoot() { + continue + } + + // Wrap values set via explicit CLI flags. + if flag.IsSet() { + if wrapped, changed := wrapFileInputValue(flag.Get()); changed { + if bodyPath := inReq.GetBodyPath(); bodyPath != "" { + if bodyMap != nil { + bodyMap[bodyPath] = wrapped + } + } else if queryPath := inReq.GetQueryPath(); queryPath != "" { + contents.Queries[queryPath] = wrapped + } else if headerPath := inReq.GetHeaderPath(); headerPath != "" { + contents.Headers[headerPath] = wrapped + } + } + } + + // Wrap values that arrived via piped YAML/JSON data in the body map. + if bodyPath := inReq.GetBodyPath(); bodyPath != "" && bodyMap != nil { + if value, exists := bodyMap[bodyPath]; exists { + if wrapped, changed := wrapFileInputValue(value); changed { + bodyMap[bodyPath] = wrapped + } + } + } + } +} + +func wrapFileInputValue(value any) (any, bool) { + switch v := value.(type) { + case string: + if v == "" { + return value, false + } + return FilePathValue(v), true + + case []string: + result := make([]any, len(v)) + for i, s := range v { + result[i] = FilePathValue(s) + } + return result, true + + case []any: + result := make([]any, len(v)) + for i, elem := range v { + if s, ok := elem.(string); ok { + result[i] = FilePathValue(s) + } else { + result[i] = elem + } + } + return result, true + + default: + return value, false + } +} diff --git a/pkg/cmd/flagoptions_test.go b/pkg/cmd/flagoptions_test.go new file mode 100644 index 0000000..00734ca --- /dev/null +++ b/pkg/cmd/flagoptions_test.go @@ -0,0 +1,392 @@ +package cmd + +import ( + "encoding/base64" + "io" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestIsUTF8TextFile(t *testing.T) { + t.Parallel() + + tests := []struct { + content []byte + expected bool + }{ + {[]byte("Hello, world!"), true}, + {[]byte(`{"key": "value"}`), true}, + {[]byte(``), true}, + {[]byte(`function test() {}`), true}, + {[]byte{0xFF, 0xD8, 0xFF, 0xE0}, false}, // JPEG header + {[]byte{0x00, 0x01, 0xFF, 0xFE}, false}, // binary + {[]byte("Hello \xFF\xFE"), false}, // invalid UTF-8 + {[]byte("Hello ☺️"), true}, // emoji + {[]byte{}, true}, // empty + } + + for _, tt := range tests { + require.Equal(t, tt.expected, isUTF8TextFile(tt.content)) + } +} + +func TestEmbedFiles(t *testing.T) { + t.Parallel() + + // Create temporary directory for test files + tmpDir := t.TempDir() + + // Create test files + configContent := "host=localhost\nport=8080" + templateContent := "Hello" + dataContent := `{"key": "value"}` + + writeTestFile(t, tmpDir, "config.txt", configContent) + writeTestFile(t, tmpDir, "template.html", templateContent) + writeTestFile(t, tmpDir, "data.json", dataContent) + jpegHeader := []byte{0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46} + writeTestFile(t, tmpDir, "image.jpg", string(jpegHeader)) + + tests := []struct { + name string + input any + want any + wantErr bool + }{ + { + name: "map[string]any with file references", + input: map[string]any{ + "config": "@" + filepath.Join(tmpDir, "config.txt"), + "template": "@file://" + filepath.Join(tmpDir, "template.html"), + "count": 42, + }, + want: map[string]any{ + "config": configContent, + "template": templateContent, + "count": 42, + }, + wantErr: false, + }, + { + name: "map[string]string with file references", + input: map[string]any{ + "config": "@" + filepath.Join(tmpDir, "config.txt"), + "name": "test", + }, + want: map[string]any{ + "config": configContent, + "name": "test", + }, + wantErr: false, + }, + { + name: "[]any with file references", + input: []any{ + "@" + filepath.Join(tmpDir, "config.txt"), + 42, + true, + "@file://" + filepath.Join(tmpDir, "data.json"), + }, + want: []any{ + configContent, + 42, + true, + dataContent, + }, + wantErr: false, + }, + { + name: "[]string with file references", + input: []any{ + "@" + filepath.Join(tmpDir, "config.txt"), + "normal string", + }, + want: []any{ + configContent, + "normal string", + }, + wantErr: false, + }, + { + name: "nested structures", + input: map[string]any{ + "outer": map[string]any{ + "inner": []any{ + "@" + filepath.Join(tmpDir, "config.txt"), + map[string]any{ + "data": "@" + filepath.Join(tmpDir, "data.json"), + }, + }, + }, + }, + want: map[string]any{ + "outer": map[string]any{ + "inner": []any{ + configContent, + map[string]any{ + "data": dataContent, + }, + }, + }, + }, + wantErr: false, + }, + { + name: "base64 encoding", + input: map[string]any{ + "encoded": "@data://" + filepath.Join(tmpDir, "config.txt"), + "image": "@" + filepath.Join(tmpDir, "image.jpg"), + }, + want: map[string]any{ + "encoded": base64.StdEncoding.EncodeToString([]byte(configContent)), + "image": base64.StdEncoding.EncodeToString(jpegHeader), + }, + wantErr: false, + }, + { + name: "non-existent file with @ prefix", + input: map[string]any{ + "missing": "@file.txt", + }, + want: nil, + wantErr: true, + }, + { + name: "non-file-like thing with @ prefix", + input: map[string]any{ + "username": "@user", + "favorite_symbol": "@", + }, + want: map[string]any{ + "username": "@user", + "favorite_symbol": "@", + }, + wantErr: false, + }, + { + name: "non-existent file with @file:// prefix (error)", + input: map[string]any{ + "missing": "@file:///nonexistent/file.txt", + }, + want: nil, + wantErr: true, + }, + { + name: "escaping", + input: map[string]any{ + "simple": "\\@file.txt", + "file": "\\@file://file.txt", + "data": "\\@data://file.txt", + "keep_escape": "user\\@example.com", + }, + want: map[string]any{ + "simple": "@file.txt", + "file": "@file://file.txt", + "data": "@data://file.txt", + "keep_escape": "user\\@example.com", + }, + wantErr: false, + }, + { + name: "primitive types", + input: map[string]any{ + "int": 123, + "float": 45.67, + "bool": true, + "null": nil, + "string": "no prefix", + "email": "user@example.com", + }, + want: map[string]any{ + "int": 123, + "float": 45.67, + "bool": true, + "null": nil, + "string": "no prefix", + "email": "user@example.com", + }, + wantErr: false, + }, + { + name: "[]int values unchanged", + input: []int{1, 2, 3, 4, 5}, + want: []any{1, 2, 3, 4, 5}, + wantErr: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name+" text", func(t *testing.T) { + t.Parallel() + + got, err := embedFiles(tt.input, EmbedText, nil) + if tt.wantErr { + require.Error(t, err) + } else { + require.NoError(t, err) + require.Equal(t, tt.want, got) + } + }) + + t.Run(tt.name+" io.Reader", func(t *testing.T) { + t.Parallel() + + _, err := embedFiles(tt.input, EmbedIOReader, nil) + if tt.wantErr { + require.Error(t, err) + } else { + require.NoError(t, err) + } + }) + } +} + +func TestEmbedFilesStdin(t *testing.T) { + t.Parallel() + + t.Run("FilePathValueDash", func(t *testing.T) { + t.Parallel() + + stdin := &onceStdinReader{stdinReader: strings.NewReader("stdin content")} + + withEmbedded, err := embedFiles(map[string]any{"file": FilePathValue("-")}, EmbedText, stdin) + require.NoError(t, err) + require.Equal(t, map[string]any{"file": "stdin content"}, withEmbedded) + }) + + t.Run("FilePathValueDevStdin", func(t *testing.T) { + t.Parallel() + + stdin := &onceStdinReader{stdinReader: strings.NewReader("stdin content")} + + withEmbedded, err := embedFiles(map[string]any{"file": FilePathValue("/dev/stdin")}, EmbedText, stdin) + require.NoError(t, err) + require.Equal(t, map[string]any{"file": "stdin content"}, withEmbedded) + }) + + t.Run("MultipleFilePathValueDashesError", func(t *testing.T) { + t.Parallel() + + stdin := &onceStdinReader{stdinReader: strings.NewReader("stdin content")} + + _, err := embedFiles(map[string]any{ + "file1": FilePathValue("-"), + "file2": FilePathValue("-"), + }, EmbedText, stdin) + require.Error(t, err) + require.Contains(t, err.Error(), "already been read") + }) + + t.Run("FilePathValueDashUnavailableStdin", func(t *testing.T) { + t.Parallel() + + stdin := &onceStdinReader{failureReason: "stdin is already being used for the request body"} + + _, err := embedFiles(map[string]any{"file": FilePathValue("-")}, EmbedText, stdin) + require.Error(t, err) + require.Contains(t, err.Error(), "cannot read from stdin") + require.Contains(t, err.Error(), "request body") + }) + + t.Run("AtDashEmbedText", func(t *testing.T) { + t.Parallel() + + stdin := &onceStdinReader{stdinReader: strings.NewReader("piped content")} + + withEmbedded, err := embedFiles(map[string]any{"data": "@-"}, EmbedText, stdin) + require.NoError(t, err) + require.Equal(t, map[string]any{"data": "piped content"}, withEmbedded) + }) + + t.Run("AtDashEmbedIOReader", func(t *testing.T) { + t.Parallel() + + stdin := &onceStdinReader{stdinReader: strings.NewReader("piped content")} + + withEmbedded, err := embedFiles(map[string]any{"data": "@-"}, EmbedIOReader, stdin) + require.NoError(t, err) + + withEmbeddedMap := withEmbedded.(map[string]any) + r := withEmbeddedMap["data"].(io.ReadCloser) + + content, err := io.ReadAll(r) + require.NoError(t, err) + require.Equal(t, "piped content", string(content)) + }) + + t.Run("FilePathValueRealFile", func(t *testing.T) { + t.Parallel() + + tmpDir := t.TempDir() + writeTestFile(t, tmpDir, "test.txt", "file content") + + stdin := &onceStdinReader{stdinReader: strings.NewReader("unused stdin")} + + withEmbedded, err := embedFiles(map[string]any{"file": FilePathValue(filepath.Join(tmpDir, "test.txt"))}, EmbedText, stdin) + require.NoError(t, err) + require.Equal(t, map[string]any{"file": "file content"}, withEmbedded) + }) +} + +// TestEmbedFilesUploadMetadata verifies that EmbedIOReader mode wraps file readers with filename and +// content-type metadata so the multipart encoder populates `Content-Disposition` and `Content-Type` headers. +func TestEmbedFilesUploadMetadata(t *testing.T) { + t.Parallel() + + tmpDir := t.TempDir() + writeTestFile(t, tmpDir, "hello.txt", "hi") + writeTestFile(t, tmpDir, "page.html", "") + writeTestFile(t, tmpDir, "blob.bin", "\x00\x01") + + cases := []struct { + basename string + wantContentType string + }{ + {"hello.txt", "text/plain; charset=utf-8"}, + {"page.html", "text/html; charset=utf-8"}, + {"blob.bin", "application/octet-stream"}, + } + + for _, tc := range cases { + t.Run("AtPrefix_"+tc.basename, func(t *testing.T) { + t.Parallel() + + path := filepath.Join(tmpDir, tc.basename) + withEmbedded, err := embedFiles(map[string]any{"file": "@" + path}, EmbedIOReader, nil) + require.NoError(t, err) + + upload, ok := withEmbedded.(map[string]any)["file"].(fileUpload) + require.True(t, ok, "expected fileUpload, got %T", withEmbedded.(map[string]any)["file"]) + require.Equal(t, tc.basename, upload.Filename()) + require.Equal(t, upload.ContentType(), tc.wantContentType) + require.NoError(t, upload.Close()) + }) + + t.Run("FilePathValue_"+tc.basename, func(t *testing.T) { + t.Parallel() + + path := filepath.Join(tmpDir, tc.basename) + withEmbedded, err := embedFiles(map[string]any{"file": FilePathValue(path)}, EmbedIOReader, nil) + require.NoError(t, err) + + upload, ok := withEmbedded.(map[string]any)["file"].(fileUpload) + require.True(t, ok, "expected fileUpload, got %T", withEmbedded.(map[string]any)["file"]) + require.Equal(t, tc.basename, upload.Filename()) + require.Equal(t, upload.ContentType(), tc.wantContentType) + require.NoError(t, upload.Close()) + }) + } +} + +func writeTestFile(t *testing.T, dir, filename, content string) { + t.Helper() + + path := filepath.Join(dir, filename) + + err := os.WriteFile(path, []byte(content), 0644) + require.NoError(t, err, "failed to write test file %s", path) +} diff --git a/pkg/cmd/industry.go b/pkg/cmd/industry.go new file mode 100644 index 0000000..4fd74f4 --- /dev/null +++ b/pkg/cmd/industry.go @@ -0,0 +1,179 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "context" + "fmt" + + "github.com/context-dot-dev/context-dev-cli/internal/apiquery" + "github.com/context-dot-dev/context-dev-cli/internal/requestflag" + "github.com/context-dot-dev/context-go-sdk/v2" + "github.com/context-dot-dev/context-go-sdk/v2/option" + "github.com/tidwall/gjson" + "github.com/urfave/cli/v3" +) + +var industryRetrieveNaics = cli.Command{ + Name: "retrieve-naics", + Usage: "Classify any brand into 2022 NAICS industry codes from its domain or name.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "input", + Usage: "Brand domain or title to retrieve NAICS code for. If a valid domain is provided, it will be used for classification, otherwise, we will search for the brand using the provided title.", + Required: true, + QueryPath: "input", + }, + &requestflag.Flag[int64]{ + Name: "max-results", + Usage: "Maximum number of NAICS codes to return. Must be between 1 and 10. Defaults to 5.", + Default: 5, + QueryPath: "maxResults", + }, + &requestflag.Flag[int64]{ + Name: "min-results", + Usage: "Minimum number of NAICS codes to return. Must be at least 1. Defaults to 1.", + Default: 1, + QueryPath: "minResults", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional comma-separated caller-defined tags for tracking this request. Tags are recorded on the request's usage log and can be used to filter usage on the dashboard usage page. Up to 20 tags, each 1-50 characters.", + QueryPath: "tags", + }, + &requestflag.Flag[int64]{ + Name: "timeout-ms", + Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).", + QueryPath: "timeoutMS", + }, + }, + Action: handleIndustryRetrieveNaics, + HideHelpCommand: true, +} + +var industryRetrieveSic = cli.Command{ + Name: "retrieve-sic", + Usage: "Classify any brand into Standard Industrial Classification (SIC) codes from its\ndomain or name. Choose between the original SIC system (`original_sic`) or the\nlatest SIC list maintained by the SEC (`latest_sec`).", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "input", + Usage: "Brand domain or title to retrieve SIC code for. If a valid domain is provided, it will be used for classification, otherwise, we will search for the brand using the provided title.", + Required: true, + QueryPath: "input", + }, + &requestflag.Flag[int64]{ + Name: "max-results", + Usage: "Maximum number of SIC codes to return. Must be between 1 and 10. Defaults to 5.", + Default: 5, + QueryPath: "maxResults", + }, + &requestflag.Flag[int64]{ + Name: "min-results", + Usage: "Minimum number of SIC codes to return. Must be at least 1. Defaults to 1.", + Default: 1, + QueryPath: "minResults", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional comma-separated caller-defined tags for tracking this request. Tags are recorded on the request's usage log and can be used to filter usage on the dashboard usage page. Up to 20 tags, each 1-50 characters.", + QueryPath: "tags", + }, + &requestflag.Flag[int64]{ + Name: "timeout-ms", + Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).", + QueryPath: "timeoutMS", + }, + &requestflag.Flag[string]{ + Name: "type", + Usage: "Which SIC dataset to classify against. `original_sic` uses the 1987 Standard Industrial Classification system; `latest_sec` uses the current SIC list as published by the SEC. Defaults to `original_sic`.", + Default: "original_sic", + QueryPath: "type", + }, + }, + Action: handleIndustryRetrieveSic, + HideHelpCommand: true, +} + +func handleIndustryRetrieveNaics(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + params := contextdev.IndustryGetNaicsParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Industry.GetNaics(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "industry retrieve-naics", + Transform: transform, + }) +} + +func handleIndustryRetrieveSic(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + params := contextdev.IndustryGetSicParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Industry.GetSic(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "industry retrieve-sic", + Transform: transform, + }) +} diff --git a/pkg/cmd/industry_test.go b/pkg/cmd/industry_test.go new file mode 100644 index 0000000..b7f6329 --- /dev/null +++ b/pkg/cmd/industry_test.go @@ -0,0 +1,44 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "testing" + + "github.com/context-dot-dev/context-dev-cli/internal/mocktest" +) + +func TestIndustryRetrieveNaics(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "industry", "retrieve-naics", + "--input", "xxxx", + "--max-results", "1", + "--min-results", "1", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1000", + ) + }) +} + +func TestIndustryRetrieveSic(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "industry", "retrieve-sic", + "--input", "xxxx", + "--max-results", "1", + "--min-results", "1", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1000", + "--type", "original_sic", + ) + }) +} diff --git a/pkg/cmd/monitor.go b/pkg/cmd/monitor.go new file mode 100644 index 0000000..fac0f1c --- /dev/null +++ b/pkg/cmd/monitor.go @@ -0,0 +1,1032 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "context" + "fmt" + + "github.com/context-dot-dev/context-dev-cli/internal/apiquery" + "github.com/context-dot-dev/context-dev-cli/internal/requestflag" + "github.com/context-dot-dev/context-go-sdk/v2" + "github.com/context-dot-dev/context-go-sdk/v2/option" + "github.com/tidwall/gjson" + "github.com/urfave/cli/v3" +) + +var monitorsCreate = requestflag.WithInnerFlags(cli.Command{ + Name: "create", + Usage: "Creates a monitor. The request body is a union of the supported target/change\ndetection combinations. The monitor runs immediately after creation to create\nits initial baseline.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "name", + Required: true, + BodyPath: "name", + }, + &requestflag.Flag[map[string]any]{ + Name: "target", + Usage: "Discriminated union describing what the monitor watches.", + Required: true, + BodyPath: "target", + }, + &requestflag.Flag[map[string]any]{ + Name: "change-detection", + Usage: "Discriminated union describing how changes are detected.", + BodyPath: "change_detection", + }, + &requestflag.Flag[string]{ + Name: "mode", + Usage: "Top-level monitor category. Always `web` today; the concrete behavior is described by `target` and `change_detection`.", + BodyPath: "mode", + }, + &requestflag.Flag[map[string]any]{ + Name: "schedule", + Usage: "Run the monitor on a fixed interval defined by a frequency and a unit, e.g. every 6 hours or every 2 days. The total interval (frequency × unit) must be between 10 minutes and 1 year.", + BodyPath: "schedule", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "User-defined tags for grouping and filtering monitors and their changes. Duplicates are removed.", + BodyPath: "tags", + }, + &requestflag.Flag[map[string]any]{ + Name: "webhook", + BodyPath: "webhook", + }, + }, + Action: handleMonitorsCreate, + HideHelpCommand: true, +}, map[string][]requestflag.HasOuterFlag{ + "schedule": { + &requestflag.InnerFlag[int64]{ + Name: "schedule.frequency", + Usage: "Number of units between runs. The resulting interval (frequency × unit) must be at least 10 minutes and at most 1 year (e.g. minimum 10 when unit is minutes; maximum 365 when unit is days).", + InnerField: "frequency", + }, + &requestflag.InnerFlag[string]{ + Name: "schedule.type", + Usage: `Allowed values: "interval".`, + InnerField: "type", + }, + &requestflag.InnerFlag[string]{ + Name: "schedule.unit", + Usage: `Allowed values: "minutes", "hours", "days".`, + InnerField: "unit", + }, + }, + "webhook": { + &requestflag.InnerFlag[string]{ + Name: "webhook.url", + Usage: "Webhook URL events are delivered to.", + InnerField: "url", + }, + &requestflag.InnerFlag[[]string]{ + Name: "webhook.events", + Usage: "Events delivered to this endpoint. `change.detected` fires only when a run detects a change; `run.completed` fires on every completed run — including runs that detected no change — and embeds the change when one was detected. Defaults to `[\"change.detected\"]` when omitted.", + InnerField: "events", + }, + &requestflag.InnerFlag[string]{ + Name: "webhook.secret", + Usage: "Signing secret used to verify webhook authenticity. Each delivery includes an `X-Context-Signature: t=,v1=` header, where the HMAC is SHA-256 over `\"{t}.{rawRequestBody}\"` keyed by this secret. Recompute it with a constant-time compare and reject stale timestamps to prevent replay. Generated by the API; cannot be set by clients.", + InnerField: "secret", + }, + }, +}) + +var monitorsRetrieve = cli.Command{ + Name: "retrieve", + Usage: "Get a monitor", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "monitor-id", + Required: true, + PathParam: "monitor_id", + }, + }, + Action: handleMonitorsRetrieve, + HideHelpCommand: true, +} + +var monitorsUpdate = requestflag.WithInnerFlags(cli.Command{ + Name: "update", + Usage: "Updates a monitor. If `target` or `change_detection` changes, the monitor\ncreates a new baseline. Unsupported target/change detection combinations are\nrejected.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "monitor-id", + Required: true, + PathParam: "monitor_id", + }, + &requestflag.Flag[map[string]any]{ + Name: "change-detection", + Usage: "Discriminated union describing how changes are detected.", + BodyPath: "change_detection", + }, + &requestflag.Flag[string]{ + Name: "name", + BodyPath: "name", + }, + &requestflag.Flag[map[string]any]{ + Name: "schedule", + Usage: "Run the monitor on a fixed interval defined by a frequency and a unit, e.g. every 6 hours or every 2 days. The total interval (frequency × unit) must be between 10 minutes and 1 year.", + BodyPath: "schedule", + }, + &requestflag.Flag[string]{ + Name: "status", + Usage: `Allowed values: "active", "paused".`, + BodyPath: "status", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "User-defined tags for grouping and filtering monitors and their changes. Duplicates are removed.", + BodyPath: "tags", + }, + &requestflag.Flag[map[string]any]{ + Name: "target", + Usage: "Discriminated union describing what the monitor watches.", + BodyPath: "target", + }, + &requestflag.Flag[map[string]any]{ + Name: "webhook", + Usage: "Set to null to remove the webhook.", + BodyPath: "webhook", + }, + }, + Action: handleMonitorsUpdate, + HideHelpCommand: true, +}, map[string][]requestflag.HasOuterFlag{ + "schedule": { + &requestflag.InnerFlag[int64]{ + Name: "schedule.frequency", + Usage: "Number of units between runs. The resulting interval (frequency × unit) must be at least 10 minutes and at most 1 year (e.g. minimum 10 when unit is minutes; maximum 365 when unit is days).", + InnerField: "frequency", + }, + &requestflag.InnerFlag[string]{ + Name: "schedule.type", + Usage: `Allowed values: "interval".`, + InnerField: "type", + }, + &requestflag.InnerFlag[string]{ + Name: "schedule.unit", + Usage: `Allowed values: "minutes", "hours", "days".`, + InnerField: "unit", + }, + }, + "webhook": { + &requestflag.InnerFlag[string]{ + Name: "webhook.url", + Usage: "Webhook URL events are delivered to.", + InnerField: "url", + }, + &requestflag.InnerFlag[[]string]{ + Name: "webhook.events", + Usage: "Events delivered to this endpoint. `change.detected` fires only when a run detects a change; `run.completed` fires on every completed run — including runs that detected no change — and embeds the change when one was detected. Defaults to `[\"change.detected\"]` when omitted.", + InnerField: "events", + }, + &requestflag.InnerFlag[string]{ + Name: "webhook.secret", + Usage: "Signing secret used to verify webhook authenticity. Each delivery includes an `X-Context-Signature: t=,v1=` header, where the HMAC is SHA-256 over `\"{t}.{rawRequestBody}\"` keyed by this secret. Recompute it with a constant-time compare and reject stale timestamps to prevent replay. Generated by the API; cannot be set by clients.", + InnerField: "secret", + }, + }, +}) + +var monitorsList = cli.Command{ + Name: "list", + Usage: "Lists monitors for the authenticated organization. Supports free-text search\n(`q` over `search_by` fields, `prefix` or `exact` via `search_type`) plus\nstatus/type/tag filters. Results are paginated via the opaque `cursor`.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "change-detection-type", + Usage: "Filter by change detection type.", + QueryPath: "change_detection_type", + }, + &requestflag.Flag[string]{ + Name: "cursor", + Usage: "Opaque pagination cursor from a previous response.", + QueryPath: "cursor", + }, + &requestflag.Flag[int64]{ + Name: "limit", + Usage: "Maximum number of items to return per page (1-100). Defaults to 25.", + QueryPath: "limit", + }, + &requestflag.Flag[string]{ + Name: "q", + Usage: "Free-text search term, matched against the fields named in `search_by`.", + QueryPath: "q", + }, + &requestflag.Flag[any]{ + Name: "search-by", + Usage: "Comma-separated fields to search with `q`. Defaults to all of them. Note `instructions` only exists on extract monitors.", + QueryPath: "search_by", + }, + &requestflag.Flag[string]{ + Name: "search-type", + Usage: "`prefix` for as-you-type prefix matching (default), `exact` for full-token matching.", + QueryPath: "search_type", + }, + &requestflag.Flag[string]{ + Name: "status", + Usage: "Filter monitors by lifecycle status.", + QueryPath: "status", + }, + &requestflag.Flag[string]{ + Name: "tag", + Usage: "Filter to items that have this tag.", + QueryPath: "tag", + }, + &requestflag.Flag[any]{ + Name: "tag", + Usage: "Comma-separated list of tags to filter by (matches monitors having any of them).", + QueryPath: "tags", + }, + &requestflag.Flag[string]{ + Name: "target-type", + Usage: "Filter by target type.", + QueryPath: "target_type", + }, + }, + Action: handleMonitorsList, + HideHelpCommand: true, +} + +var monitorsDelete = cli.Command{ + Name: "delete", + Usage: "Delete a monitor", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "monitor-id", + Required: true, + PathParam: "monitor_id", + }, + }, + Action: handleMonitorsDelete, + HideHelpCommand: true, +} + +var monitorsGetCreditUsage = cli.Command{ + Name: "get-credit-usage", + Usage: "Returns credits charged per monitor over an optional [since, until] window,\nnewest spenders first.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[any]{ + Name: "since", + Usage: "Only include items at or after this ISO 8601 timestamp.", + QueryPath: "since", + }, + &requestflag.Flag[any]{ + Name: "until", + Usage: "Only include items before this ISO 8601 timestamp.", + QueryPath: "until", + }, + }, + Action: handleMonitorsGetCreditUsage, + HideHelpCommand: true, +} + +var monitorsGetLimits = cli.Command{ + Name: "get-limits", + Usage: "Returns how many monitors the account has and the maximum it allows.", + Suggest: true, + Flags: []cli.Flag{}, + Action: handleMonitorsGetLimits, + HideHelpCommand: true, +} + +var monitorsListAccountChanges = cli.Command{ + Name: "list-account-changes", + Usage: "Returns an account-wide feed of detected changes across monitors.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "change-detection-type", + Usage: "Filter by change detection type.", + QueryPath: "change_detection_type", + }, + &requestflag.Flag[string]{ + Name: "cursor", + Usage: "Opaque pagination cursor from a previous response.", + QueryPath: "cursor", + }, + &requestflag.Flag[int64]{ + Name: "limit", + Usage: "Maximum number of items to return per page (1-100). Defaults to 25.", + QueryPath: "limit", + }, + &requestflag.Flag[string]{ + Name: "monitor-id", + Usage: "Filter changes to a single monitor.", + QueryPath: "monitor_id", + }, + &requestflag.Flag[any]{ + Name: "since", + Usage: "Only include items at or after this ISO 8601 timestamp.", + QueryPath: "since", + }, + &requestflag.Flag[string]{ + Name: "tag", + Usage: "Filter to items that have this tag.", + QueryPath: "tag", + }, + &requestflag.Flag[string]{ + Name: "target-type", + Usage: "Filter by target type.", + QueryPath: "target_type", + }, + &requestflag.Flag[any]{ + Name: "until", + Usage: "Only include items before this ISO 8601 timestamp.", + QueryPath: "until", + }, + }, + Action: handleMonitorsListAccountChanges, + HideHelpCommand: true, +} + +var monitorsListAccountRuns = cli.Command{ + Name: "list-account-runs", + Usage: "Returns an account-wide feed of monitor runs across all monitors.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "cursor", + Usage: "Opaque pagination cursor from a previous response.", + QueryPath: "cursor", + }, + &requestflag.Flag[int64]{ + Name: "limit", + Usage: "Maximum number of items to return per page (1-100). Defaults to 25.", + QueryPath: "limit", + }, + &requestflag.Flag[string]{ + Name: "status", + Usage: "Filter runs by lifecycle status.", + QueryPath: "status", + }, + }, + Action: handleMonitorsListAccountRuns, + HideHelpCommand: true, +} + +var monitorsListChanges = cli.Command{ + Name: "list-changes", + Usage: "List changes for a monitor", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "monitor-id", + Required: true, + PathParam: "monitor_id", + }, + &requestflag.Flag[string]{ + Name: "cursor", + Usage: "Opaque pagination cursor from a previous response.", + QueryPath: "cursor", + }, + &requestflag.Flag[int64]{ + Name: "limit", + Usage: "Maximum number of items to return per page (1-100). Defaults to 25.", + QueryPath: "limit", + }, + &requestflag.Flag[any]{ + Name: "since", + Usage: "Only include items at or after this ISO 8601 timestamp.", + QueryPath: "since", + }, + &requestflag.Flag[string]{ + Name: "tag", + Usage: "Filter to items that have this tag.", + QueryPath: "tag", + }, + &requestflag.Flag[any]{ + Name: "until", + Usage: "Only include items before this ISO 8601 timestamp.", + QueryPath: "until", + }, + }, + Action: handleMonitorsListChanges, + HideHelpCommand: true, +} + +var monitorsListRuns = cli.Command{ + Name: "list-runs", + Usage: "List monitor runs", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "monitor-id", + Required: true, + PathParam: "monitor_id", + }, + &requestflag.Flag[string]{ + Name: "cursor", + Usage: "Opaque pagination cursor from a previous response.", + QueryPath: "cursor", + }, + &requestflag.Flag[int64]{ + Name: "limit", + Usage: "Maximum number of items to return per page (1-100). Defaults to 25.", + QueryPath: "limit", + }, + &requestflag.Flag[string]{ + Name: "status", + Usage: "Filter runs by lifecycle status.", + QueryPath: "status", + }, + }, + Action: handleMonitorsListRuns, + HideHelpCommand: true, +} + +var monitorsRetrieveChange = cli.Command{ + Name: "retrieve-change", + Usage: "Get a change", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "change-id", + Required: true, + PathParam: "change_id", + }, + }, + Action: handleMonitorsRetrieveChange, + HideHelpCommand: true, +} + +var monitorsRun = cli.Command{ + Name: "run", + Usage: "Triggers an immediate run of the monitor outside its normal schedule. The run is\nqueued and processed asynchronously.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "monitor-id", + Required: true, + PathParam: "monitor_id", + }, + }, + Action: handleMonitorsRun, + HideHelpCommand: true, +} + +func handleMonitorsCreate(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + ApplicationJSON, + false, + ) + if err != nil { + return err + } + + params := contextdev.MonitorNewParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Monitors.New(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "monitors create", + Transform: transform, + }) +} + +func handleMonitorsRetrieve(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + if !cmd.IsSet("monitor-id") && len(unusedArgs) > 0 { + cmd.Set("monitor-id", unusedArgs[0]) + unusedArgs = unusedArgs[1:] + } + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Monitors.Get(ctx, cmd.Value("monitor-id").(string), options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "monitors retrieve", + Transform: transform, + }) +} + +func handleMonitorsUpdate(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + if !cmd.IsSet("monitor-id") && len(unusedArgs) > 0 { + cmd.Set("monitor-id", unusedArgs[0]) + unusedArgs = unusedArgs[1:] + } + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + ApplicationJSON, + false, + ) + if err != nil { + return err + } + + params := contextdev.MonitorUpdateParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Monitors.Update( + ctx, + cmd.Value("monitor-id").(string), + params, + options..., + ) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "monitors update", + Transform: transform, + }) +} + +func handleMonitorsList(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + params := contextdev.MonitorListParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Monitors.List(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "monitors list", + Transform: transform, + }) +} + +func handleMonitorsDelete(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + if !cmd.IsSet("monitor-id") && len(unusedArgs) > 0 { + cmd.Set("monitor-id", unusedArgs[0]) + unusedArgs = unusedArgs[1:] + } + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Monitors.Delete(ctx, cmd.Value("monitor-id").(string), options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "monitors delete", + Transform: transform, + }) +} + +func handleMonitorsGetCreditUsage(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + params := contextdev.MonitorGetCreditUsageParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Monitors.GetCreditUsage(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "monitors get-credit-usage", + Transform: transform, + }) +} + +func handleMonitorsGetLimits(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Monitors.GetLimits(ctx, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "monitors get-limits", + Transform: transform, + }) +} + +func handleMonitorsListAccountChanges(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + params := contextdev.MonitorListAccountChangesParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Monitors.ListAccountChanges(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "monitors list-account-changes", + Transform: transform, + }) +} + +func handleMonitorsListAccountRuns(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + params := contextdev.MonitorListAccountRunsParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Monitors.ListAccountRuns(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "monitors list-account-runs", + Transform: transform, + }) +} + +func handleMonitorsListChanges(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + if !cmd.IsSet("monitor-id") && len(unusedArgs) > 0 { + cmd.Set("monitor-id", unusedArgs[0]) + unusedArgs = unusedArgs[1:] + } + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + params := contextdev.MonitorListChangesParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Monitors.ListChanges( + ctx, + cmd.Value("monitor-id").(string), + params, + options..., + ) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "monitors list-changes", + Transform: transform, + }) +} + +func handleMonitorsListRuns(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + if !cmd.IsSet("monitor-id") && len(unusedArgs) > 0 { + cmd.Set("monitor-id", unusedArgs[0]) + unusedArgs = unusedArgs[1:] + } + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + params := contextdev.MonitorListRunsParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Monitors.ListRuns( + ctx, + cmd.Value("monitor-id").(string), + params, + options..., + ) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "monitors list-runs", + Transform: transform, + }) +} + +func handleMonitorsRetrieveChange(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + if !cmd.IsSet("change-id") && len(unusedArgs) > 0 { + cmd.Set("change-id", unusedArgs[0]) + unusedArgs = unusedArgs[1:] + } + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Monitors.GetChange(ctx, cmd.Value("change-id").(string), options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "monitors retrieve-change", + Transform: transform, + }) +} + +func handleMonitorsRun(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + if !cmd.IsSet("monitor-id") && len(unusedArgs) > 0 { + cmd.Set("monitor-id", unusedArgs[0]) + unusedArgs = unusedArgs[1:] + } + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Monitors.Run(ctx, cmd.Value("monitor-id").(string), options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "monitors run", + Transform: transform, + }) +} diff --git a/pkg/cmd/monitor_test.go b/pkg/cmd/monitor_test.go new file mode 100644 index 0000000..983650e --- /dev/null +++ b/pkg/cmd/monitor_test.go @@ -0,0 +1,321 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "testing" + + "github.com/context-dot-dev/context-dev-cli/internal/mocktest" + "github.com/context-dot-dev/context-dev-cli/internal/requestflag" +) + +func TestMonitorsCreate(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "monitors", "create", + "--name", "Acme pricing page", + "--target", "{type: page, url: https://acme.com/pricing, instructions: 'Report pricing or plan availability changes. Ignore counters, timestamps, testimonials, and navigation.', normalize_whitespace: true}", + "--change-detection", "{type: exact}", + "--mode", "web", + "--schedule", "{frequency: 6, type: interval, unit: hours}", + "--tag", "pricing", + "--tag", "competitor", + "--webhook", "{url: https://example.com/webhook, events: [change.detected, run.completed]}", + ) + }) + + t.Run("inner flags", func(t *testing.T) { + // Check that inner flags have been set up correctly + requestflag.CheckInnerFlags(monitorsCreate) + + // Alternative argument passing style using inner flags + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "monitors", "create", + "--name", "Acme pricing page", + "--target", "{type: page, url: https://acme.com/pricing, instructions: 'Report pricing or plan availability changes. Ignore counters, timestamps, testimonials, and navigation.', normalize_whitespace: true}", + "--change-detection", "{type: exact}", + "--mode", "web", + "--schedule.frequency", "6", + "--schedule.type", "interval", + "--schedule.unit", "hours", + "--tag", "pricing", + "--tag", "competitor", + "--webhook.url", "https://example.com/webhook", + "--webhook.events", "[change.detected, run.completed]", + ) + }) + + t.Run("piping data", func(t *testing.T) { + // Test piping YAML data over stdin + pipeData := []byte("" + + "name: Acme pricing page\n" + + "target:\n" + + " type: page\n" + + " url: https://acme.com/pricing\n" + + " instructions: >-\n" + + " Report pricing or plan availability changes. Ignore counters, timestamps,\n" + + " testimonials, and navigation.\n" + + " normalize_whitespace: true\n" + + "change_detection:\n" + + " type: exact\n" + + "mode: web\n" + + "schedule:\n" + + " frequency: 6\n" + + " type: interval\n" + + " unit: hours\n" + + "tags:\n" + + " - pricing\n" + + " - competitor\n" + + "webhook:\n" + + " url: https://example.com/webhook\n" + + " events:\n" + + " - change.detected\n" + + " - run.completed\n") + mocktest.TestRunMockTestWithPipeAndFlags( + t, pipeData, + "--api-key", "string", + "monitors", "create", + ) + }) +} + +func TestMonitorsRetrieve(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "monitors", "retrieve", + "--monitor-id", "mon_123", + ) + }) +} + +func TestMonitorsUpdate(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "monitors", "update", + "--monitor-id", "mon_123", + "--change-detection", "{type: exact}", + "--name", "Acme pricing monitor", + "--schedule", "{frequency: 1, type: interval, unit: hours}", + "--status", "active", + "--tag", "pricing", + "--tag", "competitor", + "--target", "{type: page, url: https://acme.com/pricing, instructions: 'Report pricing or plan availability changes. Ignore counters, timestamps, testimonials, and navigation.', normalize_whitespace: true}", + "--webhook", "{url: https://example.com/webhook, events: [change.detected, run.completed]}", + ) + }) + + t.Run("inner flags", func(t *testing.T) { + // Check that inner flags have been set up correctly + requestflag.CheckInnerFlags(monitorsUpdate) + + // Alternative argument passing style using inner flags + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "monitors", "update", + "--monitor-id", "mon_123", + "--change-detection", "{type: exact}", + "--name", "Acme pricing monitor", + "--schedule.frequency", "1", + "--schedule.type", "interval", + "--schedule.unit", "hours", + "--status", "active", + "--tag", "pricing", + "--tag", "competitor", + "--target", "{type: page, url: https://acme.com/pricing, instructions: 'Report pricing or plan availability changes. Ignore counters, timestamps, testimonials, and navigation.', normalize_whitespace: true}", + "--webhook.url", "https://example.com/webhook", + "--webhook.events", "[change.detected, run.completed]", + ) + }) + + t.Run("piping data", func(t *testing.T) { + // Test piping YAML data over stdin + pipeData := []byte("" + + "change_detection:\n" + + " type: exact\n" + + "name: Acme pricing monitor\n" + + "schedule:\n" + + " frequency: 1\n" + + " type: interval\n" + + " unit: hours\n" + + "status: active\n" + + "tags:\n" + + " - pricing\n" + + " - competitor\n" + + "target:\n" + + " type: page\n" + + " url: https://acme.com/pricing\n" + + " instructions: >-\n" + + " Report pricing or plan availability changes. Ignore counters, timestamps,\n" + + " testimonials, and navigation.\n" + + " normalize_whitespace: true\n" + + "webhook:\n" + + " url: https://example.com/webhook\n" + + " events:\n" + + " - change.detected\n" + + " - run.completed\n") + mocktest.TestRunMockTestWithPipeAndFlags( + t, pipeData, + "--api-key", "string", + "monitors", "update", + "--monitor-id", "mon_123", + ) + }) +} + +func TestMonitorsList(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "monitors", "list", + "--change-detection-type", "exact", + "--cursor", "cursor", + "--limit", "1", + "--q", "pricing", + "--search-by", "[name]", + "--search-type", "exact", + "--status", "active", + "--tag", "pricing", + "--tag", "[x]", + "--target-type", "page", + ) + }) +} + +func TestMonitorsDelete(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "monitors", "delete", + "--monitor-id", "mon_123", + ) + }) +} + +func TestMonitorsGetCreditUsage(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "monitors", "get-credit-usage", + "--since", "'2026-06-01T00:00:00Z'", + "--until", "'2026-06-28T00:00:00Z'", + ) + }) +} + +func TestMonitorsGetLimits(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "monitors", "get-limits", + ) + }) +} + +func TestMonitorsListAccountChanges(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "monitors", "list-account-changes", + "--change-detection-type", "exact", + "--cursor", "cursor", + "--limit", "1", + "--monitor-id", "mon_123", + "--since", "'2026-06-01T00:00:00Z'", + "--tag", "pricing", + "--target-type", "page", + "--until", "'2026-06-28T00:00:00Z'", + ) + }) +} + +func TestMonitorsListAccountRuns(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "monitors", "list-account-runs", + "--cursor", "cursor", + "--limit", "1", + "--status", "queued", + ) + }) +} + +func TestMonitorsListChanges(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "monitors", "list-changes", + "--monitor-id", "mon_123", + "--cursor", "cursor", + "--limit", "1", + "--since", "'2026-06-01T00:00:00Z'", + "--tag", "pricing", + "--until", "'2026-06-28T00:00:00Z'", + ) + }) +} + +func TestMonitorsListRuns(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "monitors", "list-runs", + "--monitor-id", "mon_123", + "--cursor", "cursor", + "--limit", "1", + "--status", "queued", + ) + }) +} + +func TestMonitorsRetrieveChange(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "monitors", "retrieve-change", + "--change-id", "chg_123", + ) + }) +} + +func TestMonitorsRun(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "monitors", "run", + "--monitor-id", "mon_123", + ) + }) +} diff --git a/pkg/cmd/news.go b/pkg/cmd/news.go new file mode 100644 index 0000000..e9f54cc --- /dev/null +++ b/pkg/cmd/news.go @@ -0,0 +1,147 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "context" + "fmt" + + "github.com/context-dot-dev/context-dev-cli/internal/apiquery" + "github.com/context-dot-dev/context-dev-cli/internal/requestflag" + "github.com/context-dot-dev/context-go-sdk/v2" + "github.com/context-dot-dev/context-go-sdk/v2/option" + "github.com/tidwall/gjson" + "github.com/urfave/cli/v3" +) + +var newsSearch = requestflag.WithInnerFlags(cli.Command{ + Name: "search", + Usage: "Searches live and historical company news for one company, identified in\nsearchBy by name, domain, ticker (optionally disambiguated by exchange), or\nISIN. Results can be filtered by publisher domain, publisher country, article\nlanguage, article type, and published-at date, and include stable story IDs,\nsource metadata, verified entity relevance, and cursor pagination.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[map[string]any]{ + Name: "search-by", + Usage: "What to search for.", + Required: true, + BodyPath: "searchBy", + }, + &requestflag.Flag[*string]{ + Name: "cursor", + Usage: "Opaque next_cursor from the previous response, or null for the first page.", + BodyPath: "cursor", + }, + &requestflag.Flag[map[string]any]{ + Name: "filter-by", + Usage: "Optional result filters.", + Default: map[string]any{}, + BodyPath: "filterBy", + }, + &requestflag.Flag[int64]{ + Name: "limit", + Usage: "Maximum results to return. Defaults to 10.", + Default: 10, + BodyPath: "limit", + }, + &requestflag.Flag[map[string]any]{ + Name: "sort-by", + Usage: "Result ordering. Defaults to newest.", + Default: map[string]any{"type": "newest"}, + BodyPath: "sortBy", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional tags for tracking usage. Up to 20 tags, each 1 to 50 characters.", + BodyPath: "tags", + }, + }, + Action: handleNewsSearch, + HideHelpCommand: true, +}, map[string][]requestflag.HasOuterFlag{ + "search-by": { + &requestflag.InnerFlag[map[string]any]{ + Name: "search-by.entity", + Usage: "The company to search news for, identified by name, domain, ticker, or ISIN.", + InnerField: "entity", + }, + &requestflag.InnerFlag[string]{ + Name: "search-by.type", + Usage: "How to search. Only entity search is supported.", + InnerField: "type", + }, + }, + "filter-by": { + &requestflag.InnerFlag[[]string]{ + Name: "filter-by.article-language", + Usage: "Article languages to include. Up to 3.", + InnerField: "articleLanguage", + }, + &requestflag.InnerFlag[[]string]{ + Name: "filter-by.article-type", + Usage: "Article types to include. Up to 3.", + InnerField: "articleType", + }, + &requestflag.InnerFlag[map[string]any]{ + Name: "filter-by.date", + Usage: "Published-at window in epoch milliseconds.", + InnerField: "date", + }, + &requestflag.InnerFlag[[]string]{ + Name: "filter-by.source-country", + Usage: "Publisher countries to include, as lowercase ISO 3166-1 alpha-2 codes. Up to 3.", + InnerField: "sourceCountry", + }, + &requestflag.InnerFlag[[]string]{ + Name: "filter-by.source-domain", + Usage: "Publisher domains to include. Up to 3.", + InnerField: "sourceDomain", + }, + }, + "sort-by": { + &requestflag.InnerFlag[string]{ + Name: "sort-by.type", + Usage: "Result ordering.", + InnerField: "type", + }, + }, +}) + +func handleNewsSearch(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + ApplicationJSON, + false, + ) + if err != nil { + return err + } + + params := contextdev.NewsSearchParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.News.Search(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "news search", + Transform: transform, + }) +} diff --git a/pkg/cmd/news_test.go b/pkg/cmd/news_test.go new file mode 100644 index 0000000..5260a23 --- /dev/null +++ b/pkg/cmd/news_test.go @@ -0,0 +1,86 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "testing" + + "github.com/context-dot-dev/context-dev-cli/internal/mocktest" + "github.com/context-dot-dev/context-dev-cli/internal/requestflag" +) + +func TestNewsSearch(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "news", "search", + "--search-by", "{entity: {name: xx, type: name}, type: entity}", + "--cursor", "cursor", + "--filter-by", "{articleLanguage: [ar], articleType: [editorial], date: {from: 0, to: 0}, sourceCountry: [ae], sourceDomain: [x]}", + "--limit", "1", + "--sort-by", "{type: relevance}", + "--tag", "production", + "--tag", "team-alpha", + ) + }) + + t.Run("inner flags", func(t *testing.T) { + // Check that inner flags have been set up correctly + requestflag.CheckInnerFlags(newsSearch) + + // Alternative argument passing style using inner flags + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "news", "search", + "--search-by.entity", "{name: xx, type: name}", + "--search-by.type", "entity", + "--cursor", "cursor", + "--filter-by.article-language", "[ar]", + "--filter-by.article-type", "[editorial]", + "--filter-by.date", "{from: 0, to: 0}", + "--filter-by.source-country", "[ae]", + "--filter-by.source-domain", "[x]", + "--limit", "1", + "--sort-by.type", "relevance", + "--tag", "production", + "--tag", "team-alpha", + ) + }) + + t.Run("piping data", func(t *testing.T) { + // Test piping YAML data over stdin + pipeData := []byte("" + + "searchBy:\n" + + " entity:\n" + + " name: xx\n" + + " type: name\n" + + " type: entity\n" + + "cursor: cursor\n" + + "filterBy:\n" + + " articleLanguage:\n" + + " - ar\n" + + " articleType:\n" + + " - editorial\n" + + " date:\n" + + " from: 0\n" + + " to: 0\n" + + " sourceCountry:\n" + + " - ae\n" + + " sourceDomain:\n" + + " - x\n" + + "limit: 1\n" + + "sortBy:\n" + + " type: relevance\n" + + "tags:\n" + + " - production\n" + + " - team-alpha\n") + mocktest.TestRunMockTestWithPipeAndFlags( + t, pipeData, + "--api-key", "string", + "news", "search", + ) + }) +} diff --git a/pkg/cmd/parse.go b/pkg/cmd/parse.go new file mode 100644 index 0000000..7f53188 --- /dev/null +++ b/pkg/cmd/parse.go @@ -0,0 +1,158 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "context" + "fmt" + "os" + + "github.com/context-dot-dev/context-dev-cli/internal/apiquery" + "github.com/context-dot-dev/context-dev-cli/internal/binaryparam" + "github.com/context-dot-dev/context-dev-cli/internal/requestflag" + "github.com/context-dot-dev/context-go-sdk/v2" + "github.com/context-dot-dev/context-go-sdk/v2/option" + "github.com/tidwall/gjson" + "github.com/urfave/cli/v3" +) + +var parseHandle = requestflag.WithInnerFlags(cli.Command{ + Name: "handle", + Usage: "Converts raw text, source code, web/data, PDF, Microsoft Office, and image bytes\ninto LLM-usable Markdown.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "body", + Required: true, + BodyRoot: true, + FileInput: true, + }, + &requestflag.Flag[string]{ + Name: "client", + Usage: "Optional client identifier used for usage attribution.", + QueryPath: "client", + }, + &requestflag.Flag[string]{ + Name: "extension", + Usage: "Optional file extension hint, such as pdf, docx, xlsx, pptx, html, json, csv, md, py, rtf, jpg, png, or txt.", + QueryPath: "extension", + }, + &requestflag.Flag[bool]{ + Name: "include-images", + Usage: "Include image references in Markdown output", + Default: false, + QueryPath: "includeImages", + }, + &requestflag.Flag[bool]{ + Name: "include-links", + Usage: "Preserve hyperlinks in Markdown output", + Default: true, + QueryPath: "includeLinks", + }, + &requestflag.Flag[bool]{ + Name: "ocr", + Usage: "When true for PDF inputs, OCR the selected pages that have no usable text layer (scans), replacing each recovered page's text with the OCR result while pages with a real text layer keep it. pdf.start/pdf.end limit the inclusive page range. Billed at 1 credit per page OCR actually recovered, on top of the base request cost. When false, no OCR runs.", + Default: false, + QueryPath: "ocr", + }, + &requestflag.Flag[map[string]any]{ + Name: "pdf", + Usage: `PDF page-range options as a JSON object, e.g. {"start": 2, "end": 5}.`, + Default: map[string]any{}, + QueryPath: "pdf", + }, + &requestflag.Flag[bool]{ + Name: "shorten-base64-images", + Usage: "Shorten base64-encoded image data in the Markdown output", + Default: true, + QueryPath: "shortenBase64Images", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional comma-separated caller-defined tags for tracking this request. Tags are recorded on the request's usage log and can be used to filter usage on the dashboard usage page. Up to 20 tags, each 1-50 characters.", + QueryPath: "tags", + }, + &requestflag.Flag[bool]{ + Name: "use-main-content-only", + Usage: "Extract only the main content from HTML-like inputs", + Default: false, + QueryPath: "useMainContentOnly", + }, + &requestflag.Flag[string]{ + Name: "zdr", + Usage: "Set to enabled to bypass shared caches and omit request and response content from retained usage logs. Requires zero data retention to be enabled for your organization (contact support@context.dev), otherwise the request fails with ZDR_NOT_ENABLED. Successful ZDR responses include X-Context-ZDR: true.", + Default: "disabled", + QueryPath: "zdr", + }, + }, + Action: handleParseHandle, + HideHelpCommand: true, +}, map[string][]requestflag.HasOuterFlag{ + "pdf": { + &requestflag.InnerFlag[int64]{ + Name: "pdf.end", + Usage: "Last 1-based PDF page to parse. When omitted, parsing ends at the last page. Must be greater than or equal to start when both are provided.", + InnerField: "end", + }, + &requestflag.InnerFlag[int64]{ + Name: "pdf.start", + Usage: "First 1-based PDF page to parse. When omitted, parsing starts at the first page.", + InnerField: "start", + }, + }, +}) + +func handleParseHandle(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + if !cmd.IsSet("body") && len(unusedArgs) > 0 { + cmd.Set("body", unusedArgs[0]) + unusedArgs = unusedArgs[1:] + } + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + bodyReader, stdinInUse, err := binaryparam.FileOrStdin(os.Stdin, cmd.Value("body").(string)) + if err != nil { + return fmt.Errorf("Failed on param '%s': %w", "body", err) + } + defer bodyReader.Close() + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + ApplicationOctetStream, + stdinInUse, + ) + if err != nil { + return err + } + + params := contextdev.ParseHandleParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Parse.Handle( + ctx, + bodyReader, + params, + options..., + ) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "parse handle", + Transform: transform, + }) +} diff --git a/pkg/cmd/parse_test.go b/pkg/cmd/parse_test.go new file mode 100644 index 0000000..fd11604 --- /dev/null +++ b/pkg/cmd/parse_test.go @@ -0,0 +1,79 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "testing" + + "github.com/context-dot-dev/context-dev-cli/internal/mocktest" + "github.com/context-dot-dev/context-dev-cli/internal/requestflag" +) + +func TestParseHandle(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "parse", "handle", + "--body", mocktest.TestFile(t, "Example data"), + "--client", "x", + "--extension", "txt", + "--include-images=true", + "--include-links=true", + "--ocr=true", + "--pdf", "{end: 1, start: 1}", + "--shorten-base64-images=true", + "--tag", "production", + "--tag", "team-alpha", + "--use-main-content-only=true", + "--zdr", "enabled", + ) + }) + + t.Run("inner flags", func(t *testing.T) { + // Check that inner flags have been set up correctly + requestflag.CheckInnerFlags(parseHandle) + + // Alternative argument passing style using inner flags + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "parse", "handle", + "--body", mocktest.TestFile(t, "Example data"), + "--client", "x", + "--extension", "txt", + "--include-images=true", + "--include-links=true", + "--ocr=true", + "--pdf.end", "1", + "--pdf.start", "1", + "--shorten-base64-images=true", + "--tag", "production", + "--tag", "team-alpha", + "--use-main-content-only=true", + "--zdr", "enabled", + ) + }) + + t.Run("piping data", func(t *testing.T) { + // Test piping YAML data over stdin + pipeData := []byte("Example data") + mocktest.TestRunMockTestWithPipeAndFlags( + t, pipeData, + "--api-key", "string", + "parse", "handle", + "--client", "x", + "--extension", "txt", + "--include-images=true", + "--include-links=true", + "--ocr=true", + "--pdf", "{end: 1, start: 1}", + "--shorten-base64-images=true", + "--tag", "production", + "--tag", "team-alpha", + "--use-main-content-only=true", + "--zdr", "enabled", + ) + }) +} diff --git a/pkg/cmd/person.go b/pkg/cmd/person.go new file mode 100644 index 0000000..efc6e30 --- /dev/null +++ b/pkg/cmd/person.go @@ -0,0 +1,153 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "context" + "fmt" + + "github.com/context-dot-dev/context-dev-cli/internal/apiquery" + "github.com/context-dot-dev/context-dev-cli/internal/requestflag" + "github.com/context-dot-dev/context-go-sdk/v2" + "github.com/context-dot-dev/context-go-sdk/v2/option" + "github.com/tidwall/gjson" + "github.com/urfave/cli/v3" +) + +var peopleEnrich = requestflag.WithInnerFlags(cli.Command{ + Name: "enrich", + Usage: "Finds and normalizes the best available person candidate from additive identity\nclues, then assigns an identity match score from 0 to 100. Available on all paid\nplans. Successful requests cost 20 credits. Disposable and free email addresses\n(like gmail.com, yahoo.com) will throw a 422 error.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[map[string]any]{ + Name: "company", + BodyPath: "company", + }, + &requestflag.Flag[[]map[string]any]{ + Name: "education", + BodyPath: "education", + }, + &requestflag.Flag[string]{ + Name: "email", + BodyPath: "email", + }, + &requestflag.Flag[map[string]any]{ + Name: "location", + BodyPath: "location", + }, + &requestflag.Flag[map[string]any]{ + Name: "name", + BodyPath: "name", + }, + &requestflag.Flag[[]string]{ + Name: "social-url", + BodyPath: "social_urls", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional tags for tracking usage. Up to 20 tags, each 1 to 50 characters.", + BodyPath: "tags", + }, + &requestflag.Flag[int64]{ + Name: "timeout-ms", + Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).", + BodyPath: "timeoutMS", + }, + }, + Action: handlePeopleEnrich, + HideHelpCommand: true, +}, map[string][]requestflag.HasOuterFlag{ + "company": { + &requestflag.InnerFlag[string]{ + Name: "company.domain", + InnerField: "domain", + }, + &requestflag.InnerFlag[string]{ + Name: "company.name", + InnerField: "name", + }, + }, + "education": { + &requestflag.InnerFlag[string]{ + Name: "education.degree", + InnerField: "degree", + }, + &requestflag.InnerFlag[string]{ + Name: "education.field-of-study", + InnerField: "field_of_study", + }, + &requestflag.InnerFlag[int64]{ + Name: "education.graduation-year", + InnerField: "graduation_year", + }, + &requestflag.InnerFlag[map[string]any]{ + Name: "education.institution", + InnerField: "institution", + }, + }, + "location": { + &requestflag.InnerFlag[string]{ + Name: "location.city", + InnerField: "city", + }, + &requestflag.InnerFlag[string]{ + Name: "location.country", + InnerField: "country", + }, + &requestflag.InnerFlag[string]{ + Name: "location.region", + InnerField: "region", + }, + }, + "name": { + &requestflag.InnerFlag[string]{ + Name: "name.first", + InnerField: "first", + }, + &requestflag.InnerFlag[string]{ + Name: "name.last", + InnerField: "last", + }, + }, +}) + +func handlePeopleEnrich(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + ApplicationJSON, + false, + ) + if err != nil { + return err + } + + params := contextdev.PersonEnrichParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.People.Enrich(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "people enrich", + Transform: transform, + }) +} diff --git a/pkg/cmd/person_test.go b/pkg/cmd/person_test.go new file mode 100644 index 0000000..2a1a4b1 --- /dev/null +++ b/pkg/cmd/person_test.go @@ -0,0 +1,92 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "testing" + + "github.com/context-dot-dev/context-dev-cli/internal/mocktest" + "github.com/context-dot-dev/context-dev-cli/internal/requestflag" +) + +func TestPeopleEnrich(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "people", "enrich", + "--company", "{domain: analyticalengines.example, name: Analytical Engines}", + "--education", "{degree: x, field_of_study: x, graduation_year: 1900, institution: {domain: x, name: x}}", + "--email", "dev@stainless.com", + "--location", "{city: x, country: x, region: x}", + "--name", "{first: Ada, last: Lovelace}", + "--social-url", "https://www.linkedin.com/in/ada-lovelace/", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1000", + ) + }) + + t.Run("inner flags", func(t *testing.T) { + // Check that inner flags have been set up correctly + requestflag.CheckInnerFlags(peopleEnrich) + + // Alternative argument passing style using inner flags + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "people", "enrich", + "--company.domain", "analyticalengines.example", + "--company.name", "Analytical Engines", + "--education.degree", "x", + "--education.field-of-study", "x", + "--education.graduation-year", "1900", + "--education.institution", "{domain: x, name: x}", + "--email", "dev@stainless.com", + "--location.city", "x", + "--location.country", "x", + "--location.region", "x", + "--name.first", "Ada", + "--name.last", "Lovelace", + "--social-url", "https://www.linkedin.com/in/ada-lovelace/", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1000", + ) + }) + + t.Run("piping data", func(t *testing.T) { + // Test piping YAML data over stdin + pipeData := []byte("" + + "company:\n" + + " domain: analyticalengines.example\n" + + " name: Analytical Engines\n" + + "education:\n" + + " - degree: x\n" + + " field_of_study: x\n" + + " graduation_year: 1900\n" + + " institution:\n" + + " domain: x\n" + + " name: x\n" + + "email: dev@stainless.com\n" + + "location:\n" + + " city: x\n" + + " country: x\n" + + " region: x\n" + + "name:\n" + + " first: Ada\n" + + " last: Lovelace\n" + + "social_urls:\n" + + " - https://www.linkedin.com/in/ada-lovelace/\n" + + "tags:\n" + + " - production\n" + + " - team-alpha\n" + + "timeoutMS: 1000\n") + mocktest.TestRunMockTestWithPipeAndFlags( + t, pipeData, + "--api-key", "string", + "people", "enrich", + ) + }) +} diff --git a/pkg/cmd/suggest.go b/pkg/cmd/suggest.go new file mode 100644 index 0000000..b4b637c --- /dev/null +++ b/pkg/cmd/suggest.go @@ -0,0 +1,126 @@ +package cmd + +import ( + "fmt" + "math" + "slices" + "strings" + + "github.com/urfave/cli/v3" +) + +// This entire file is mostly taken from urfave/cli/v3's source, with the exception of suggestCommand which is +// modified for a nicer error message. + +// jaroDistance is the measure of similarity between two strings. It returns a +// value between 0 and 1, where 1 indicates identical strings and 0 indicates +// completely different strings. +// +// Adapted from https://github.com/xrash/smetrics/blob/5f08fbb34913bc8ab95bb4f2a89a0637ca922666/jaro.go. +func jaroDistance(a, b string) float64 { + if len(a) == 0 && len(b) == 0 { + return 1 + } + if len(a) == 0 || len(b) == 0 { + return 0 + } + + lenA := float64(len(a)) + lenB := float64(len(b)) + hashA := make([]bool, len(a)) + hashB := make([]bool, len(b)) + maxDistance := int(math.Max(0, math.Floor(math.Max(lenA, lenB)/2.0)-1)) + + var matches float64 + for i := 0; i < len(a); i++ { + start := int(math.Max(0, float64(i-maxDistance))) + end := int(math.Min(lenB-1, float64(i+maxDistance))) + + for j := start; j <= end; j++ { + if hashB[j] { + continue + } + if a[i] == b[j] { + hashA[i] = true + hashB[j] = true + matches++ + break + } + } + } + if matches == 0 { + return 0 + } + + var transpositions float64 + var j int + for i := 0; i < len(a); i++ { + if !hashA[i] { + continue + } + for !hashB[j] { + j++ + } + if a[i] != b[j] { + transpositions++ + } + j++ + } + + transpositions /= 2 + return ((matches / lenA) + (matches / lenB) + ((matches - transpositions) / matches)) / 3.0 +} + +// jaroWinkler is more accurate when strings have a common prefix up to a +// defined maximum length. +// +// Adapted from https://github.com/xrash/smetrics/blob/5f08fbb34913bc8ab95bb4f2a89a0637ca922666/jaro-winkler.go. +func jaroWinkler(a, b string) float64 { + const ( + boostThreshold = 0.7 + prefixSize = 4 + ) + jaroDist := jaroDistance(a, b) + if jaroDist <= boostThreshold { + return jaroDist + } + + prefix := int(math.Min(float64(len(a)), math.Min(float64(prefixSize), float64(len(b))))) + + var prefixMatch float64 + for i := 0; i < prefix; i++ { + if a[i] == b[i] { + prefixMatch++ + } else { + break + } + } + return jaroDist + 0.1*prefixMatch*(1.0-jaroDist) +} + +// suggestCommand takes a list of commands and a provided string to suggest a +// command name +func suggestCommand(commands []*cli.Command, provided string) string { + distance := 0.0 + var lineage []*cli.Command + for _, command := range commands { + for _, name := range command.Names() { + newDistance := jaroWinkler(name, provided) + if newDistance > distance { + distance = newDistance + lineage = command.Lineage() + } + } + } + + var parts []string + for _, command := range lineage { + parts = append(parts, command.Name) + } + slices.Reverse(parts) + return fmt.Sprintf("Did you mean '%s'?", strings.Join(parts, " ")) +} + +func init() { + cli.SuggestCommand = suggestCommand +} diff --git a/pkg/cmd/utility.go b/pkg/cmd/utility.go new file mode 100644 index 0000000..4173edf --- /dev/null +++ b/pkg/cmd/utility.go @@ -0,0 +1,88 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "context" + "fmt" + + "github.com/context-dot-dev/context-dev-cli/internal/apiquery" + "github.com/context-dot-dev/context-dev-cli/internal/requestflag" + "github.com/context-dot-dev/context-go-sdk/v2" + "github.com/context-dot-dev/context-go-sdk/v2/option" + "github.com/tidwall/gjson" + "github.com/urfave/cli/v3" +) + +var utilityPrefetch = cli.Command{ + Name: "prefetch", + Usage: "Signal that you may fetch data soon to improve latency. The type field selects\nwhat to prefetch ('brand' queues a brand data fetch, 'styleguide' queues a\nstyleguide extraction) and identifier carries exactly one lookup key: a domain,\nor an email whose domain is extracted and validated (free email providers and\ndisposable email addresses are not allowed).", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[map[string]any]{ + Name: "identifier", + Usage: "Identifier of the target to prefetch. Provide exactly one of domain or email.", + Required: true, + BodyPath: "identifier", + }, + &requestflag.Flag[string]{ + Name: "type", + Usage: "What to prefetch: 'brand' warms the brand data cache, 'styleguide' warms the styleguide cache.", + Required: true, + BodyPath: "type", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional tags for tracking usage. Up to 20 tags, each 1 to 50 characters.", + BodyPath: "tags", + }, + &requestflag.Flag[int64]{ + Name: "timeout-ms", + Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).", + BodyPath: "timeoutMS", + }, + }, + Action: handleUtilityPrefetch, + HideHelpCommand: true, +} + +func handleUtilityPrefetch(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + ApplicationJSON, + false, + ) + if err != nil { + return err + } + + params := contextdev.UtilityPrefetchParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Utility.Prefetch(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "utility prefetch", + Transform: transform, + }) +} diff --git a/pkg/cmd/utility_test.go b/pkg/cmd/utility_test.go new file mode 100644 index 0000000..8f43739 --- /dev/null +++ b/pkg/cmd/utility_test.go @@ -0,0 +1,42 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "testing" + + "github.com/context-dot-dev/context-dev-cli/internal/mocktest" +) + +func TestUtilityPrefetch(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "utility", "prefetch", + "--identifier", "{domain: xxx}", + "--type", "brand", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1000", + ) + }) + + t.Run("piping data", func(t *testing.T) { + // Test piping YAML data over stdin + pipeData := []byte("" + + "identifier:\n" + + " domain: xxx\n" + + "type: brand\n" + + "tags:\n" + + " - production\n" + + " - team-alpha\n" + + "timeoutMS: 1000\n") + mocktest.TestRunMockTestWithPipeAndFlags( + t, pipeData, + "--api-key", "string", + "utility", "prefetch", + ) + }) +} diff --git a/pkg/cmd/version.go b/pkg/cmd/version.go new file mode 100644 index 0000000..a82500c --- /dev/null +++ b/pkg/cmd/version.go @@ -0,0 +1,5 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +const Version = "1.9.0" // x-release-please-version diff --git a/pkg/cmd/web.go b/pkg/cmd/web.go new file mode 100644 index 0000000..bd2b64d --- /dev/null +++ b/pkg/cmd/web.go @@ -0,0 +1,1456 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "context" + "fmt" + + "github.com/context-dot-dev/context-dev-cli/internal/apiquery" + "github.com/context-dot-dev/context-dev-cli/internal/requestflag" + "github.com/context-dot-dev/context-go-sdk/v2" + "github.com/context-dot-dev/context-go-sdk/v2/option" + "github.com/tidwall/gjson" + "github.com/urfave/cli/v3" +) + +var webExtract = requestflag.WithInnerFlags(cli.Command{ + Name: "extract", + Usage: "Crawl a website, use the provided JSON Schema and instructions to prioritize\nrelevant internal links, and extract structured data from the selected pages.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[map[string]any]{ + Name: "schema", + Usage: "JSON Schema for the returned data object. Image fields such as `image_urls` or `product_photos` automatically make page image references available to extraction, so product data and photos can be returned in one call. TypeScript Zod users can pass a JSON Schema generated from a Zod object; Python users can pass the equivalent JSON Schema object.", + Required: true, + BodyPath: "schema", + }, + &requestflag.Flag[string]{ + Name: "url", + Usage: "The starting website URL to crawl and extract from. Must include http:// or https://.", + Required: true, + BodyPath: "url", + }, + &requestflag.Flag[[]map[string]any]{ + Name: "action", + Usage: "Optional browser actions executed in order on the requested page after it loads, before links are discovered or additional pages are crawled. Requires a paid plan. When actions are provided and stopAfterMs is omitted, the crawl budget defaults to 110000 ms.", + BodyPath: "actions", + }, + &requestflag.Flag[bool]{ + Name: "fact-check", + Usage: "When true, every returned value must be grounded in facts stated on the page; fields that cannot be supported by the page are returned as null/empty. When false (default), the model may make reasonable inferences and derivations from the page content (e.g. ideal customer, competitor analysis, recommendations) while keeping verifiable specifics (names, quotes, URLs, dates, metrics) faithful to the source.", + Default: false, + BodyPath: "factCheck", + }, + &requestflag.Flag[bool]{ + Name: "follow-subdomains", + Usage: "When true, follow links on subdomains of the starting URL's domain.", + Default: false, + BodyPath: "followSubdomains", + }, + &requestflag.Flag[bool]{ + Name: "include-frames", + Usage: "When true, iframe contents are included in Markdown before extraction.", + Default: false, + BodyPath: "includeFrames", + }, + &requestflag.Flag[string]{ + Name: "instructions", + Usage: "Optional extraction guidance, such as which facts to prioritize or how to interpret fields in the schema.", + BodyPath: "instructions", + }, + &requestflag.Flag[int64]{ + Name: "max-age-ms", + Usage: "Return cached scrape results if a prior scrape for the same parameters is younger than this many milliseconds. Defaults to 7 days (604800000 ms).", + Default: 604800000, + BodyPath: "maxAgeMs", + }, + &requestflag.Flag[int64]{ + Name: "max-depth", + Usage: "Optional maximum link depth from the starting URL (0 = only the starting page). If omitted, there is no crawl depth limit.", + BodyPath: "maxDepth", + }, + &requestflag.Flag[int64]{ + Name: "max-pages", + Usage: "Maximum number of pages to analyze for extraction. Hard cap: 50. Defaults to 5.", + Default: 5, + BodyPath: "maxPages", + }, + &requestflag.Flag[map[string]any]{ + Name: "pdf", + Default: map[string]any{"shouldParse": true}, + BodyPath: "pdf", + }, + &requestflag.Flag[bool]{ + Name: "settle-animations", + Usage: "When true, waits briefly for CSS and transition animations to settle before extracting each crawled page. Defaults to false. This adds a bit of latency in exchange for more stable output on animated pages.", + Default: false, + BodyPath: "settleAnimations", + }, + &requestflag.Flag[int64]{ + Name: "stop-after-ms", + Usage: "Soft time budget for the crawl in milliseconds. Min: 10000 (10s). Max: 110000 (110s). Defaults to 80000 (80s), or 110000 (110s) when browser actions are provided.", + Default: 80000, + BodyPath: "stopAfterMs", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional tags for tracking usage. Up to 20 tags, each 1 to 50 characters.", + BodyPath: "tags", + }, + &requestflag.Flag[int64]{ + Name: "timeout-ms", + Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).", + BodyPath: "timeoutMS", + }, + &requestflag.Flag[int64]{ + Name: "wait-for-ms", + Usage: "Optional browser wait time in milliseconds after initial page load for each crawled page.", + BodyPath: "waitForMs", + }, + }, + Action: handleWebExtract, + HideHelpCommand: true, +}, map[string][]requestflag.HasOuterFlag{ + "pdf": { + &requestflag.InnerFlag[int64]{ + Name: "pdf.end", + Usage: "Last 1-based PDF page to parse. Must be greater than or equal to start when both are provided.", + InnerField: "end", + }, + &requestflag.InnerFlag[bool]{ + Name: "pdf.should-parse", + Usage: "When true, PDF pages are fetched and parsed. When false, PDF pages are skipped.", + InnerField: "shouldParse", + }, + &requestflag.InnerFlag[int64]{ + Name: "pdf.start", + Usage: "First 1-based PDF page to parse.", + InnerField: "start", + }, + }, +}) + +var webExtractCompetitors = cli.Command{ + Name: "extract-competitors", + Usage: "Analyze a company's landing page and web search evidence to return direct\ncompetitors for the same product or market.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "domain", + Usage: "Company domain to analyze, such as `stripe.com`. Full http(s) URLs are accepted and normalized to their domain.", + Required: true, + QueryPath: "domain", + }, + &requestflag.Flag[int64]{ + Name: "num-competitors", + Usage: "Exact number of direct competitors to return. Defaults to 5.", + Default: 5, + QueryPath: "numCompetitors", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional comma-separated caller-defined tags for tracking this request. Tags are recorded on the request's usage log and can be used to filter usage on the dashboard usage page. Up to 20 tags, each 1-50 characters.", + QueryPath: "tags", + }, + &requestflag.Flag[int64]{ + Name: "timeout-ms", + Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).", + QueryPath: "timeoutMS", + }, + }, + Action: handleWebExtractCompetitors, + HideHelpCommand: true, +} + +var webExtractFonts = cli.Command{ + Name: "extract-fonts", + Usage: "Scrape font information from a website including font families, usage\nstatistics, fallbacks, and element/word counts.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "direct-url", + Usage: "A specific URL to fetch fonts from directly, bypassing domain resolution (e.g., 'https://example.com/design-system'). When provided, fonts are extracted from this exact URL. You must provide either 'domain' or 'directUrl', but not both.", + QueryPath: "directUrl", + }, + &requestflag.Flag[string]{ + Name: "domain", + Usage: "Domain name to extract fonts from (e.g., 'example.com', 'google.com'). The domain will be automatically normalized and validated. You must provide either 'domain' or 'directUrl', but not both.", + QueryPath: "domain", + }, + &requestflag.Flag[*int64]{ + Name: "max-age-ms", + Usage: "Maximum age in milliseconds for cached brand data before the API performs a hard refresh. Defaults to 3 months (7776000000 ms). Values below 1 day (86400000 ms) are clamped to 1 day; values above 1 year (31536000000 ms) are clamped to 1 year.", + Default: requestflag.Ptr[int64](7776000000), + QueryPath: "maxAgeMs", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional comma-separated caller-defined tags for tracking this request. Tags are recorded on the request's usage log and can be used to filter usage on the dashboard usage page. Up to 20 tags, each 1-50 characters.", + QueryPath: "tags", + }, + &requestflag.Flag[int64]{ + Name: "timeout-ms", + Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).", + QueryPath: "timeoutMS", + }, + }, + Action: handleWebExtractFonts, + HideHelpCommand: true, +} + +var webExtractStyleguide = cli.Command{ + Name: "extract-styleguide", + Usage: "Extract a comprehensive design system from a website including colors,\ntypography, spacing, shadows, and UI components.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "color-scheme", + Usage: "Optional browser color scheme to emulate for websites that respond to prefers-color-scheme. This value is part of the styleguide cache key.", + QueryPath: "colorScheme", + }, + &requestflag.Flag[string]{ + Name: "direct-url", + Usage: "A specific URL to fetch the styleguide from directly, bypassing domain resolution (e.g., 'https://example.com/design-system'). When provided, the styleguide is extracted from this exact URL. You must provide either 'domain' or 'directUrl', but not both.", + QueryPath: "directUrl", + }, + &requestflag.Flag[string]{ + Name: "domain", + Usage: "Domain name to extract styleguide from (e.g., 'example.com', 'google.com'). The domain will be automatically normalized and validated. You must provide either 'domain' or 'directUrl', but not both.", + QueryPath: "domain", + }, + &requestflag.Flag[*int64]{ + Name: "max-age-ms", + Usage: "Maximum age in milliseconds for cached brand data before the API performs a hard refresh. Defaults to 3 months (7776000000 ms). Values below 1 day (86400000 ms) are clamped to 1 day; values above 1 year (31536000000 ms) are clamped to 1 year.", + Default: requestflag.Ptr[int64](7776000000), + QueryPath: "maxAgeMs", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional comma-separated caller-defined tags for tracking this request. Tags are recorded on the request's usage log and can be used to filter usage on the dashboard usage page. Up to 20 tags, each 1-50 characters.", + QueryPath: "tags", + }, + &requestflag.Flag[int64]{ + Name: "timeout-ms", + Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).", + QueryPath: "timeoutMS", + }, + }, + Action: handleWebExtractStyleguide, + HideHelpCommand: true, +} + +var webScreenshot = requestflag.WithInnerFlags(cli.Command{ + Name: "screenshot", + Usage: "Capture a screenshot of a website.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[bool]{ + Name: "clear-popups", + Usage: "Optional parameter for comprehensive popup cleanup. If 'true', the browser dismisses detected cookie/consent UI and clears other detected obstructive popups and overlays before capture. If 'false' or not provided, this parameter requests no cleanup; handleCookiePopup can still request cookie/consent handling independently.", + Default: false, + QueryPath: "clearPopups", + }, + &requestflag.Flag[string]{ + Name: "color-scheme", + Usage: "Optional parameter to choose the site's visual theme in the screenshot. Use 'light' or 'dark' when the site offers both appearances.", + QueryPath: "colorScheme", + }, + &requestflag.Flag[string]{ + Name: "country", + Usage: "Fetch the target page through a residential proxy in this country (ISO 3166-1 alpha-2).", + QueryPath: "country", + }, + &requestflag.Flag[string]{ + Name: "direct-url", + Usage: "A specific URL to screenshot directly, bypassing domain resolution (e.g., 'https://example.com/pricing'). When provided, the screenshot is taken of this exact URL. You must provide either 'domain' or 'directUrl', but not both.", + QueryPath: "directUrl", + }, + &requestflag.Flag[string]{ + Name: "domain", + Usage: "Domain name to take screenshot of (e.g., 'example.com', 'google.com'). The domain will be automatically normalized and validated. You must provide either 'domain' or 'directUrl', but not both.", + QueryPath: "domain", + }, + &requestflag.Flag[string]{ + Name: "full-screenshot", + Usage: "Optional parameter to determine screenshot type. If 'true', takes a full page screenshot capturing all content. If 'false' or not provided, takes a viewport screenshot (standard browser view).", + QueryPath: "fullScreenshot", + }, + &requestflag.Flag[bool]{ + Name: "handle-cookie-popup", + Usage: "Optional parameter to control cookie/consent popup handling. If 'true', we dismiss cookie banner before capture. If 'false' or not provided, captures the page without that step.", + Default: false, + QueryPath: "handleCookiePopup", + }, + &requestflag.Flag[*int64]{ + Name: "max-age-ms", + Usage: "Return a cached screenshot if a prior screenshot for the same parameters exists and is younger than this many milliseconds. Defaults to 1 day (86400000 ms) when omitted. Max is 30 days (2592000000 ms). Set to 0 to always capture fresh.", + Default: requestflag.Ptr[int64](86400000), + QueryPath: "maxAgeMs", + }, + &requestflag.Flag[string]{ + Name: "page", + Usage: "Optional parameter to specify which page type to screenshot. If provided, the system will scrape the domain's links and use heuristics to find the most appropriate URL for the specified page type (30 supported languages). If not provided, screenshots the main domain landing page. Only applicable when using 'domain', not 'directUrl'.", + QueryPath: "page", + }, + &requestflag.Flag[*int64]{ + Name: "scroll-offset", + Usage: "Optional vertical scroll offset in pixels for capturing a long page in viewport-sized chunks. When provided, the full page is captured once and the returned image is the viewport-sized slice that begins at this Y offset (e.g. request scrollOffset=0, then 1080, then 2160 to walk a 1920x1080 landing page top to bottom). The final slice may be shorter than the viewport height. Takes precedence over fullScreenshot. Max: 100000.", + QueryPath: "scrollOffset", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional comma-separated caller-defined tags for tracking this request. Tags are recorded on the request's usage log and can be used to filter usage on the dashboard usage page. Up to 20 tags, each 1-50 characters.", + QueryPath: "tags", + }, + &requestflag.Flag[int64]{ + Name: "timeout-ms", + Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).", + QueryPath: "timeoutMS", + }, + &requestflag.Flag[map[string]any]{ + Name: "viewport", + Usage: "Optional browser viewport dimensions for the screenshot. Defaults to 1920x1080.", + Default: map[string]any{"width": 1920, "height": 1080}, + QueryPath: "viewport", + }, + &requestflag.Flag[*int64]{ + Name: "wait-for-ms", + Usage: "Optional browser wait time in milliseconds after initial page load before taking the screenshot. Min: 0. Max: 30000 (30 seconds). Defaults to 3000 ms when omitted.", + Default: requestflag.Ptr[int64](3000), + QueryPath: "waitForMs", + }, + &requestflag.Flag[string]{ + Name: "zdr", + Usage: "Set to enabled to bypass shared caches and omit request and response content from retained usage logs. Requires zero data retention to be enabled for your organization (contact support@context.dev), otherwise the request fails with ZDR_NOT_ENABLED. Successful ZDR responses include X-Context-ZDR: true.", + Default: "disabled", + QueryPath: "zdr", + }, + }, + Action: handleWebScreenshot, + HideHelpCommand: true, +}, map[string][]requestflag.HasOuterFlag{ + "viewport": { + &requestflag.InnerFlag[int64]{ + Name: "viewport.height", + Usage: "Viewport height in pixels.", + InnerField: "height", + }, + &requestflag.InnerFlag[int64]{ + Name: "viewport.width", + Usage: "Viewport width in pixels.", + InnerField: "width", + }, + }, +}) + +var webSearch = requestflag.WithInnerFlags(cli.Command{ + Name: "search", + Usage: "Search the web and optionally scrape each result to Markdown in one round-trip.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "query", + Usage: "Search query. Accepts natural language as well as Google-style search operators such as `site:`, `-site:`, `inurl:`, `intitle:`, quoted phrases, and `OR`.", + Required: true, + BodyPath: "query", + }, + &requestflag.Flag[string]{ + Name: "country", + Usage: "Two-letter ISO 3166-1 alpha-2 country code to localize results to a specific country (maps to Google's `gl` parameter). Example: \"us\", \"gb\", \"de\".", + BodyPath: "country", + }, + &requestflag.Flag[[]string]{ + Name: "exclude-domain", + Usage: `Blocklist — drop results from these domains. Example: ["pinterest.com", "reddit.com"].`, + BodyPath: "excludeDomains", + }, + &requestflag.Flag[string]{ + Name: "freshness", + Usage: "Restrict results to content published within this window.", + BodyPath: "freshness", + }, + &requestflag.Flag[[]string]{ + Name: "include-domain", + Usage: `Allowlist — only return results from these domains. Example: ["arxiv.org", "github.com"].`, + BodyPath: "includeDomains", + }, + &requestflag.Flag[map[string]any]{ + Name: "markdown-options", + Usage: "Inline Markdown scraping for each result. Set `enabled: true` to activate.", + BodyPath: "markdownOptions", + }, + &requestflag.Flag[int64]{ + Name: "num-results", + Usage: "Number of results to request and return (10–100). Defaults to 10.", + Default: 10, + BodyPath: "numResults", + }, + &requestflag.Flag[bool]{ + Name: "query-fanout", + Usage: "Expand the query into multiple parallel variants for broader recall.", + BodyPath: "queryFanout", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional tags for tracking usage. Up to 20 tags, each 1 to 50 characters.", + BodyPath: "tags", + }, + &requestflag.Flag[int64]{ + Name: "timeout-ms", + Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).", + BodyPath: "timeoutMS", + }, + }, + Action: handleWebSearch, + HideHelpCommand: true, +}, map[string][]requestflag.HasOuterFlag{ + "markdown-options": { + &requestflag.InnerFlag[bool]{ + Name: "markdown-options.enabled", + Usage: "Scrape each result to Markdown. Off by default to keep search cheap and fast.", + InnerField: "enabled", + }, + &requestflag.InnerFlag[bool]{ + Name: "markdown-options.include-frames", + Usage: "Render iframe contents into the Markdown.", + InnerField: "includeFrames", + }, + &requestflag.InnerFlag[bool]{ + Name: "markdown-options.include-images", + Usage: "Emit image references in the Markdown.", + InnerField: "includeImages", + }, + &requestflag.InnerFlag[bool]{ + Name: "markdown-options.include-links", + Usage: "Keep hyperlinks in the Markdown.", + InnerField: "includeLinks", + }, + &requestflag.InnerFlag[int64]{ + Name: "markdown-options.max-age-ms", + Usage: "Cache TTL in ms for scraped Markdown keyed by URL + options. Default 1 day, max 30 days. Set to 0 to force a fresh scrape.", + InnerField: "maxAgeMs", + }, + &requestflag.InnerFlag[map[string]any]{ + Name: "markdown-options.pdf", + Usage: "PDF handling. Use start/end to bound text extraction and OCR to a page range.", + InnerField: "pdf", + }, + &requestflag.InnerFlag[bool]{ + Name: "markdown-options.shorten-base64-images", + Usage: "Truncate inline base64 image payloads to keep responses small.", + InnerField: "shortenBase64Images", + }, + &requestflag.InnerFlag[int64]{ + Name: "markdown-options.timeout-ms", + Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).", + InnerField: "timeoutMS", + }, + &requestflag.InnerFlag[bool]{ + Name: "markdown-options.use-main-content-only", + Usage: "Strip nav, header, footer, and sidebar — keep only the primary article content.", + InnerField: "useMainContentOnly", + }, + &requestflag.InnerFlag[int64]{ + Name: "markdown-options.wait-for-ms", + Usage: "Extra wait after page load before rendering, in ms (0–30000). Useful for JS-heavy pages.", + InnerField: "waitForMs", + }, + }, +}) + +var webWebCrawlMd = requestflag.WithInnerFlags(cli.Command{ + Name: "web-crawl-md", + Usage: "Performs a crawl starting from a given URL, extracts page content as Markdown,\nand returns results for all crawled pages.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "url", + Usage: "The starting URL for the crawl (must include http:// or https:// protocol)", + Required: true, + BodyPath: "url", + }, + &requestflag.Flag[string]{ + Name: "country", + Usage: "Fetch the target page through a residential proxy in this country (ISO 3166-1 alpha-2).", + BodyPath: "country", + }, + &requestflag.Flag[[]string]{ + Name: "exclude-selector", + Usage: `CSS selectors to remove before each crawled page is converted to Markdown. Applied after includeSelectors. Exclusion takes precedence: an element matching both is removed. Examples: "nav", "footer", ".ad-banner", "[aria-hidden=true]".`, + BodyPath: "excludeSelectors", + }, + &requestflag.Flag[bool]{ + Name: "follow-subdomains", + Usage: "When true, follow links on subdomains of the starting URL's domain (e.g. docs.example.com when starting from example.com). www and apex are always treated as equivalent.", + Default: false, + BodyPath: "followSubdomains", + }, + &requestflag.Flag[bool]{ + Name: "include-frames", + Usage: "When true, the contents of iframes are rendered to Markdown for each crawled page.", + Default: false, + BodyPath: "includeFrames", + }, + &requestflag.Flag[bool]{ + Name: "include-images", + Usage: "Include image references in the Markdown output", + Default: false, + BodyPath: "includeImages", + }, + &requestflag.Flag[bool]{ + Name: "include-links", + Usage: "Preserve hyperlinks in the Markdown output", + Default: true, + BodyPath: "includeLinks", + }, + &requestflag.Flag[[]string]{ + Name: "include-selector", + Usage: `CSS selectors. When provided, only matching HTML subtrees (and their descendants) are kept before each crawled page is converted to Markdown. When omitted, the entire document is kept. Examples: "article.main", "#content", "[role=main]".`, + BodyPath: "includeSelectors", + }, + &requestflag.Flag[int64]{ + Name: "max-age-ms", + Usage: "Return a cached result if a prior scrape for the same parameters exists and is younger than this many milliseconds. Defaults to 1 day (86400000 ms) when omitted. Max is 30 days (2592000000 ms). Set to 0 to always scrape fresh.", + Default: 86400000, + BodyPath: "maxAgeMs", + }, + &requestflag.Flag[int64]{ + Name: "max-depth", + Usage: "Maximum link depth from the starting URL (0 = only the starting page)", + BodyPath: "maxDepth", + }, + &requestflag.Flag[int64]{ + Name: "max-pages", + Usage: "Maximum number of pages to crawl. Hard cap: 500.", + Default: 100, + BodyPath: "maxPages", + }, + &requestflag.Flag[map[string]any]{ + Name: "pdf", + Usage: "PDF parsing controls. Use start/end to limit text extraction and embedded-image detection/OCR to an inclusive 1-based page range.", + Default: map[string]any{"shouldParse": true, "ocr": false}, + BodyPath: "pdf", + }, + &requestflag.Flag[bool]{ + Name: "settle-animations", + Usage: "When true, waits briefly for CSS and transition animations to settle before extracting each crawled page. Defaults to false. This adds a bit of latency in exchange for more stable output on animated pages.", + Default: false, + BodyPath: "settleAnimations", + }, + &requestflag.Flag[bool]{ + Name: "shorten-base64-images", + Usage: "Truncate base64-encoded image data in the Markdown output", + Default: true, + BodyPath: "shortenBase64Images", + }, + &requestflag.Flag[int64]{ + Name: "stop-after-ms", + Usage: "Soft time budget for the crawl in milliseconds. After each scrape, the crawler checks the elapsed time and, if exceeded, returns the pages collected so far instead of continuing. Min: 10000 (10s). Max: 110000 (110s). Default: 80000 (80s).", + Default: 80000, + BodyPath: "stopAfterMs", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional tags for tracking usage. Up to 20 tags, each 1 to 50 characters.", + BodyPath: "tags", + }, + &requestflag.Flag[int64]{ + Name: "timeout-ms", + Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).", + BodyPath: "timeoutMS", + }, + &requestflag.Flag[string]{ + Name: "url-regex", + Usage: "Regex pattern. Only URLs matching this pattern will be followed and scraped. An automatic prefix scope in the form ^ follows a redirect of the starting page.", + BodyPath: "urlRegex", + }, + &requestflag.Flag[bool]{ + Name: "use-main-content-only", + Usage: "Extract only the main content, stripping headers, footers, sidebars, and navigation", + Default: false, + BodyPath: "useMainContentOnly", + }, + &requestflag.Flag[int64]{ + Name: "wait-for-ms", + Usage: "Browser wait time in milliseconds after initial page load for each crawled page. Defaults to 3500 (3.5 seconds). Min: 0. Max: 30000 (30 seconds).", + Default: 3500, + BodyPath: "waitForMs", + }, + &requestflag.Flag[string]{ + Name: "zdr", + Usage: "Set to enabled to bypass shared caches and omit request and response content from retained usage logs. Requires zero data retention to be enabled for your organization (contact support@context.dev), otherwise the request fails with ZDR_NOT_ENABLED. Successful ZDR responses include X-Context-ZDR: true.", + Default: "disabled", + BodyPath: "zdr", + }, + }, + Action: handleWebWebCrawlMd, + HideHelpCommand: true, +}, map[string][]requestflag.HasOuterFlag{ + "pdf": { + &requestflag.InnerFlag[int64]{ + Name: "pdf.end", + Usage: "Last 1-based PDF page to parse. When omitted, parsing ends at the last page. Must be greater than or equal to start when both are provided.", + InnerField: "end", + }, + &requestflag.InnerFlag[bool]{ + Name: "pdf.ocr", + Usage: "When true, OCR the selected PDF pages that have no usable text layer (scans), replacing each recovered page's text with the OCR result while pages with a real text layer keep it. Billed at 1 credit per page OCR actually recovered, on top of the base request cost.", + InnerField: "ocr", + }, + &requestflag.InnerFlag[bool]{ + Name: "pdf.should-parse", + Usage: "When true, PDF pages are fetched and parsed. When false, PDF pages are skipped entirely (not included in results and not counted as failures).", + InnerField: "shouldParse", + }, + &requestflag.InnerFlag[int64]{ + Name: "pdf.start", + Usage: "First 1-based PDF page to parse. When omitted, parsing starts at the first page.", + InnerField: "start", + }, + }, +}) + +var webWebScrapeHTML = requestflag.WithInnerFlags(cli.Command{ + Name: "web-scrape-html", + Usage: "Scrapes the given URL and returns the raw HTML content of the page. The base\nrequest costs 1 credit; requests with browser actions cost 2 credits.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "url", + Usage: "Full URL to scrape (must include http:// or https:// protocol)", + Required: true, + QueryPath: "url", + }, + &requestflag.Flag[any]{ + Name: "action", + Usage: "Optional browser actions executed in array order after the page loads and before content is captured. Requires a paid plan. Send a JSON array in the query parameter. Maximum: 5 actions.", + QueryPath: "actions", + }, + &requestflag.Flag[string]{ + Name: "country", + Usage: "Fetch the target page through a residential proxy in this country (ISO 3166-1 alpha-2).", + QueryPath: "country", + }, + &requestflag.Flag[any]{ + Name: "exclude-selector", + Usage: `CSS selectors to remove from the result. Applied after includeSelectors. Exclusion takes precedence: an element matching both is removed. Examples: "nav", "footer", ".ad-banner", "[aria-hidden=true]".`, + QueryPath: "excludeSelectors", + }, + &requestflag.Flag[map[string]any]{ + Name: "headers", + Usage: "Optional outbound HTTP headers forwarded only to the target URL, sent as deep-object query params such as headers[X-Custom]=value. When provided, caching is bypassed: the result is neither read from nor written to cache.", + QueryPath: "headers", + }, + &requestflag.Flag[bool]{ + Name: "include-frames", + Usage: "When true, iframes are rendered inline into the returned HTML.", + Default: false, + QueryPath: "includeFrames", + }, + &requestflag.Flag[any]{ + Name: "include-selector", + Usage: `CSS selectors. When provided, only matching subtrees (and their descendants) are kept and everything else is dropped. When omitted, the entire document is kept. Examples: "article.main", "#content", "[role=main]".`, + QueryPath: "includeSelectors", + }, + &requestflag.Flag[*int64]{ + Name: "max-age-ms", + Usage: "Return a cached result if a prior scrape for the same parameters exists and is younger than this many milliseconds. Defaults to 1 day (86400000 ms) when omitted. Max is 30 days (2592000000 ms). Set to 0 to always scrape fresh.", + Default: requestflag.Ptr[int64](86400000), + QueryPath: "maxAgeMs", + }, + &requestflag.Flag[map[string]any]{ + Name: "pdf", + Usage: "PDF parsing controls. Use start/end to limit text extraction and embedded-image detection/OCR to an inclusive 1-based page range.", + Default: map[string]any{"shouldParse": true, "ocr": false}, + QueryPath: "pdf", + }, + &requestflag.Flag[bool]{ + Name: "settle-animations", + Usage: "When true, waits briefly for CSS and transition animations to settle before extracting HTML. Defaults to false. This adds a bit of latency in exchange for more stable output on animated pages.", + Default: false, + QueryPath: "settleAnimations", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional comma-separated caller-defined tags for tracking this request. Tags are recorded on the request's usage log and can be used to filter usage on the dashboard usage page. Up to 20 tags, each 1-50 characters.", + QueryPath: "tags", + }, + &requestflag.Flag[int64]{ + Name: "timeout-ms", + Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).", + QueryPath: "timeoutMS", + }, + &requestflag.Flag[bool]{ + Name: "use-main-content-only", + Usage: "When true, return only the page's main content in the HTML response, excluding headers, footers, sidebars, and navigation when detectable.", + Default: false, + QueryPath: "useMainContentOnly", + }, + &requestflag.Flag[*int64]{ + Name: "wait-for-ms", + Usage: "Optional browser wait time in milliseconds after initial page load. Min: 0. Max: 30000 (30 seconds).", + QueryPath: "waitForMs", + }, + &requestflag.Flag[string]{ + Name: "zdr", + Usage: "Set to enabled to bypass shared caches and omit request and response content from retained usage logs. Requires zero data retention to be enabled for your organization (contact support@context.dev), otherwise the request fails with ZDR_NOT_ENABLED. Successful ZDR responses include X-Context-ZDR: true.", + Default: "disabled", + QueryPath: "zdr", + }, + }, + Action: handleWebWebScrapeHTML, + HideHelpCommand: true, +}, map[string][]requestflag.HasOuterFlag{ + "pdf": { + &requestflag.InnerFlag[int64]{ + Name: "pdf.end", + Usage: "Last 1-based PDF page to parse. When omitted, parsing ends at the last page. Must be greater than or equal to start when both are provided.", + InnerField: "end", + }, + &requestflag.InnerFlag[bool]{ + Name: "pdf.ocr", + Usage: "When true, OCR the selected PDF pages that have no usable text layer (scans), replacing each recovered page's text with the OCR result while pages with a real text layer keep it. Billed at 1 credit per page OCR actually recovered, on top of the base request cost. When false, no OCR runs.", + InnerField: "ocr", + }, + &requestflag.InnerFlag[bool]{ + Name: "pdf.should-parse", + Usage: "When true, PDF URLs are fetched and parsed. When false, PDF URLs are skipped and a 400 PDF_SKIPPED is returned.", + InnerField: "shouldParse", + }, + &requestflag.InnerFlag[int64]{ + Name: "pdf.start", + Usage: "First 1-based PDF page to parse. When omitted, parsing starts at the first page.", + InnerField: "start", + }, + }, +}) + +var webWebScrapeImages = requestflag.WithInnerFlags(cli.Command{ + Name: "web-scrape-images", + Usage: "Extract image assets from a web page, including standard URLs, inline SVGs, data\nURIs, responsive image sources, metadata, CSS backgrounds, video posters, and\nembeds. The base request costs 1 credit, or 2 credits with browser actions. When\nenrichment is enabled, the entire call costs 5 credits, including requests that\nalso use actions.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "url", + Usage: "Page URL to inspect. Must include http:// or https://.", + Required: true, + QueryPath: "url", + }, + &requestflag.Flag[any]{ + Name: "action", + Usage: "Optional browser actions executed in array order after the page loads and before content is captured. Requires a paid plan. Send a JSON array in the query parameter. Maximum: 5 actions.", + QueryPath: "actions", + }, + &requestflag.Flag[bool]{ + Name: "dedupe", + Usage: "When true, visually duplicate images are removed: every image is loaded and perceptually hashed, and only the highest-resolution copy of each duplicate group is kept. Images that cannot be downloaded or hashed are kept. Default: false.", + Default: false, + QueryPath: "dedupe", + }, + &requestflag.Flag[map[string]any]{ + Name: "enrichment", + Usage: "Optional per-image processing, sent as deep-object query params such as enrichment[resolution]=true.", + QueryPath: "enrichment", + }, + &requestflag.Flag[map[string]any]{ + Name: "headers", + Usage: "Optional outbound HTTP headers forwarded only to the target URL, sent as deep-object query params such as headers[X-Custom]=value. When provided, caching is bypassed: the result is neither read from nor written to cache.", + QueryPath: "headers", + }, + &requestflag.Flag[*int64]{ + Name: "max-age-ms", + Usage: "Reuse a cached result this many milliseconds old or newer. Default: 86400000 (1 day). Set to 0 to bypass cache. Maximum: 2592000000 (30 days).", + Default: requestflag.Ptr[int64](86400000), + QueryPath: "maxAgeMs", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional comma-separated caller-defined tags for tracking this request. Tags are recorded on the request's usage log and can be used to filter usage on the dashboard usage page. Up to 20 tags, each 1-50 characters.", + QueryPath: "tags", + }, + &requestflag.Flag[int64]{ + Name: "timeout-ms", + Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).", + QueryPath: "timeoutMS", + }, + &requestflag.Flag[*int64]{ + Name: "wait-for-ms", + Usage: "Optional browser wait time in milliseconds after initial page load before collecting images. Min: 0. Max: 30000 (30 seconds).", + QueryPath: "waitForMs", + }, + }, + Action: handleWebWebScrapeImages, + HideHelpCommand: true, +}, map[string][]requestflag.HasOuterFlag{ + "enrichment": { + &requestflag.InnerFlag[bool]{ + Name: "enrichment.classification", + Usage: "Classify each image by visual asset type.", + InnerField: "classification", + }, + &requestflag.InnerFlag[bool]{ + Name: "enrichment.hosted-url", + Usage: "Host materializable images on the Brand.dev CDN and return their URL and MIME type.", + InnerField: "hostedUrl", + }, + &requestflag.InnerFlag[int64]{ + Name: "enrichment.max-time-per-ms", + Usage: "Per-image enrichment timeout in milliseconds. Default: 30000. Maximum: 60000.", + InnerField: "maxTimePerMs", + }, + &requestflag.InnerFlag[bool]{ + Name: "enrichment.resolution", + Usage: "Measure image width and height when possible.", + InnerField: "resolution", + }, + }, +}) + +var webWebScrapeMd = requestflag.WithInnerFlags(cli.Command{ + Name: "web-scrape-md", + Usage: "Scrapes the given URL into LLM usable Markdown. Inspect key_metadata on JSON\nresponses from a recognized API key; use error_code to distinguish stable\nfailure categories.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "url", + Usage: "Full URL to scrape into LLM usable Markdown (must include http:// or https:// protocol)", + Required: true, + QueryPath: "url", + }, + &requestflag.Flag[any]{ + Name: "action", + Usage: "Optional browser actions executed in array order after the page loads and before content is captured. Requires a paid plan. Send a JSON array in the query parameter. Maximum: 5 actions.", + QueryPath: "actions", + }, + &requestflag.Flag[string]{ + Name: "country", + Usage: "Fetch the target page through a residential proxy in this country (ISO 3166-1 alpha-2).", + QueryPath: "country", + }, + &requestflag.Flag[any]{ + Name: "exclude-selector", + Usage: `CSS selectors to remove before conversion to Markdown. Applied after includeSelectors. Exclusion takes precedence: an element matching both is removed. Examples: "nav", "footer", ".ad-banner", "[aria-hidden=true]".`, + QueryPath: "excludeSelectors", + }, + &requestflag.Flag[map[string]any]{ + Name: "headers", + Usage: "Optional outbound HTTP headers forwarded only to the target URL, sent as deep-object query params such as headers[X-Custom]=value. When provided, caching is bypassed: the result is neither read from nor written to cache.", + QueryPath: "headers", + }, + &requestflag.Flag[bool]{ + Name: "include-frames", + Usage: "When true, the contents of iframes are rendered to Markdown.", + Default: false, + QueryPath: "includeFrames", + }, + &requestflag.Flag[bool]{ + Name: "include-html", + Usage: "When true, the response also includes an `html` field with the page HTML the Markdown was converted from — the same body the Scrape HTML endpoint returns for the equivalent request.", + Default: false, + QueryPath: "includeHTML", + }, + &requestflag.Flag[bool]{ + Name: "include-images", + Usage: "Include image references in Markdown output", + Default: false, + QueryPath: "includeImages", + }, + &requestflag.Flag[bool]{ + Name: "include-links", + Usage: "Preserve hyperlinks in Markdown output", + Default: true, + QueryPath: "includeLinks", + }, + &requestflag.Flag[any]{ + Name: "include-selector", + Usage: `CSS selectors. When provided, only matching HTML subtrees (and their descendants) are kept before conversion to Markdown. When omitted, the entire document is kept. Examples: "article.main", "#content", "[role=main]".`, + QueryPath: "includeSelectors", + }, + &requestflag.Flag[*int64]{ + Name: "max-age-ms", + Usage: "Return a cached result if a prior scrape for the same parameters exists and is younger than this many milliseconds. Defaults to 1 day (86400000 ms) when omitted. Max is 30 days (2592000000 ms). Set to 0 to always scrape fresh.", + Default: requestflag.Ptr[int64](86400000), + QueryPath: "maxAgeMs", + }, + &requestflag.Flag[map[string]any]{ + Name: "pdf", + Usage: "PDF parsing controls. Use start/end to limit text extraction and embedded-image detection/OCR to an inclusive 1-based page range.", + Default: map[string]any{"shouldParse": true, "ocr": false}, + QueryPath: "pdf", + }, + &requestflag.Flag[bool]{ + Name: "settle-animations", + Usage: "When true, waits briefly for CSS and transition animations to settle before converting to Markdown. Defaults to false. This adds a bit of latency in exchange for more stable output on animated pages.", + Default: false, + QueryPath: "settleAnimations", + }, + &requestflag.Flag[bool]{ + Name: "shorten-base64-images", + Usage: "Shorten base64-encoded image data in the Markdown output", + Default: true, + QueryPath: "shortenBase64Images", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional comma-separated caller-defined tags for tracking this request. Tags are recorded on the request's usage log and can be used to filter usage on the dashboard usage page. Up to 20 tags, each 1-50 characters.", + QueryPath: "tags", + }, + &requestflag.Flag[int64]{ + Name: "timeout-ms", + Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).", + QueryPath: "timeoutMS", + }, + &requestflag.Flag[bool]{ + Name: "use-main-content-only", + Usage: "Extract only the main content of the page, excluding headers, footers, sidebars, and navigation", + Default: false, + QueryPath: "useMainContentOnly", + }, + &requestflag.Flag[*int64]{ + Name: "wait-for-ms", + Usage: "Optional browser wait time in milliseconds after initial page load before converting the page to Markdown. Min: 0. Max: 30000 (30 seconds).", + QueryPath: "waitForMs", + }, + &requestflag.Flag[string]{ + Name: "zdr", + Usage: "Set to enabled to bypass shared caches and omit request and response content from retained usage logs. Requires zero data retention to be enabled for your organization (contact support@context.dev), otherwise the request fails with ZDR_NOT_ENABLED. Successful ZDR responses include X-Context-ZDR: true.", + Default: "disabled", + QueryPath: "zdr", + }, + }, + Action: handleWebWebScrapeMd, + HideHelpCommand: true, +}, map[string][]requestflag.HasOuterFlag{ + "pdf": { + &requestflag.InnerFlag[int64]{ + Name: "pdf.end", + Usage: "Last 1-based PDF page to parse. When omitted, parsing ends at the last page. Must be greater than or equal to start when both are provided.", + InnerField: "end", + }, + &requestflag.InnerFlag[bool]{ + Name: "pdf.ocr", + Usage: "When true, OCR the selected PDF pages that have no usable text layer (scans), replacing each recovered page's text with the OCR result while pages with a real text layer keep it. Billed at 1 credit per page OCR actually recovered, on top of the base request cost. When false, no OCR runs.", + InnerField: "ocr", + }, + &requestflag.InnerFlag[bool]{ + Name: "pdf.should-parse", + Usage: "When true, PDF URLs are fetched and parsed. When false, PDF URLs are skipped and a 400 PDF_SKIPPED is returned.", + InnerField: "shouldParse", + }, + &requestflag.InnerFlag[int64]{ + Name: "pdf.start", + Usage: "First 1-based PDF page to parse. When omitted, parsing starts at the first page.", + InnerField: "start", + }, + }, +}) + +var webWebScrapeSitemap = cli.Command{ + Name: "web-scrape-sitemap", + Usage: "Crawl an entire website's sitemap and return all discovered page URLs. Pass\n`search` to have the crawled sitemap filtered down to the pages about a phrase\n(for example `pricing and plans` or `api authentication docs`), most relevant\nfirst — a searched crawl scans the whole sitemap and costs 2 credits instead\nof 1.", + Suggest: true, + Flags: []cli.Flag{ + &requestflag.Flag[string]{ + Name: "domain", + Usage: "Domain to build a sitemap for", + Required: true, + QueryPath: "domain", + }, + &requestflag.Flag[map[string]any]{ + Name: "headers", + Usage: "Optional outbound HTTP headers forwarded only to the target URL, sent as deep-object query params such as headers[X-Custom]=value. When provided, caching is bypassed: the result is neither read from nor written to cache.", + QueryPath: "headers", + }, + &requestflag.Flag[int64]{ + Name: "max-links", + Usage: "Maximum number of links to return from the sitemap crawl. Defaults to 10,000. Minimum is 1, maximum is 100,000.", + Default: 10000, + QueryPath: "maxLinks", + }, + &requestflag.Flag[string]{ + Name: "search", + Usage: "Optional search phrase. When provided, the crawled sitemap is filtered to the pages whose URLs are about that phrase, most relevant first, and the request costs 2 credits instead of 1.", + QueryPath: "search", + }, + &requestflag.Flag[string]{ + Name: "sitemap-url", + Usage: "Optional explicit sitemap URL. When provided, exactly this sitemap is crawled instead of discovering the domain's sitemaps.", + QueryPath: "sitemapUrl", + }, + &requestflag.Flag[[]string]{ + Name: "tag", + Usage: "Optional comma-separated caller-defined tags for tracking this request. Tags are recorded on the request's usage log and can be used to filter usage on the dashboard usage page. Up to 20 tags, each 1-50 characters.", + QueryPath: "tags", + }, + &requestflag.Flag[int64]{ + Name: "timeout-ms", + Usage: "Optional timeout in milliseconds for the request. If the request takes longer than this value, it will be aborted with a 408 status code. Maximum allowed value is 300000ms (5 minutes).", + QueryPath: "timeoutMS", + }, + &requestflag.Flag[string]{ + Name: "url-regex", + Usage: "Optional RE2-compatible regex pattern. Only URLs matching this pattern are returned and counted against maxLinks.", + QueryPath: "urlRegex", + }, + &requestflag.Flag[string]{ + Name: "zdr", + Usage: "Set to enabled to bypass shared caches and omit request and response content from retained usage logs. Requires zero data retention to be enabled for your organization (contact support@context.dev), otherwise the request fails with ZDR_NOT_ENABLED. Successful ZDR responses include X-Context-ZDR: true.", + Default: "disabled", + QueryPath: "zdr", + }, + }, + Action: handleWebWebScrapeSitemap, + HideHelpCommand: true, +} + +func handleWebExtract(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + ApplicationJSON, + false, + ) + if err != nil { + return err + } + + params := contextdev.WebExtractParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Web.Extract(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "web extract", + Transform: transform, + }) +} + +func handleWebExtractCompetitors(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + params := contextdev.WebExtractCompetitorsParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Web.ExtractCompetitors(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "web extract-competitors", + Transform: transform, + }) +} + +func handleWebExtractFonts(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + params := contextdev.WebExtractFontsParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Web.ExtractFonts(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "web extract-fonts", + Transform: transform, + }) +} + +func handleWebExtractStyleguide(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + params := contextdev.WebExtractStyleguideParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Web.ExtractStyleguide(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "web extract-styleguide", + Transform: transform, + }) +} + +func handleWebScreenshot(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + params := contextdev.WebScreenshotParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Web.Screenshot(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "web screenshot", + Transform: transform, + }) +} + +func handleWebSearch(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + ApplicationJSON, + false, + ) + if err != nil { + return err + } + + params := contextdev.WebSearchParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Web.Search(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "web search", + Transform: transform, + }) +} + +func handleWebWebCrawlMd(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + ApplicationJSON, + false, + ) + if err != nil { + return err + } + + params := contextdev.WebWebCrawlMdParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Web.WebCrawlMd(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "web web-crawl-md", + Transform: transform, + }) +} + +func handleWebWebScrapeHTML(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + params := contextdev.WebWebScrapeHTMLParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Web.WebScrapeHTML(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "web web-scrape-html", + Transform: transform, + }) +} + +func handleWebWebScrapeImages(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + params := contextdev.WebWebScrapeImagesParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Web.WebScrapeImages(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "web web-scrape-images", + Transform: transform, + }) +} + +func handleWebWebScrapeMd(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + params := contextdev.WebWebScrapeMdParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Web.WebScrapeMd(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "web web-scrape-md", + Transform: transform, + }) +} + +func handleWebWebScrapeSitemap(ctx context.Context, cmd *cli.Command) error { + client := contextdev.NewClient(getDefaultRequestOptions(cmd)...) + unusedArgs := cmd.Args().Slice() + + if len(unusedArgs) > 0 { + return fmt.Errorf("Unexpected extra arguments: %v", unusedArgs) + } + + options, err := flagOptions( + cmd, + apiquery.NestedQueryFormatBrackets, + apiquery.ArrayQueryFormatComma, + EmptyBody, + false, + ) + if err != nil { + return err + } + + params := contextdev.WebWebScrapeSitemapParams{} + + var res []byte + options = append(options, option.WithResponseBodyInto(&res)) + _, err = client.Web.WebScrapeSitemap(ctx, params, options...) + if err != nil { + return err + } + + obj := gjson.ParseBytes(res) + format := cmd.Root().String("format") + explicitFormat := cmd.Root().IsSet("format") + transform := cmd.Root().String("transform") + return ShowJSON(obj, ShowJSONOpts{ + ExplicitFormat: explicitFormat, + Format: format, + RawOutput: cmd.Root().Bool("raw-output"), + Title: "web web-scrape-sitemap", + Transform: transform, + }) +} diff --git a/pkg/cmd/web_test.go b/pkg/cmd/web_test.go new file mode 100644 index 0000000..acbcf47 --- /dev/null +++ b/pkg/cmd/web_test.go @@ -0,0 +1,604 @@ +// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. + +package cmd + +import ( + "testing" + + "github.com/context-dot-dev/context-dev-cli/internal/mocktest" + "github.com/context-dot-dev/context-dev-cli/internal/requestflag" +) + +func TestWebExtract(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "web", "extract", + "--schema", "{type: bar, properties: bar, required: bar, additionalProperties: bar}", + "--url", "https://example.com", + "--action", "{do: wait, timeMs: 0}", + "--fact-check=true", + "--follow-subdomains=true", + "--include-frames=true", + "--instructions", "instructions", + "--max-age-ms", "0", + "--max-depth", "0", + "--max-pages", "1", + "--pdf", "{end: 1, shouldParse: true, start: 1}", + "--settle-animations=true", + "--stop-after-ms", "10000", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1000", + "--wait-for-ms", "0", + ) + }) + + t.Run("inner flags", func(t *testing.T) { + // Check that inner flags have been set up correctly + requestflag.CheckInnerFlags(webExtract) + + // Alternative argument passing style using inner flags + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "web", "extract", + "--schema", "{type: bar, properties: bar, required: bar, additionalProperties: bar}", + "--url", "https://example.com", + "--action", "{do: wait, timeMs: 0}", + "--fact-check=true", + "--follow-subdomains=true", + "--include-frames=true", + "--instructions", "instructions", + "--max-age-ms", "0", + "--max-depth", "0", + "--max-pages", "1", + "--pdf.end", "1", + "--pdf.should-parse=true", + "--pdf.start", "1", + "--settle-animations=true", + "--stop-after-ms", "10000", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1000", + "--wait-for-ms", "0", + ) + }) + + t.Run("piping data", func(t *testing.T) { + // Test piping YAML data over stdin + pipeData := []byte("" + + "schema:\n" + + " type: bar\n" + + " properties: bar\n" + + " required: bar\n" + + " additionalProperties: bar\n" + + "url: https://example.com\n" + + "actions:\n" + + " - do: wait\n" + + " timeMs: 0\n" + + "factCheck: true\n" + + "followSubdomains: true\n" + + "includeFrames: true\n" + + "instructions: instructions\n" + + "maxAgeMs: 0\n" + + "maxDepth: 0\n" + + "maxPages: 1\n" + + "pdf:\n" + + " end: 1\n" + + " shouldParse: true\n" + + " start: 1\n" + + "settleAnimations: true\n" + + "stopAfterMs: 10000\n" + + "tags:\n" + + " - production\n" + + " - team-alpha\n" + + "timeoutMS: 1000\n" + + "waitForMs: 0\n") + mocktest.TestRunMockTestWithPipeAndFlags( + t, pipeData, + "--api-key", "string", + "web", "extract", + ) + }) +} + +func TestWebExtractCompetitors(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "web", "extract-competitors", + "--domain", "xxx", + "--num-competitors", "1", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1000", + ) + }) +} + +func TestWebExtractFonts(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "web", "extract-fonts", + "--direct-url", "https://example.com", + "--domain", "xxx", + "--max-age-ms", "0", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1000", + ) + }) +} + +func TestWebExtractStyleguide(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "web", "extract-styleguide", + "--color-scheme", "light", + "--direct-url", "https://example.com", + "--domain", "xxx", + "--max-age-ms", "0", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1000", + ) + }) +} + +func TestWebScreenshot(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "web", "screenshot", + "--clear-popups=true", + "--color-scheme", "light", + "--country", "de", + "--direct-url", "https://example.com", + "--domain", "xxx", + "--full-screenshot", "true", + "--handle-cookie-popup=true", + "--max-age-ms", "0", + "--page", "login", + "--scroll-offset", "0", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1", + "--viewport", "{height: 240, width: 240}", + "--wait-for-ms", "0", + "--zdr", "enabled", + ) + }) + + t.Run("inner flags", func(t *testing.T) { + // Check that inner flags have been set up correctly + requestflag.CheckInnerFlags(webScreenshot) + + // Alternative argument passing style using inner flags + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "web", "screenshot", + "--clear-popups=true", + "--color-scheme", "light", + "--country", "de", + "--direct-url", "https://example.com", + "--domain", "xxx", + "--full-screenshot", "true", + "--handle-cookie-popup=true", + "--max-age-ms", "0", + "--page", "login", + "--scroll-offset", "0", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1", + "--viewport.height", "240", + "--viewport.width", "240", + "--wait-for-ms", "0", + "--zdr", "enabled", + ) + }) +} + +func TestWebSearch(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "web", "search", + "--query", "x", + "--country", "af", + "--exclude-domain", "string", + "--freshness", "last_24_hours", + "--include-domain", "string", + "--markdown-options", "{enabled: true, includeFrames: true, includeImages: true, includeLinks: true, maxAgeMs: 0, pdf: {end: 1, shouldParse: true, start: 1}, shortenBase64Images: true, timeoutMS: 1000, useMainContentOnly: true, waitForMs: 0}", + "--num-results", "10", + "--query-fanout=true", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1000", + ) + }) + + t.Run("inner flags", func(t *testing.T) { + // Check that inner flags have been set up correctly + requestflag.CheckInnerFlags(webSearch) + + // Alternative argument passing style using inner flags + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "web", "search", + "--query", "x", + "--country", "af", + "--exclude-domain", "string", + "--freshness", "last_24_hours", + "--include-domain", "string", + "--markdown-options.enabled=true", + "--markdown-options.include-frames=true", + "--markdown-options.include-images=true", + "--markdown-options.include-links=true", + "--markdown-options.max-age-ms", "0", + "--markdown-options.pdf", "{end: 1, shouldParse: true, start: 1}", + "--markdown-options.shorten-base64-images=true", + "--markdown-options.timeout-ms", "1000", + "--markdown-options.use-main-content-only=true", + "--markdown-options.wait-for-ms", "0", + "--num-results", "10", + "--query-fanout=true", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1000", + ) + }) + + t.Run("piping data", func(t *testing.T) { + // Test piping YAML data over stdin + pipeData := []byte("" + + "query: x\n" + + "country: af\n" + + "excludeDomains:\n" + + " - string\n" + + "freshness: last_24_hours\n" + + "includeDomains:\n" + + " - string\n" + + "markdownOptions:\n" + + " enabled: true\n" + + " includeFrames: true\n" + + " includeImages: true\n" + + " includeLinks: true\n" + + " maxAgeMs: 0\n" + + " pdf:\n" + + " end: 1\n" + + " shouldParse: true\n" + + " start: 1\n" + + " shortenBase64Images: true\n" + + " timeoutMS: 1000\n" + + " useMainContentOnly: true\n" + + " waitForMs: 0\n" + + "numResults: 10\n" + + "queryFanout: true\n" + + "tags:\n" + + " - production\n" + + " - team-alpha\n" + + "timeoutMS: 1000\n") + mocktest.TestRunMockTestWithPipeAndFlags( + t, pipeData, + "--api-key", "string", + "web", "search", + ) + }) +} + +func TestWebWebCrawlMd(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "web", "web-crawl-md", + "--url", "https://example.com", + "--country", "de", + "--exclude-selector", "string", + "--follow-subdomains=true", + "--include-frames=true", + "--include-images=true", + "--include-links=true", + "--include-selector", "string", + "--max-age-ms", "0", + "--max-depth", "0", + "--max-pages", "1", + "--pdf", "{end: 1, ocr: true, shouldParse: true, start: 1}", + "--settle-animations=true", + "--shorten-base64-images=true", + "--stop-after-ms", "10000", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1000", + "--url-regex", "^https?://[^/]+/blog/", + "--use-main-content-only=true", + "--wait-for-ms", "0", + "--zdr", "enabled", + ) + }) + + t.Run("inner flags", func(t *testing.T) { + // Check that inner flags have been set up correctly + requestflag.CheckInnerFlags(webWebCrawlMd) + + // Alternative argument passing style using inner flags + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "web", "web-crawl-md", + "--url", "https://example.com", + "--country", "de", + "--exclude-selector", "string", + "--follow-subdomains=true", + "--include-frames=true", + "--include-images=true", + "--include-links=true", + "--include-selector", "string", + "--max-age-ms", "0", + "--max-depth", "0", + "--max-pages", "1", + "--pdf.end", "1", + "--pdf.ocr=true", + "--pdf.should-parse=true", + "--pdf.start", "1", + "--settle-animations=true", + "--shorten-base64-images=true", + "--stop-after-ms", "10000", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1000", + "--url-regex", "^https?://[^/]+/blog/", + "--use-main-content-only=true", + "--wait-for-ms", "0", + "--zdr", "enabled", + ) + }) + + t.Run("piping data", func(t *testing.T) { + // Test piping YAML data over stdin + pipeData := []byte("" + + "url: https://example.com\n" + + "country: de\n" + + "excludeSelectors:\n" + + " - string\n" + + "followSubdomains: true\n" + + "includeFrames: true\n" + + "includeImages: true\n" + + "includeLinks: true\n" + + "includeSelectors:\n" + + " - string\n" + + "maxAgeMs: 0\n" + + "maxDepth: 0\n" + + "maxPages: 1\n" + + "pdf:\n" + + " end: 1\n" + + " ocr: true\n" + + " shouldParse: true\n" + + " start: 1\n" + + "settleAnimations: true\n" + + "shortenBase64Images: true\n" + + "stopAfterMs: 10000\n" + + "tags:\n" + + " - production\n" + + " - team-alpha\n" + + "timeoutMS: 1000\n" + + "urlRegex: ^https?://[^/]+/blog/\n" + + "useMainContentOnly: true\n" + + "waitForMs: 0\n" + + "zdr: enabled\n") + mocktest.TestRunMockTestWithPipeAndFlags( + t, pipeData, + "--api-key", "string", + "web", "web-crawl-md", + ) + }) +} + +func TestWebWebScrapeHTML(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "web", "web-scrape-html", + "--url", "https://example.com", + "--action", "[{do: wait, timeMs: 0}]", + "--country", "de", + "--exclude-selector", "[x]", + "--headers", "{foo: J!}", + "--include-frames=true", + "--include-selector", "[x]", + "--max-age-ms", "0", + "--pdf", "{end: 1, ocr: true, shouldParse: true, start: 1}", + "--settle-animations=true", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1", + "--use-main-content-only=true", + "--wait-for-ms", "0", + "--zdr", "enabled", + ) + }) + + t.Run("inner flags", func(t *testing.T) { + // Check that inner flags have been set up correctly + requestflag.CheckInnerFlags(webWebScrapeHTML) + + // Alternative argument passing style using inner flags + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "web", "web-scrape-html", + "--url", "https://example.com", + "--action", "[{do: wait, timeMs: 0}]", + "--country", "de", + "--exclude-selector", "[x]", + "--headers", "{foo: J!}", + "--include-frames=true", + "--include-selector", "[x]", + "--max-age-ms", "0", + "--pdf.end", "1", + "--pdf.ocr=true", + "--pdf.should-parse=true", + "--pdf.start", "1", + "--settle-animations=true", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1", + "--use-main-content-only=true", + "--wait-for-ms", "0", + "--zdr", "enabled", + ) + }) +} + +func TestWebWebScrapeImages(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "web", "web-scrape-images", + "--url", "https://example.com", + "--action", "[{do: wait, timeMs: 0}]", + "--dedupe=true", + "--enrichment", "{classification: true, hostedUrl: true, maxTimePerMs: 1, resolution: true}", + "--headers", "{foo: J!}", + "--max-age-ms", "0", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1", + "--wait-for-ms", "0", + ) + }) + + t.Run("inner flags", func(t *testing.T) { + // Check that inner flags have been set up correctly + requestflag.CheckInnerFlags(webWebScrapeImages) + + // Alternative argument passing style using inner flags + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "web", "web-scrape-images", + "--url", "https://example.com", + "--action", "[{do: wait, timeMs: 0}]", + "--dedupe=true", + "--enrichment.classification=true", + "--enrichment.hosted-url=true", + "--enrichment.max-time-per-ms", "1", + "--enrichment.resolution=true", + "--headers", "{foo: J!}", + "--max-age-ms", "0", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1", + "--wait-for-ms", "0", + ) + }) +} + +func TestWebWebScrapeMd(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "web", "web-scrape-md", + "--url", "https://example.com", + "--action", "[{do: wait, timeMs: 0}]", + "--country", "de", + "--exclude-selector", "[x]", + "--headers", "{foo: J!}", + "--include-frames=true", + "--include-html=true", + "--include-images=true", + "--include-links=true", + "--include-selector", "[x]", + "--max-age-ms", "0", + "--pdf", "{end: 1, ocr: true, shouldParse: true, start: 1}", + "--settle-animations=true", + "--shorten-base64-images=true", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1", + "--use-main-content-only=true", + "--wait-for-ms", "0", + "--zdr", "enabled", + ) + }) + + t.Run("inner flags", func(t *testing.T) { + // Check that inner flags have been set up correctly + requestflag.CheckInnerFlags(webWebScrapeMd) + + // Alternative argument passing style using inner flags + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "web", "web-scrape-md", + "--url", "https://example.com", + "--action", "[{do: wait, timeMs: 0}]", + "--country", "de", + "--exclude-selector", "[x]", + "--headers", "{foo: J!}", + "--include-frames=true", + "--include-html=true", + "--include-images=true", + "--include-links=true", + "--include-selector", "[x]", + "--max-age-ms", "0", + "--pdf.end", "1", + "--pdf.ocr=true", + "--pdf.should-parse=true", + "--pdf.start", "1", + "--settle-animations=true", + "--shorten-base64-images=true", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1", + "--use-main-content-only=true", + "--wait-for-ms", "0", + "--zdr", "enabled", + ) + }) +} + +func TestWebWebScrapeSitemap(t *testing.T) { + t.Skip("Mock server tests are disabled") + t.Run("regular flags", func(t *testing.T) { + mocktest.TestRunMockTestWithFlags( + t, + "--api-key", "string", + "web", "web-scrape-sitemap", + "--domain", "xxx", + "--headers", "{foo: J!}", + "--max-links", "1", + "--search", "help center and troubleshooting articles", + "--sitemap-url", "https://example.com", + "--tag", "production", + "--tag", "team-alpha", + "--timeout-ms", "1", + "--url-regex", "^https?://[^/]+/blog/", + "--zdr", "enabled", + ) + }) +} diff --git a/release-please-config.json b/release-please-config.json new file mode 100644 index 0000000..53619de --- /dev/null +++ b/release-please-config.json @@ -0,0 +1,67 @@ +{ + "packages": { + ".": {} + }, + "$schema": "https://raw.githubusercontent.com/stainless-api/release-please/main/schemas/config.json", + "include-v-in-tag": true, + "include-component-in-tag": false, + "versioning": "prerelease", + "prerelease": true, + "bump-minor-pre-major": true, + "bump-patch-for-minor-pre-major": false, + "pull-request-header": "Automated Release PR", + "pull-request-title-pattern": "release: ${version}", + "changelog-sections": [ + { + "type": "feat", + "section": "Features" + }, + { + "type": "fix", + "section": "Bug Fixes" + }, + { + "type": "perf", + "section": "Performance Improvements" + }, + { + "type": "revert", + "section": "Reverts" + }, + { + "type": "chore", + "section": "Chores" + }, + { + "type": "docs", + "section": "Documentation" + }, + { + "type": "style", + "section": "Styles" + }, + { + "type": "refactor", + "section": "Refactors" + }, + { + "type": "test", + "section": "Tests", + "hidden": true + }, + { + "type": "build", + "section": "Build System" + }, + { + "type": "ci", + "section": "Continuous Integration", + "hidden": true + } + ], + "release-type": "simple", + "extra-files": [ + "pkg/cmd/version.go", + "README.md" + ] +} \ No newline at end of file diff --git a/scripts/bootstrap b/scripts/bootstrap new file mode 100755 index 0000000..bbc786d --- /dev/null +++ b/scripts/bootstrap @@ -0,0 +1,22 @@ +#!/usr/bin/env bash + +set -e + +cd "$(dirname "$0")/.." + +if [ -f "Brewfile" ] && [ "$(uname -s)" = "Darwin" ] && [ "${SKIP_BREW:-}" != "1" ] && [ -t 0 ]; then + brew bundle check >/dev/null 2>&1 || { + echo -n "==> Install Homebrew dependencies? (y/N): " + read -r response + case "$response" in + [yY][eE][sS]|[yY]) + brew bundle + ;; + *) + ;; + esac + echo + } +fi +echo "==> Installing Go dependencies…" +go mod tidy -e || true diff --git a/scripts/build b/scripts/build new file mode 100755 index 0000000..499fe73 --- /dev/null +++ b/scripts/build @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -euo pipefail + +cd "$(dirname "$0")/.." + +# Mark the necessary Go modules as private to avoid Go's proxy +export GOPRIVATE="${GOPRIVATE:+$GOPRIVATE,}github.com/context-dot-dev/context-go-sdk/v2,github.com/stainless-sdks/context.dev-go/v2" + +echo "==> Building context-dev" +go build ./cmd/context-dev diff --git a/scripts/format b/scripts/format new file mode 100755 index 0000000..db2a3fa --- /dev/null +++ b/scripts/format @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +set -e + +cd "$(dirname "$0")/.." + +echo "==> Running gofmt -s -w" +gofmt -s -w . diff --git a/scripts/link b/scripts/link new file mode 100755 index 0000000..abb68d9 --- /dev/null +++ b/scripts/link @@ -0,0 +1,17 @@ +#!/usr/bin/env bash + +set -euo pipefail + +cd "$(dirname "$0")/.." + +# Mark the necessary Go modules as private to avoid Go's proxy +export GOPRIVATE="${GOPRIVATE:+$GOPRIVATE,}github.com/context-dot-dev/context-go-sdk/v2,github.com/stainless-sdks/context.dev-go/v2" + +REPLACEMENT="${1:-"../context.dev-go"}" +echo "==> Replacing Go SDK with $REPLACEMENT" +if [[ -d "$REPLACEMENT" ]] || go list -m "$REPLACEMENT" >/dev/null; then + go mod edit -replace github.com/context-dot-dev/context-go-sdk/v2="$REPLACEMENT" + go mod tidy -e +else + echo "Skipping Go SDK replacement (branch may not exist on Go SDK)" +fi diff --git a/scripts/lint b/scripts/lint new file mode 100755 index 0000000..6abbdeb --- /dev/null +++ b/scripts/lint @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -euo pipefail + +cd "$(dirname "$0")/.." + +# Mark the necessary Go modules as private to avoid Go's proxy +export GOPRIVATE="${GOPRIVATE:+$GOPRIVATE,}github.com/context-dot-dev/context-go-sdk/v2,github.com/stainless-sdks/context.dev-go/v2" + +echo "==> Running Go build" +go build ./... diff --git a/scripts/run b/scripts/run new file mode 100755 index 0000000..4826ab6 --- /dev/null +++ b/scripts/run @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +set -euo pipefail + +cd "$(dirname "$0")/.." + +# Mark the necessary Go modules as private to avoid Go's proxy +export GOPRIVATE="${GOPRIVATE:+$GOPRIVATE,}github.com/context-dot-dev/context-go-sdk/v2,github.com/stainless-sdks/context.dev-go/v2" + +go run ./cmd/context-dev "$@" diff --git a/scripts/test b/scripts/test new file mode 100755 index 0000000..ba3a3c7 --- /dev/null +++ b/scripts/test @@ -0,0 +1,18 @@ +#!/usr/bin/env bash + +set -euo pipefail + +cd "$(dirname "$0")/.." + +# Mark the necessary Go modules as private to avoid Go's proxy +export GOPRIVATE="${GOPRIVATE:+$GOPRIVATE,}github.com/context-dot-dev/context-go-sdk/v2,github.com/stainless-sdks/context.dev-go/v2" + + + +echo "==> Running tests" +go test ./... "$@" + +echo "==> Checking tests on Windows" +GOARCH=amd64 GOOS=windows go test -c ./... "$@" +# `go test -c` produces a bunch of .exe files; make sure to clean those up +find . -name "*.test.exe" -exec rm {} \; diff --git a/scripts/unlink b/scripts/unlink new file mode 100755 index 0000000..4cda132 --- /dev/null +++ b/scripts/unlink @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +set -e + +cd "$(dirname "$0")/.." + +echo "==> Unlinking with local directory" +go mod edit -dropreplace github.com/context-dot-dev/context-go-sdk/v2 diff --git a/scripts/utils/upload-artifact.sh b/scripts/utils/upload-artifact.sh new file mode 100755 index 0000000..ae9f330 --- /dev/null +++ b/scripts/utils/upload-artifact.sh @@ -0,0 +1,59 @@ +#!/usr/bin/env bash +set -exuo pipefail + +BINARY_NAME="context-dev" +DIST_DIR="dist" +FILENAME="dist.zip" + +files=() +while IFS= read -r -d '' file; do + files+=("$file") +done < <(find "$DIST_DIR" -type f \( \ + -path "*amd64*/$BINARY_NAME" -o \ + -path "*arm64*/$BINARY_NAME" -o \ + -path "*amd64*/${BINARY_NAME}.exe" -o \ + -path "*arm64*/${BINARY_NAME}.exe" \ + \) -print0) + +if [[ ${#files[@]} -eq 0 ]]; then + echo -e "\033[31mNo binaries found for packaging.\033[0m" + exit 1 +fi + +rm -f "${DIST_DIR}/${FILENAME}" + +while IFS= read -r -d '' dir; do + printf "Remove the quarantine attribute before running the executable:\n\nxattr -d com.apple.quarantine %s\n" \ + "$BINARY_NAME" >"${dir}/README.txt" + files+=("${dir}/README.txt") +done < <(find "$DIST_DIR" -type d -path '*macos*' -print0) + +relative_files=() +for file in "${files[@]}"; do + relative_files+=("${file#"${DIST_DIR}"/}") +done + +(cd "$DIST_DIR" && zip -r "$FILENAME" "${relative_files[@]}") + +RESPONSE=$(curl -X POST "$URL?filename=$FILENAME" \ + -H "Authorization: Bearer $AUTH" \ + -H "Content-Type: application/json") + +SIGNED_URL=$(echo "$RESPONSE" | jq -r '.url') + +if [[ "$SIGNED_URL" == "null" ]]; then + echo -e "\033[31mFailed to get signed URL.\033[0m" + exit 1 +fi + +UPLOAD_RESPONSE=$(curl -v -X PUT \ + -H "Content-Type: application/zip" \ + --data-binary "@${DIST_DIR}/${FILENAME}" "$SIGNED_URL" 2>&1) + +if echo "$UPLOAD_RESPONSE" | grep -q "HTTP/[0-9.]* 200"; then + echo -e "\033[32mUploaded build to Stainless storage.\033[0m" + echo -e "\033[32mInstallation: Download and unzip: 'https://pkg.stainless.com/s/context.dev-cli/$SHA'. On macOS, run 'xattr -d com.apple.quarantine {executable name}'.\033[0m" +else + echo -e "\033[31mFailed to upload artifact.\033[0m" + exit 1 +fi