Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public CodegenType getTag() {
}

/**
* Configures a friendly name for the generator. This will be used by
* Configures a friendly name for the generator. This will be used by
* the generator to select the library with the -l flag.
*
* @return the friendly name for the generator
Expand All @@ -74,9 +74,9 @@ public String getName() {
}

/**
* Returns human-friendly help for the generator. Provide the consumer with
* Returns human-friendly help for the generator. Provide the consumer with
* help tips, parameters here
*
*
* @return A string value for the help message
*/
public String getHelp() {
Expand Down Expand Up @@ -128,7 +128,7 @@ public BashClientCodegen() {
"(e.g. PETSTORE_HOST='http://petstore.swagger.io:8080')"));
cliOptions.add(CliOption.newString(BASIC_AUTH_ENVIRONMENT_VARIABLE_NAME,
"Name of environment variable where username and password "
+
+
"can be defined (e.g. PETSTORE_CREDS='username:password')"));
cliOptions.add(CliOption.newBoolean(APIKEY_AUTH_ENVIRONMENT_VARIABLE_NAME,
"Name of environment variable where API key "
Expand Down Expand Up @@ -206,7 +206,7 @@ public void processOpts() {
Boolean.parseBoolean(
additionalProperties.get(PROCESS_MARKDOWN).toString()));
}

if (additionalProperties.containsKey(GENERATE_BASH_COMPLETION)) {
setGenerateBashCompletion(
Boolean.parseBoolean(
Expand All @@ -225,7 +225,7 @@ public void processOpts() {
additionalProperties.put("x-codegen-script-name", scriptName);

if (additionalProperties.containsKey(HOST_ENVIRONMENT_VARIABLE_NAME)) {
setHostEnvironmentVariable(
setHostEnvironmentVariable(
additionalProperties.get(HOST_ENVIRONMENT_VARIABLE_NAME).toString());
additionalProperties.put("x-codegen-host-env", hostEnvironmentVariable);
}
Expand All @@ -237,11 +237,11 @@ public void processOpts() {
}

if (additionalProperties.containsKey(APIKEY_AUTH_ENVIRONMENT_VARIABLE_NAME)) {
setApiKeyAuthEnvironmentVariable(
setApiKeyAuthEnvironmentVariable(
additionalProperties.get(APIKEY_AUTH_ENVIRONMENT_VARIABLE_NAME).toString());
additionalProperties.put("x-codegen-apikey-env", apiKeyAuthEnvironmentVariable);
}

supportingFiles.add(new SupportingFile(
"client.mustache", "", scriptName));
supportingFiles.add(new SupportingFile(
Expand All @@ -250,6 +250,8 @@ public void processOpts() {
"zsh-completion.mustache", "", "_"+scriptName));
supportingFiles.add(new SupportingFile(
"README.mustache", "", "README.md"));
supportingFiles.add(new SupportingFile(
"Dockerfile.mustache", "", "Dockerfile"));
}

public void setCurlOptions(String curlOptions) {
Expand All @@ -263,25 +265,25 @@ public void setProcessMarkdown(boolean processMarkdown) {
public void setScriptName(String scriptName) {
this.scriptName = scriptName;
}

public void setGenerateBashCompletion(boolean generateBashCompletion) {
this.generateBashCompletion = generateBashCompletion;
}

public void setGenerateZshCompletion(boolean generateZshCompletion) {
this.generateZshCompletion = generateZshCompletion;
}

public void setHostEnvironmentVariable(String hostEnvironmentVariable) {
this.hostEnvironmentVariable = hostEnvironmentVariable;
}
public void setBasicAuthEnvironmentVariable(String

public void setBasicAuthEnvironmentVariable(String
basicAuthEnvironmentVariable) {
this.basicAuthEnvironmentVariable = basicAuthEnvironmentVariable;
}

public void setApiKeyAuthEnvironmentVariable(String
public void setApiKeyAuthEnvironmentVariable(String
apiKeyAuthEnvironmentVariable) {
this.apiKeyAuthEnvironmentVariable = apiKeyAuthEnvironmentVariable;
}
Expand All @@ -300,15 +302,15 @@ public String escapeReservedWord(String name) {
}

/**
* Location to write model files. You can use the modelPackage() as defined
* Location to write model files. You can use the modelPackage() as defined
* when the class is instantiated.
*/
public String modelFileFolder() {
return outputFolder;
}

/**
* Location to write api files. You can use the apiPackage() as defined when
* Location to write api files. You can use the apiPackage() as defined when
* the class is instantiated.
*/
@Override
Expand Down Expand Up @@ -402,10 +404,10 @@ else if(param instanceof SerializableParameter) {
p.vendorExtensions.put("x-codegen-collection-min-items",
sparam.getMinItems());
}
if( (collectionFormat.equals("multi"))

if( (collectionFormat.equals("multi"))
&& (param.getIn().equals("query")) ) {

/**
* 'multi' is only supported for query parameters
*/
Expand Down Expand Up @@ -445,15 +447,20 @@ public String escapeText(String input) {
return input;
}

/**
* Trim the input text always.
*/
String result = input.trim();

/**
* remove standalone '\'
*
* replace " with \"
* outter unescape to retain the original multi-byte characters
*/
String result = escapeUnsafeCharacters(
result = escapeUnsafeCharacters(
StringEscapeUtils.unescapeJava(
StringEscapeUtils.escapeJava(input).replace("\\/", "/"))
StringEscapeUtils.escapeJava(result).replace("\\/", "/"))
.replace("\\", "\\\\")
.replace("\"", "\\\""));

Expand Down Expand Up @@ -481,28 +488,28 @@ public String escapeText(String input) {

/**
* Convert all markdown section 1 level headers with bold
*/
*/
result = result.replaceAll("(?m)^\\#\\s+(.+)$",
"\n\\$\\(tput bold\\)\\$\\(tput setaf 7\\)"
+"$1\\$\\(tput sgr0\\)");

/**
* Convert all markdown section 2 level headers with bold
*/
*/
result = result.replaceAll("(?m)^\\#\\#\\s+(.+)$",
"\n\\$\\(tput bold\\)\\$\\(tput setaf 7\\)"
+"$1\\$\\(tput sgr0\\)");

/**
* Convert all markdown section 3 level headers with bold
*/
*/
result = result.replaceAll("(?m)^\\#\\#\\#\\s+(.+)$",
"\n\\$\\(tput bold\\)\\$\\(tput setaf 7\\)"
+"$1\\$\\(tput sgr0\\)");

/**
* Convert all markdown code blocks into --- delimited sections
*/
*/
result = result.replaceAll("(?m)\\s*```.*$",
"\n---");

Expand Down Expand Up @@ -555,7 +562,7 @@ public CodegenOperation fromOperation(String path, String httpMethod,
* for help
*/
if(op.vendorExtensions.containsKey("x-bash-codegen-description")) {
String bash_description
String bash_description
= (String)op.vendorExtensions.get("x-bash-codegen-description");

op.vendorExtensions.put("x-bash-codegen-description",
Expand Down Expand Up @@ -633,15 +640,15 @@ public void preprocessSwagger(Swagger swagger) {
swagger.setBasePath("");
}

if(swagger.getInfo() != null
if(swagger.getInfo() != null
&& swagger.getInfo().getVendorExtensions()!=null) {
String bash_codegen_app_description
= (String)swagger.getInfo().getVendorExtensions()
.get("x-bash-codegen-description");

if(bash_codegen_app_description != null) {

bash_codegen_app_description
bash_codegen_app_description
= escapeText(bash_codegen_app_description);

additionalProperties.put("x-bash-codegen-app-description",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
FROM ubuntu:16.10

RUN apt-get update -y && apt-get full-upgrade -y
RUN apt-get install -y bash-completion zsh curl cowsay git vim bsdmainutils

ADD {{scriptName}} /usr/bin/{{scriptName}}
ADD _{{scriptName}} /usr/local/share/zsh/site-functions/_{{scriptName}}
ADD {{scriptName}}.bash-completion /etc/bash-completion.d/{{scriptName}}


#
# Install oh-my-zsh
#
RUN sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)" || true

#
# Enable bash completion
#
RUN echo '\n\
. /etc/bash_completion\n\
source /etc/bash-completion.d/{{scriptName}}\n\
' >> ~/.bashrc

#
# Setup prompt
#
RUN echo 'export PS1="[{{appName}}] \$ "' >> ~/.bashrc
RUN echo 'export PROMPT="[{{appName}}] \$ "' >> ~/.zshrc

#
# Setup a welcome message with basic instruction
#
RUN echo 'cat << EOF\n\
\n\
This Docker provides preconfigured environment for running the command\n\
line REST client for $(tput setaf 6){{appName}}$(tput sgr0).\n\
\n\
For convenience, you can export the following environment variables:\n\
\n\
{{#x-codegen-host-env}}$(tput setaf 3){{x-codegen-host-env}}$(tput sgr0) - server URL, e.g. https://example.com:8080\n\{{/x-codegen-host-env}}
{{#hasAuthMethods}}
{{#authMethods}}
{{#isBasic}}
{{#x-codegen-basicauth-env}}$(tput setaf 3){{x-codegen-basicauth-env}}$(tput sgr0) - basic authentication credentials, e.g.: "username:password"\n\{{/x-codegen-basicauth-env}}
{{/isBasic}}
{{#isApiKey}}
{{#x-codegen-apikey-env}}$(tput setaf 3){{x-codegen-apikey-env}}$(tput sgr0) - access token, e.g. "ASDASHJDG63456asdASSD"\n\{{/x-codegen-apikey-env}}
{{/isApiKey}}
{{/authMethods}}
{{/hasAuthMethods}}
\n\
$(tput setaf 7)Basic usage:$(tput sgr0)\n\
\n\
$(tput setaf 3)Print the list of operations available on the service$(tput sgr0)\n\
$ {{scriptName}} -h\n\
\n\
$(tput setaf 3)Print the service description$(tput sgr0)\n\
$ {{scriptName}} --about\n\
\n\
$(tput setaf 3)Print detailed information about specific operation$(tput sgr0)\n\
$ {{scriptName}} <operationId> -h\n\
\n\
By default you are logged into Zsh with full autocompletion for your REST API,\n\
but you can switch to Bash, where basic autocompletion is also supported.\n\
\n\
EOF\n\
' | tee -a ~/.bashrc ~/.zshrc

ENTRYPOINT ["zsh"]
4 changes: 2 additions & 2 deletions modules/swagger-codegen/src/main/resources/bash/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Bash script generator for Swagger Codegen

## Overview
This is a Bash client script codegen.
This is a Bash client script codegen.

The codegen creates a standalone, single-file Bash script client to quickly test and access Swagger annotated REST services. The generated script uses underneath [cURL](https://curl.haxx.se) to make actual REST calls.

Expand Down Expand Up @@ -204,7 +204,7 @@ and add the following to the `~/.bashrc`:
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
```
```

### Zsh
In Zsh, the generated `_{{scriptName}}` file (e.g. _petstore-cli) must be copied to one of the folders under `$fpath` variable.
Expand Down
19 changes: 16 additions & 3 deletions modules/swagger-codegen/src/main/resources/bash/README.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ $ ./{{scriptName}} --about
# Print detailed information about specific operation
$ ./{{scriptName}} <operationId> -h

# Make GET request
# Make GET request
./{{scriptName}} --host http://<hostname>:<port> --accept xml <operationId> <queryParam1>=<value1> <header_key1>:<header_value2>

# Make GET request using arbitrary curl options (must be passed before <operationId>) to an SSL service using username:password
Expand All @@ -42,6 +42,20 @@ $ {{scriptName}} --host http://<hostname>:<port> --dry-run <operationid>

```

## Docker image
You can easily create a Docker image containing a preconfigured environment
for using the REST Bash client including working autocompletion and short
welcome message with basic instructions, using the generated Dockerfile:

```shell
docker build -t my-rest-client .
docker run -it my-rest-client
```

By default you will be logged into a Zsh environment which has much more
advanced auto completion, but you can switch to Bash, where basic autocompletion
is also available.

## Shell completion

### Bash
Expand All @@ -68,8 +82,7 @@ and add the following to the `~/.bashrc`:
if [ -f $(brew --prefix)/etc/bash_completion ]; then
. $(brew --prefix)/etc/bash_completion
fi
```
```

### Zsh
In Zsh, the generated `_{{scriptName}}` Zsh completion file must be copied to one of the folders under `$FPATH` variable.

Loading