Skip to content
Browse files

Adds a simple bash completion script (#277)

* Adds a simple bash completion script

This works with any loading script named openapi-generator-cli.
That is, if you've installed via homebrew or created a script similar

This script is relatively simple, relying on fallback to the recently
add "completion" command to the CLI project.

The script includes a possible extension to allow for per-language
options to autocomplete when the user is applying additional properties.
This work is currently commented out, as it may be simplified a bit in
the CLI first.

* Add launcher script and "install" instructions
  • Loading branch information...
jimschubert authored and wing328 committed Jul 25, 2018
1 parent 85f0909 commit c6004a8f89e25c351532b4073c614db60bbd8bad
Showing with 149 additions and 0 deletions.
  1. +43 −0
  2. +60 −0 bin/utils/
  3. +46 −0 scripts/openapi-generator-cli-completion.bash
@@ -167,6 +167,49 @@ export JAVA_HOME=`/usr/libexec/java_home -v 1.8`
export PATH=${JAVA_HOME}/bin:$PATH

### Launcher Script

One downside to manual jar downloads is that you don't keep up-to-date with the latest released version. We have a Bash launcher script at [bin/utils/](./bin/utils/ which resolves this issue.

To install the launcher script, copy the contents of the script to a location on your path and make the script executable.

An example of setting this up (NOTE: Always evaluate scripts curled from external systems before executing them).

mkdir -p ~/bin/openapitools
curl > ~/bin/openapitools/openapi-generator-cli
chmod u+x ~/bin/openapitools/openapi-generator-cli
export PATH=$PATH:~/bin/openapitools/

Now, `openapi-generator-cli` is "installed". On invocation, it will query the GitHub repository for the most recently released version. If this matches the last downloaded jar,
it will execute as normal. If a newer version is found, the script will download the latest release and execute it.

If you need to invoke an older version of the generator, you can define the variable `OPENAPI_GENERATOR_VERSION` either ad hoc or globally. You can export this variable if you'd like to persist a specific release version.


# Execute latest released openapi-generator-cli
openapi-generator-cli version
# Execute version 3.1.0 for the current invocation, regardless of the latest released version
OPENAPI_GENERATOR_VERSION=3.1.0 openapi-generator-cli version
# Execute version 3.1.0-SNAPSHOT for the current invocation
OPENAPI_GENERATOR_VERSION=3.1.0-SNAPSHOT openapi-generator-cli version
# Execute version 3.0.2 for every invocation in the current shell session
openapi-generator-cli version # is 3.0.2
openapi-generator-cli version # is also 3.0.2
# To "install" a specific version, set the variable in .bashrc/.bash_profile
echo "export OPENAPI_GENERATOR_VERSION=3.0.2" >> ~/.bashrc
source ~/.bashrc
openapi-generator-cli version # is always 3.0.2, unless any of the above overrides are done ad hoc

### [1.4 - Build Projects](#table-of-contents)

To build from source, you need the following installed and available in your `$PATH:`
@@ -0,0 +1,60 @@
#!/usr/bin/env bash
# Save as openapi-generator-cli on your PATH. chmod u+x. Enjoy.
# This script will query github on every invocation to pull the latest released version
# of openapi-generator.
# If you want repeatable executions, you can explicitly set a version via
# e.g. (in Bash)
# or
# This is also helpful, for example, if you want want to evaluate a SNAPSHOT version.
# NOTE: Jars are downloaded on demand from maven into the same directory as this script
# for every 'latest' version pulled from github. Consider putting this under its own directory.
set -o pipefail

for cmd in {mvn,python,curl}; do
if ! command -v ${cmd} > /dev/null; then
>&2 echo "This script requires '${cmd}' to be installed."
exit 1

function latest.tag {
local uri="${1}/tags"
curl -s ${uri} | python -c "import sys, json; print json.load(sys.stdin)[0]['name'][1:]"

ver=${OPENAPI_GENERATOR_VERSION:-$(latest.tag $ghrepo)}

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

if [ ! -f ${DIR}/${jar} ]; then
if [[ ${ver} =~ ^.*-SNAPSHOT$ ]]; then
mvn org.apache.maven.plugins:maven-dependency-plugin:2.9:get \
-DremoteRepositories=${repo} \
-Dartifact=${groupid}:${artifactid}:${ver} \
-Dtransitive=false \

java -ea \
-Xms512M \
-Xmx1024M \
-server \
-jar ${DIR}/${jar} "$@"
@@ -0,0 +1,46 @@
#!/usr/bin/env bash

# Provides completion assistance for openapi-generator-cli
# Install
# Mac:
# brew install bash-completion
# cp openapi-generator-cli-completion.bash `brew --prefix`/etc/bash_completion.d
# Linux: many distributions include this automatically. Search for your distro-specific instructions.
# When in doubt, try sourcing this file:
# type complete && source openapi-generator-cli
# see

_openapi_generator_cli_completions() {
local IFS=$' \t\n'
local options=()

options+=("$($1 completion ${COMP_WORDS[@]:1})")

case "${COMP_WORDS[1]}" in
case "${COMP_WORDS[@]:2}" in
# TODO: This is a nice-to-have and not required.
# Apply generator-specific options to additional properties. These can be queried via:
# openapi-generator-cli config-help -l YOUR_LANG | grep '^\t' | grep -v '^\t\s\s\s\s' | tr -d '\t'
# where YOUR_LANG would need to be evaluated as the value after the current switch.
# but rather than switching on 'generate' maybe switch on --additional-properties?
# ignore

# printf '%s\n' "${options[@]}"
if [[ -n "${options[@]}" ]]; then
COMPREPLY=( $(compgen -W "${options}" -- ${2}) )

complete -F _openapi_generator_cli_completions openapi-generator-cli

0 comments on commit c6004a8

Please sign in to comment.
You can’t perform that action at this time.