|  | 
| 1 | 1 | #!/usr/bin/env bash | 
| 2 |  | - | 
|  | 2 | +source bin/util | 
| 3 | 3 | set -euo pipefail | 
| 4 | 4 | 
 | 
| 5 | 5 | print_help(){ | 
| 6 |  | -    echo -e "./bin/generate_client [-o <output-directory>] [-l <client-language> ] [-n] [--enterprise]" | 
| 7 |  | -    echo -e "\tUses the OpenApi generator to create a client for the API spec" | 
| 8 |  | -    echo -e "\tDefault output goes in the 'out' directory under the generated client language" | 
| 9 |  | -    echo -e "\tIf no language is specified the Python client will be generated" | 
| 10 |  | -    echo | 
| 11 |  | -    echo -e "\tThe -n option will prevent the creation of a client language directory" | 
| 12 |  | -    echo -e "\t\tso the output would go to 'out/' instead of 'out/python' for the python client" | 
| 13 |  | -    echo | 
| 14 |  | -    echo -e "\tThe --enterprise option will generate the spec with included Conjur Enterprise-only endpoints" | 
|  | 6 | +  announce "CONJUR OPENAPI DESCRIPTION :: CLIENT GENERATION" | 
|  | 7 | +  cat << EOF | 
|  | 8 | +Uses the OpenAPI Generator to create a client for the API spec. | 
|  | 9 | +Can generate clients for Conjur OSS (default) or Enterprise. | 
|  | 10 | +Clients are output to ./out/<oss|enterprise>/<language> by default. | 
|  | 11 | +
 | 
|  | 12 | +USAGE | 
|  | 13 | +./bin/generate_client -l <language> [options] | 
|  | 14 | +
 | 
|  | 15 | +MANDATORY | 
|  | 16 | +-l|--language <language>    Specify a client language | 
|  | 17 | +
 | 
|  | 18 | +OPTIONS | 
|  | 19 | +-e|--enterprise             Generate client for Conjur Enterprise | 
|  | 20 | +-h|--help                   Print help message | 
|  | 21 | +-n|--no-sub-dir             Generated clients output directly to ./out | 
|  | 22 | +-o|--output <dir>           Specify an output directory | 
|  | 23 | +EOF | 
| 15 | 24 | } | 
| 16 | 25 | 
 | 
| 17 | 26 | generator_version="v4.3.1" | 
| 18 | 27 | GENERATOR_IMAGE="openapitools/openapi-generator-cli:$generator_version" | 
| 19 | 28 | 
 | 
| 20 |  | -client_lang="python" | 
|  | 29 | +client_lang="" | 
|  | 30 | +appliance="oss" | 
|  | 31 | +output_volume="" | 
|  | 32 | +enterprise=0 | 
|  | 33 | +given_out_dir=0 | 
| 21 | 34 | make_client_dir=1 | 
| 22 |  | -output_volume="out/" | 
| 23 |  | -output_dir='/out/oss' | 
| 24 |  | -input_dir='out/oss/spec' | 
| 25 |  | -dap_spec=0 | 
| 26 |  | - | 
| 27 |  | -mkdir -p out/enterprise | 
| 28 |  | -mkdir -p out/oss | 
| 29 | 35 | 
 | 
| 30 | 36 | while test $# -gt 0 | 
| 31 | 37 | do | 
| 32 |  | -    param=$1 | 
| 33 |  | -    shift | 
| 34 |  | -    case "$param" in | 
| 35 |  | -        -o) | 
| 36 |  | -            output_volume=$1 | 
| 37 |  | -            shift | 
| 38 |  | -            ;; | 
| 39 |  | -        -l) | 
| 40 |  | -            client_lang=$1 | 
| 41 |  | -            shift | 
| 42 |  | -            ;; | 
| 43 |  | -        -n) | 
| 44 |  | -            make_client_dir=0 | 
| 45 |  | -            ;; | 
| 46 |  | -        -h) | 
| 47 |  | -            print_help | 
| 48 |  | -            exit 0 | 
| 49 |  | -            ;; | 
| 50 |  | -        --enterprise) | 
| 51 |  | -            dap_spec=1 | 
| 52 |  | -            input_dir='out/enterprise/spec' | 
| 53 |  | -            output_dir='/out/enterprise' | 
| 54 |  | -            ;; | 
| 55 |  | -        *) | 
| 56 |  | -            break | 
| 57 |  | -            ;; | 
| 58 |  | -    esac | 
|  | 38 | +  param=$1 | 
|  | 39 | +  shift | 
|  | 40 | +  case "$param" in | 
|  | 41 | +    -e|--enterprise) | 
|  | 42 | +      enterprise=1 | 
|  | 43 | +      appliance="enterprise" | 
|  | 44 | +      input_dir='out/enterprise/spec' | 
|  | 45 | +      output_dir='/out/enterprise' | 
|  | 46 | +      ;; | 
|  | 47 | +    -h|--help) | 
|  | 48 | +      print_help | 
|  | 49 | +      exit 0 | 
|  | 50 | +      ;; | 
|  | 51 | +    -l|--language) | 
|  | 52 | +      client_lang=$1 | 
|  | 53 | + | 
|  | 54 | +      if [[ ${client_lang:0:1} == "-" ]]; then | 
|  | 55 | +        echo "Option --language requires specifying argument" | 
|  | 56 | +        echo "Usage: ./bin/generate_client -l <language> [options]" | 
|  | 57 | +        exit 1 | 
|  | 58 | +      fi | 
|  | 59 | + | 
|  | 60 | +      shift | 
|  | 61 | +      ;; | 
|  | 62 | +    -n|--no-sub-dir) | 
|  | 63 | +      make_client_dir=0 | 
|  | 64 | +      ;; | 
|  | 65 | +    -o|--output) | 
|  | 66 | +      output_volume=$1 | 
|  | 67 | + | 
|  | 68 | +      if [[ ${output_volume:0:1} == "-" ]]; then | 
|  | 69 | +        echo "Option --output requires specifying argument" | 
|  | 70 | +        echo "Usage: ./bin/generate_client -l <language> -o <dir>" | 
|  | 71 | +        exit 1 | 
|  | 72 | +      fi | 
|  | 73 | + | 
|  | 74 | +      given_out_dir=1 | 
|  | 75 | +      if [ ${output_volume:0:2} == "./" ]; then | 
|  | 76 | +        ouput_volume=${PWD}/${output_volume#*./} | 
|  | 77 | +      elif [ ${output_volume:0:1} != "/" ]; then | 
|  | 78 | +        output_volume=${PWD}/${output_volume} | 
|  | 79 | +      fi | 
|  | 80 | +      shift | 
|  | 81 | +      ;; | 
|  | 82 | +    *) | 
|  | 83 | +      break | 
|  | 84 | +      ;; | 
|  | 85 | +  esac | 
| 59 | 86 | done | 
| 60 | 87 | 
 | 
| 61 |  | -if [ ! -e "$output_volume" ]; then | 
| 62 |  | -    mkdir -p "$output_volume" | 
|  | 88 | +if [[ -z $client_lang ]]; then | 
|  | 89 | +  echo "Missing required --language option" | 
|  | 90 | +  echo "Usage: ./bin/generate_client -l <language> [options]" | 
|  | 91 | +  exit 1 | 
| 63 | 92 | fi | 
| 64 |  | -output_volume="$(pushd $output_volume > /dev/null && echo ${PWD} && popd > /dev/null)" | 
| 65 | 93 | 
 | 
| 66 |  | -if [ $make_client_dir -eq 1 ]; then | 
| 67 |  | -    output_dir="$output_dir/$client_lang" | 
|  | 94 | +input_dir="out/$appliance/spec" | 
|  | 95 | +if [ $given_out_dir -eq 0 ]; then | 
|  | 96 | +  output_volume="${PWD}/out/$appliance/$client_lang/" | 
| 68 | 97 | fi | 
|  | 98 | +if [ $make_client_dir -eq 0 ]; then | 
|  | 99 | +  output_volume="${PWD}/out/" | 
|  | 100 | +fi | 
|  | 101 | + | 
|  | 102 | +echo "Removing old client dir..." | 
|  | 103 | +rm -rf $output_volume | 
|  | 104 | +mkdir -p $output_volume | 
| 69 | 105 | 
 | 
| 70 | 106 | # if templates for a language exist we use them for generation | 
| 71 | 107 | template_dir="templates/$client_lang" | 
| 72 | 108 | if [ -e $template_dir ]; then | 
| 73 |  | -    template_arg="-t /local/$template_dir" | 
|  | 109 | +  template_arg="-t /local/$template_dir" | 
| 74 | 110 | else | 
| 75 |  | -    template_arg="" | 
|  | 111 | +  template_arg="" | 
| 76 | 112 | fi | 
| 77 | 113 | 
 | 
| 78 |  | -echo "Pulling latest release $GENERATOR_IMAGE..." | 
| 79 |  | -docker pull "$GENERATOR_IMAGE" | 
| 80 |  | - | 
| 81 |  | -echo "Removing old client dir..." | 
| 82 |  | -rm -rf "$(pwd)/out/$client_lang" | 
| 83 |  | - | 
| 84 | 114 | # Check if there is a configuration file available for this language | 
| 85 | 115 | if [ -e "./spec/config/$client_lang.yml" ]; then | 
| 86 |  | -    client_config="-c /local/spec/config/$client_lang.yml" | 
|  | 116 | +  client_config="-c /local/spec/config/$client_lang.yml" | 
| 87 | 117 | else | 
| 88 |  | -    client_config="" | 
|  | 118 | +  client_config="" | 
| 89 | 119 | fi | 
| 90 | 120 | 
 | 
| 91 |  | -if [ $dap_spec -eq 1 ]; then | 
| 92 |  | -    ./bin/transform --enterprise | 
| 93 |  | -else | 
| 94 |  | -    ./bin/transform --oss | 
| 95 |  | -fi | 
|  | 121 | +bin/transform --$appliance | 
|  | 122 | + | 
|  | 123 | +echo "Pulling latest release $GENERATOR_IMAGE..." | 
|  | 124 | +docker pull "$GENERATOR_IMAGE" | 
| 96 | 125 | 
 | 
| 97 | 126 | echo "Generating $client_lang client..." | 
| 98 |  | -docker run --rm -v ${PWD}:/local -v $output_volume:/out "$GENERATOR_IMAGE" generate \ | 
|  | 127 | +docker run --rm \ | 
|  | 128 | +  -v ${PWD}:/local \ | 
|  | 129 | +  -v $output_volume:/out \ | 
|  | 130 | +  "$GENERATOR_IMAGE" generate \ | 
| 99 | 131 |     -i "/local/$input_dir/openapi.yml" \ | 
| 100 | 132 |     -g "$client_lang" \ | 
| 101 |  | -    -o "$output_dir" \ | 
|  | 133 | +    -o "/out/" \ | 
| 102 | 134 |     $client_config \ | 
| 103 | 135 |     $template_arg | 
| 104 | 136 | 
 | 
| 105 |  | -echo "Done! Client is in $output_dir folder!" | 
|  | 137 | +echo "Done! Client is in $output_volume folder!" | 
0 commit comments