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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# Build Artifacts
/target
examples/**/target/

# Compiled class file
*.class
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ The sorting looks like this:

A map becomes a Property / Value table. Nested maps become nested tables, and a list of plain values is joined into one cell. The `BuilderConfig` provides configurability for how `null`, `True`, and `False` values show up.

The [pets-cli example](examples/pets-cli) code has a more complete example.

A list of strings, numbers, or booleans becomes one Items column with a caption such as `Found 6 items`. A list of maps uses the first matching key field (`name`, then `id`) as the row name. One other field becomes the second column. Further fields are nested under Properties. Long keys, values, and URLs are cut off with `...`.

```Java
Expand Down Expand Up @@ -392,6 +394,10 @@ import rickwporter.prettytable.BuilderConfig;
| `setUrlPrefixes` | `http://`, `https://`, `ftp://` |
| `setColumns` | `null` |

## Example

[`examples/pets-cli`](examples/pets-cli) is a [picocli](https://picocli.info/) client for the modified [OpenAPI Petstore](examples/pets.yaml). It prints JSON responses with `TableBuilder`. See the example README for how to build and run it.

## Development

Your input is welcomed.
Expand Down
55 changes: 55 additions & 0 deletions examples/pets-cli/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
.PHONY = build
.PHONY += clean
.PHONY += example
.PHONY += depend-check
.PHONY += depend-tree
.PHONY += lint
.PHONY += test

MVN_ARGS ?= -ntp
MVN_CMD ?= mvn

#################################################
# Targets
#################################################
default: help

all: lint depend-check test build ## Perform lint, dependency checking, coverage, and build

help: ## This message
@echo "===================="
@echo " Available Commands"
@echo "===================="
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[$$()% a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

###########
##@ Build
build: ## Create the JAR file
$(MVN_CMD) $(MVN_ARGS) -Dmaven.test.skip=true package

clean: ## Cleans up build artifacts
$(MVN_CMD) $(MVN_ARGS) clean

###########
##@ Dependencies
depend-check: ## Checks to make sure all dependencies are used (or declared unused)
$(MVN_CMD) $(MVN_ARGS) dependency:analyze-only

depend-tree: ## Generates dependency reports
$(MVN_CMD) $(MVN_ARGS) dependency:tree

###########
##@ Formatting
lint: ## Check formatting
$(MVN_CMD) $(MVN_ARGS) checkstyle:check

###########
##@ Test
test: ## Run unittests
$(MVN_CMD) $(MVN_ARGS) test

###########
##@ Example
example: ## Build and test the pets CLI example
$(MVN_CMD) $(MVN_ARGS) -DskipTests package install:install
$(MVN_CMD) $(MVN_ARGS) -f examples/pets-cli verify
45 changes: 45 additions & 0 deletions examples/pets-cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# pets

A [picocli](https://picocli.info/) command line client for the Swagger Petstore in [`pet.yaml`](pet.yaml). Responses are printed with `TableBuilder`.

The default server is `http://petstore.swagger.io/v1`. A flag on the command line wins over the environment variable.

Requires Java 11 or newer. Install the prettytable jar into the local Maven repository first (the example depends on `io.github.rickwporter:prettytable:1.0.0`).

```bash
mvn -q package
java -jar target/pets.jar list --limit 5
```

`make test` runs the example tests. The tests talk to an in-process stub, not the public server.

## Commands

The commands follow `pet.yaml`: `list` is `GET /pets`, `add` is `POST /pets`, `show` is `GET /pets/{petId}`, and `delete` is `DELETE /pets/{petId}`.

Global options can go before the command or after it:

| Option | Environment variable | Meaning |
| --- | --- | --- |
| `-s`, `--server` | `API_HOST` | Petstore base URL. `http://petstore.swagger.io/v1` is used when both are omitted. |
| `-k`, `--api-key` | `API_KEY` | Bearer token. Omitted when both are empty. |
| `-f`, `--format` | `OUTPUT_FORMAT` | `text`, `rich`, `csv`, `html`, or `json`. `rich` when both are omitted. |
| `-h`, `--help` | | Help for the current command |

```bash
# First page holds at most 5 pets. Further pages come from the x-next header.
java -jar target/pets.jar list --limit 5

# One pet, as a Property / Value table.
java -jar target/pets.jar show 1

# Create a pet. id, name, and owner are required. tag is optional.
# The spec responds with an empty 201 body.
java -jar target/pets.jar add --id 7 --name Fido --owner Rick --tag trained

java -jar target/pets.jar delete 1
```

`list --limit` is sent to the server and must be from 1 to 100. When the response includes an `x-next` header, `list` follows that link and prints the combined pages. `show` and `add` print the pet fields `id`, `name`, `tag`, and `owner`. `add` and `delete` print `Empty response.` when the server returns no body.

`mvn exec:java -Dexec.args="list --limit 5"` runs the same main class without the shaded jar.
209 changes: 209 additions & 0 deletions examples/pets-cli/checkstyle.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">

<!--

Checkstyle configuration that checks the sun coding conventions from:

- the Java Language Specification at
https://docs.oracle.com/javase/specs/jls/se11/html/index.html

- the Sun Code Conventions at https://www.oracle.com/java/technologies/javase/codeconventions-contents.html

- the Javadoc guidelines at
https://www.oracle.com/technical-resources/articles/java/javadoc-tool.html

- the JDK Api documentation https://docs.oracle.com/en/java/javase/11/

- some best practices

Checkstyle is very configurable. Be sure to read the documentation at
https://checkstyle.org (or in your downloaded distribution).

Most Checks are configurable, be sure to consult the documentation.

To completely disable a check, just comment it out or delete it from the file.
To suppress certain violations please review suppression filters.

Finally, it is worth reading the documentation.

-->

<module name="Checker">
<!--
If you set the basedir property below, then all reported file
names will be relative to the specified directory. See
https://checkstyle.org/config.html#Checker

<property name="basedir" value="${basedir}"/>
-->
<property name="severity" value="error"/>

<property name="fileExtensions" value="java, properties, xml"/>

<!-- Excludes all 'module-info.java' files -->
<!-- See https://checkstyle.org/config_filefilters.html -->
<module name="BeforeExecutionExclusionFileFilter">
<property name="fileNamePattern" value="module\-info\.java$"/>
</module>

<!-- https://checkstyle.org/config_filters.html#SuppressionFilter -->
<module name="SuppressionFilter">
<property name="file" value="${org.checkstyle.sun.suppressionfilter.config}"
default="checkstyle-suppressions.xml" />
<property name="optional" value="true"/>
</module>

<!-- Checks that a package-info.java file exists for each package. -->
<!-- See https://checkstyle.org/config_javadoc.html#JavadocPackage -->
<!-- RWP: no interest in Javadocs
<module name="JavadocPackage"/>
-->

<!-- Checks whether files end with a new line. -->
<!-- See https://checkstyle.org/config_misc.html#NewlineAtEndOfFile -->
<module name="NewlineAtEndOfFile"/>

<!-- Checks that property files contain the same keys. -->
<!-- See https://checkstyle.org/config_misc.html#Translation -->
<module name="Translation"/>

<!-- Checks for Size Violations. -->
<!-- See https://checkstyle.org/config_sizes.html -->
<module name="FileLength"/>
<module name="LineLength">
<property name="fileExtensions" value="java"/>
<property name="max" value="120"/>
</module>

<!-- Checks for whitespace -->
<!-- See https://checkstyle.org/config_whitespace.html -->
<module name="FileTabCharacter"/>

<!-- Miscellaneous other checks. -->
<!-- See https://checkstyle.org/config_misc.html -->
<module name="RegexpSingleline">
<property name="format" value="\s+$"/>
<property name="minimum" value="0"/>
<property name="maximum" value="0"/>
<property name="message" value="Line has trailing spaces."/>
</module>

<!-- Checks for Headers -->
<!-- See https://checkstyle.org/config_header.html -->
<!-- <module name="Header"> -->
<!-- <property name="headerFile" value="${checkstyle.header.file}"/> -->
<!-- <property name="fileExtensions" value="java"/> -->
<!-- </module> -->

<module name="TreeWalker">

<!-- Checks for Javadoc comments. -->
<!-- See https://checkstyle.org/config_javadoc.html -->
<!-- RWP: Javadoc's are too verbose for my liking
<module name="InvalidJavadocPosition"/>
<module name="JavadocMethod"/>
<module name="JavadocType"/>
<module name="JavadocVariable"/>
<module name="JavadocStyle"/>
<module name="MissingJavadocMethod"/>
-->

<!-- Checks for Naming Conventions. -->
<!-- See https://checkstyle.org/config_naming.html -->
<module name="ConstantName"/>
<module name="LocalFinalVariableName"/>
<module name="LocalVariableName"/>
<module name="MemberName"/>
<module name="MethodName"/>
<module name="PackageName"/>
<module name="ParameterName"/>
<module name="StaticVariableName"/>
<module name="TypeName"/>

<!-- Checks for imports -->
<!-- See https://checkstyle.org/config_imports.html -->
<module name="AvoidStarImport"/>
<module name="IllegalImport"/> <!-- defaults to sun.* packages -->
<module name="RedundantImport"/>
<module name="UnusedImports">
<property name="processJavadoc" value="false"/>
</module>

<!-- Checks for Size Violations. -->
<!-- See https://checkstyle.org/config_sizes.html -->
<module name="MethodLength"/>
<module name="ParameterNumber"/>

<!-- Checks for whitespace -->
<!-- See https://checkstyle.org/config_whitespace.html -->
<module name="EmptyForIteratorPad"/>
<module name="GenericWhitespace"/>
<module name="MethodParamPad"/>
<module name="NoWhitespaceAfter"/>
<module name="NoWhitespaceBefore"/>
<module name="OperatorWrap"/>
<module name="ParenPad"/>
<module name="TypecastParenPad"/>
<module name="WhitespaceAfter"/>
<module name="WhitespaceAround"/>

<!-- Modifier Checks -->
<!-- See https://checkstyle.org/config_modifier.html -->
<module name="ModifierOrder"/>
<module name="RedundantModifier"/>

<!-- Checks for blocks. You know, those {}'s -->
<!-- See https://checkstyle.org/config_blocks.html -->
<module name="AvoidNestedBlocks"/>
<module name="EmptyBlock"/>
<module name="LeftCurly"/>
<module name="NeedBraces"/>
<module name="RightCurly"/>

<!-- Checks for common coding problems -->
<!-- See https://checkstyle.org/config_coding.html -->
<module name="EmptyStatement"/>
<module name="EqualsHashCode"/>
<module name="HiddenField"/>
<module name="IllegalInstantiation"/>
<module name="InnerAssignment"/>
<module name="MagicNumber"/>
<!-- RWP: should not need a default (particularly if you cover all the cases)
<module name="MissingSwitchDefault"/>
-->
<module name="MultipleVariableDeclarations"/>
<module name="SimplifyBooleanExpression"/>
<module name="SimplifyBooleanReturn"/>

<!-- Checks for class design -->
<!-- See https://checkstyle.org/config_design.html -->
<module name="DesignForExtension"/>
<module name="FinalClass"/>
<module name="HideUtilityClassConstructor"/>
<module name="InterfaceIsType"/>
<module name="VisibilityModifier"/>

<!-- Miscellaneous other checks. -->
<!-- See https://checkstyle.org/config_misc.html -->
<module name="ArrayTypeStyle"/>
<!-- FIXME: final parameters is a valid style check, just more change than I want to do now
<module name="FinalParameters"/>
-->
<!-- RWP: not sure the value in using a lowercase todo
<module name="TodoComment"/>
-->
<module name="UpperEll"/>

<!-- https://checkstyle.org/config_filters.html#SuppressionXpathFilter -->
<module name="SuppressionXpathFilter">
<property name="file" value="${org.checkstyle.sun.suppressionxpathfilter.config}"
default="checkstyle-xpath-suppressions.xml" />
<property name="optional" value="true"/>
</module>

</module>

</module>
Loading
Loading