Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bal-build-task] Add help text for add command #1598

Merged
merged 7 commits into from
Feb 1, 2024
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 @@ -108,8 +108,8 @@ public void execute(ToolContext toolContext) {
if (openAPI.isEmpty()) {
return;
}
// Handle the details with Ballerina.toml option table, if option table is null then by default
// client will generate
//Extract the details using the `tool config options` table in the `Ballerina.toml` file.
//If the `tool options` table is not specified in the TOML file, the client will be generated by default.
TomlTableNode tomlTableNode = toolContext.optionsTable();
if (tomlTableNode == null) {
// Default generate client
Expand Down
13 changes: 10 additions & 3 deletions openapi-cli/src/main/java/io/ballerina/openapi/cmd/Add.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
description = "Update Ballerina.toml with the OpenAPI CLI code generation implementation"
)
public class Add implements BLauncherCmd {
private static final String COMMAND_IDENTIFIER = "openapi-add";
private final PrintStream outStream;
private final boolean exitWhenFinish;

Expand Down Expand Up @@ -108,6 +109,11 @@ public void execute() {
Paths.get(System.getProperty("user.dir")).toAbsolutePath();

try {
if (baseCmd.helpFlag) {
String commandUsageInfo = BLauncherCmd.getCommandUsageInfo(COMMAND_IDENTIFIER);
outStream.println(commandUsageInfo);
return;
}
//check the given path is the Ballerina package
Optional<Path> ballerinaTomlPath = validateBallerinaProject(projectPath);
if (ballerinaTomlPath.isEmpty()) {
Expand Down Expand Up @@ -136,6 +142,7 @@ public void execute() {
//write toml file
try (FileWriter writer = new FileWriter(tomlPath.toString(), StandardCharsets.UTF_8)) {
writer.write(content);
outStream.print(ErrorMessages.TOML_UPDATED_MSG);
}
}
} catch (BallerinaOpenApiException | IOException e) {
Expand Down Expand Up @@ -212,7 +219,7 @@ private NodeList<DocumentMemberDeclarationNode> populateOpenAPITomlConfig(CmdOpt
SeparatedNodeList<ValueNode> value = createSeparatedNodeList(arrayItems);
ArrayNode arrayNode = NodeFactory.createArrayNode(createToken(SyntaxKind.OPEN_BRACKET_TOKEN), value,
createToken(SyntaxKind.CLOSE_BRACKET_TOKEN));
KeyValueNode tagNodes = NodeFactory.createKeyValueNode(getKeyNode("option.tags"), getAssignToken(),
KeyValueNode tagNodes = NodeFactory.createKeyValueNode(getKeyNode("options.tags"), getAssignToken(),
arrayNode);
moduleMembers = moduleMembers.add(tagNodes);
}
Expand All @@ -224,7 +231,7 @@ private NodeList<DocumentMemberDeclarationNode> populateOpenAPITomlConfig(CmdOpt
SeparatedNodeList<ValueNode> value = createSeparatedNodeList(arrayItems);
ArrayNode arrayNode = NodeFactory.createArrayNode(createToken(SyntaxKind.OPEN_BRACKET_TOKEN), value,
createToken(SyntaxKind.CLOSE_BRACKET_TOKEN));
KeyValueNode tagNodes = NodeFactory.createKeyValueNode(getKeyNode("option.operations"),
KeyValueNode tagNodes = NodeFactory.createKeyValueNode(getKeyNode("options.operations"),
getAssignToken(), arrayNode);
moduleMembers = moduleMembers.add(tagNodes);;
}
Expand All @@ -236,7 +243,7 @@ private NodeList<DocumentMemberDeclarationNode> populateOpenAPITomlConfig(CmdOpt
moduleMembers = moduleMembers.add(SampleNodeGenerator.createStringKV("options.clientMethods",
optionsBuilder.getClientMethod(), null));
}
if (optionsBuilder.getLicensePath() != null || !optionsBuilder.getLicensePath().isBlank()) {
if (optionsBuilder.getLicensePath() != null && !optionsBuilder.getLicensePath().isBlank()) {
moduleMembers = moduleMembers.add(SampleNodeGenerator.createStringKV("options.licensePath",
optionsBuilder.getLicensePath(), null));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
* @since 1.9.0
*/
public class BaseCmd {
@CommandLine.Option(names = {"-h", "--help"}, hidden = true)
public boolean helpFlag;
@CommandLine.Option(names = {"-i", "--input"}, description = "Generating the client and service both files")
public String inputPath;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public class ErrorMessages {
"cannot find 'Ballerina.toml' file.%n%n";
public static final String INVALID_INPUT_PATH = "ERROR: provide input oas contract path.%n%n";

public static final String TOML_UPDATED_MSG = "The `Ballerina.toml` file is updated successfully with" +
" the OpenAPI tool configuration.";

private ErrorMessages() {
throw new AssertionError();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,6 @@ public class OpenApiCmd implements BLauncherCmd {

@CommandLine.Mixin
private BaseCmd baseCmd = new BaseCmd();
@CommandLine.Option(names = {"-h", "--help"}, hidden = true)
private boolean helpFlag;

@CommandLine.Option(names = {"-o", "--output"}, description = "Location of the generated Ballerina service, " +
"client and model files.")
Expand Down Expand Up @@ -197,7 +195,7 @@ public void execute() {
}

private boolean isHelp() {
return helpFlag || (argList != null && argList.get(0).equals("help"))
return baseCmd.helpFlag || (argList != null && argList.get(0).equals("help"))
|| (argList == null && baseCmd.inputPath == null);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
NAME
bal openapi add - Update the `Ballerina.toml` file with the OpenAPI tool configuration details for generating a Ballerina service or client.

SYNOPSIS
bal openapi add [-i | --input] <openapi-contract-file-path>
[-p | --package] <package-location>
[--id] <service/client id>
[--module] <module-name-for-generated-service/client>
[--mode] <mode-type>
[--tags] <tag-names>
[--operations] <operation-names>
[-n | --nullable]
[--license] <license-file-path>
[--client-methods] <resource|remote>

DESCRIPTION
Update the `Ballerina.toml` file with the OpenAPI tool configuration details for generating a Ballerina source
(either a mock service or a client stub) from the given OpenAPI definition file and other command options.

OPTIONS
-i, --input <openapi-contract-file-path>
This is a mandatory input. The given OpenAPI contract will
generate a Ballerina service and the client stub files for the given
OpenAPI contract. The OpenAPI contract can be either a YAML or a JSON.

--id <service/client id>
This is a mandatory input. The given service/client ID will be used
to identify the generated service/client in the `Ballerina.toml` file.
This ID will be used to generate the Ballerina service/client
sources cache in the target.

--module <module-name-for-generated-service/client>
This is a mandatory input. The given module name will be used to
lnash94 marked this conversation as resolved.
Show resolved Hide resolved
generate the Ballerina service/client sources in the given module
directory.

-p, --package <package-location>
This is an optional input. The given package location will be used
to update the `Ballerina.toml` file in the given
package directory.

--mode <mode-type>
Mode type can be 'service' or 'client'. The Ballerina service and
lnash94 marked this conversation as resolved.
Show resolved Hide resolved
client will be generated according to the specified mode. The default option is `client`.

-n, --nullable
Optional. The JSON schema properties that are not marked as `nullable:true` may
return as null in some responses. It will result in a JSON schema to
Ballerina record data binding error. This is a safe option to
generate all data types in the record with Ballerina nil support.

--license <license-file-path>
Optional. The `.bal` files will generate with the given copyright or
license header.

--tags <tag-names>
Optional. These tags are used to filter the operations tags that are needed to
generate services.

--operations <operation-names>
Optional. List of operations to generate the Ballerina service or client.

--client-methods <resource|remote>
Optional. This option can be used in client generation to select the client method type, which can be `resource` or `remote`. The default option is `resource`.

EXAMPLES
Update the `Ballerina.toml` file for generating a Ballerina client using a `hello.yaml` OpenAPI contract to generate a directory named `hello`.
$ bal openapi add -i hello.yaml --id hello_client --mode client --module hello

Update the `Ballerina.toml` file for generating a Ballerina client using a `hello.yaml` OpenAPI contract with the tags filter.
$ bal openapi add -i hello.yaml --id hello_client --mode client --module hello --tags tag_ID
Loading