Skip to content

Latest commit

 

History

History
71 lines (53 loc) · 2.56 KB

README.md

File metadata and controls

71 lines (53 loc) · 2.56 KB

CLI autogenerated from Swagger OpenAPI specification

Have you ever wondered if there was a better way to directly use an API other than CURLing it? I have.

The result is autocli, a package which automatically generates subcommands and arguments based on the specification. Bash completion included.

Installation

$ git pull ...
$ cd autocli
$ python3 -m venv .venv && source .venv/bin/activate
$ python3 -m pip install -e .

Usage

$ autocli example-cli https://example.org/api/swagger-1.2.7.json https://example.org/api
...
You have pipx available. The easiest way to install the package is:
> pipx install /home/mhorky/src/autocli/build/example-cli
> _example-cli() {
>     local cur=${COMP_WORDS[COMP_CWORD]}
>     local OPTIONS=`$HOME/.local/pipx/venvs/example-cli/bin/python3 -c 'import example_cli; example_cli.complete()' "${COMP_WORDS[@]}"`
>     COMPREPLY=( $( compgen -W "${OPTIONS}" -- "$cur" ) )
>     return 0
> }
> complete -F _example-cli example-cli
$ example-cli <tab><tab>

How it works

  • The autocli command downloads the OpenAPI specification and saves it into the __about__.py.
  • All files are packaged into a build/ directory, allowing us to install it as any other Python package.
  • pipx (or pure pip in .venv) installs it into the system, making it available.
  • On bash completion event (typing example-cli <tab><tab>), it scans the paths to the API endpoints and suggests them using regular bash completion.
  • Once the path is entered, typing - starts suggesting headers and query parameters the endpoint requires and/or supports.

Example

Following is an example for the Candlepin Project.

$ candlepin-cli <tab><tab>
activation_keys       crl                   jobs                  serials
admin                 deleted_consumers     owners                status
cdn                   distributor_versions  pools                 subscriptions
consumers             entitlements          products              users
consumertypes         environments          roles                 
content               hypervisors           rules                 
$ candlepin-cli act<tab><tab>
$ candlepin-cli activation_keys activation_key_id=0123456789 <tab><tab>
content_overrides  pools  product
$ candlepin-cli activation_keys activation_key_id=0123456789 cont<tab><tab>
$ candlepin-cli activation_keys activation_key_id=0123456789 content_overrides -X
delete  get  put
$ candlepin-cli activation_keys activation_key_id=0123456789 content_overrides -X get

License

MIT