All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
io.knowledgelinks.cicd.semantic.versioning.commandline.ApiCli Maven / Gradle / Ivy
package io.knowledgelinks.cicd.semantic.versioning.commandline;
/*-
* #%L
* Semantic Versioning
* %%
* Copyright (C) 2022 - 2023 Knowledgelinks
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import io.knowledgelinks.cicd.semantic.versioning.api.BuildInfo;
import io.knowledgelinks.cicd.semantic.versioning.api.BuildInfoBuilder;
import io.knowledgelinks.cicd.semantic.versioning.api.SemanticVersioningApi;
import io.knowledgelinks.cicd.semantic.versioning.api.SemanticVersioningApiBuilder;
import io.knowledgelinks.cicd.semantic.versioning.config.Configuration;
import io.knowledgelinks.cicd.semantic.versioning.config.ConfigutationWriter;
import io.knowledgelinks.cicd.semantic.versioning.config.Globals;
import io.knowledgelinks.cicd.semantic.versioning.enums.ArtifactTypes;
import io.knowledgelinks.cicd.semantic.versioning.enums.CliReturnType;
import io.knowledgelinks.cicd.semantic.versioning.enums.VersionTypes;
import io.knowledgelinks.cicd.semantic.versioning.exceptions.CliParameterException;
import io.knowledgelinks.cicd.semantic.versioning.exceptions.GitTagRetrievalException;
import io.knowledgelinks.cicd.semantic.versioning.exceptions.SemanticVersioningException;
import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Option;
import picocli.CommandLine.Parameters;
import picocli.CommandLine.Spec;
/**
* This is the main class for accessing the semantic versioning command-line interface. See the
* command-line help by utilizing --help when calling the application.
*
* @author mstabile75
*
*/
@Command(name = "semantic-versioning", mixinStandardHelpOptions = true, version = "checksum 4.0",
description = Globals.API_DESCRIPTION)
public class ApiCli implements Callable {
@Parameters(index = "0", arity = "0..1", description = "Specify the return type.")
private CliReturnType returnType = CliReturnType.NONE;
@Option(names = {"-h", "--help"}, usageHelp = true, description = "display a help message")
private boolean helpRequested = false; // NOPMD Field is utilized by piccocli and not locally
@Option(names = {"-e", "--env"},
description = "Generate a starter environment variable configuration file")
private boolean envRequested = false;
@Option(names = {"-p", "--properties"},
description = "Specify a java properties based configuration file")
private String propertiesFile = null;
@Option(names = {"-g", "--generate-properties"},
description = "Generate a starter java properties based configuration file")
private boolean propertiesRequested = false;
@Option(names = {"-V", "--version"}, description = "display the version name")
private boolean showVersion = false;
@Option(names = {"-P", "--pretty-print"}, description = "pretty print the return value")
private boolean prettyPrint = false;
@Option(names = {"-M", "--major"}, description = Globals.MAJOR_CLI_DESCRIPTION)
private int majorVersion = -1;
@Option(names = {"-m", "--minor"}, description = Globals.MINOR_CLI_DESCRIPTION)
private int minorVersion = -1;
@Option(names = {"-a", "--artifact-name"}, description = Globals.ARTIFACT_NAME_CLI_DESCRIPTION)
private String artifactName = null;
@Option(names = {"-t", "--artifact-type"}, description = Globals.ARTIFACT_TYPES_CLI_DESCRIPTION)
private List artifactTypes = new ArrayList<>();
@Option(names = {"-b", "--branch"}, description = Globals.BRANCH_NAME_CLI_DESCRIPTION)
private String branchName = null;
@Option(names = {"-r", "--respository-url"}, description = Globals.REPOSITORY_URL_CLI_DESCRIPTION)
private String repositoryUrl = null;
@Option(names = {"-f", "--version-filter"}, description = Globals.VERSION_FILTER_CLI_DESCRIPTION)
private VersionTypes versionFilterType = VersionTypes.NONE;
@Option(names = {"-w", "--working-directory"},
description = Globals.WORKING_DIRECTORY_CLI_DESCRIPTION)
private String workingDirectory = System.getProperty("user.dir");
private Configuration config;
private SemanticVersioningApi api;
@Spec
CommandSpec spec;
@Override
public Integer call() throws Exception {
// if the properties file is specified via command line args add it to the system properties
if (propertiesFile != null && !propertiesFile.isBlank()) {
System.setProperty(Globals.SYSTEM_PROPERTIES_SPECIFIED_PROPERTIES_FILE_KEY, propertiesFile);
}
// use the default configuration unless set on the class separately
Configuration config = this.config == null ? Configuration.getInstance() : this.config;
if (showVersion) {
System.out.println(Configuration.getApiVersion()); // NOPMD command line output
return 0;
}
if (envRequested) {
ConfigutationWriter.writeEnvironmentFile(config);
return 0;
}
if (propertiesRequested) {
ConfigutationWriter.writePropertiesFile(config);
return 0;
}
BuildInfo buildInfo = new BuildInfoBuilder(config).setArtifactName(artifactName)
.setBranchName(branchName).setMajorVersion(majorVersion).setMinorVersion(minorVersion)
.setRepsoitoryUrl(repositoryUrl).setArtifactTypes(artifactTypes).build();
api = new SemanticVersioningApiBuilder().setConfig(config).setBuildInfo(buildInfo)
.setWorkingDirectory(workingDirectory).build();
List errors = new ArrayList<>();
try {
api.retrieveGitTags();
} catch (GitTagRetrievalException e) {
errors.add(String.format("%s: %s", e.getClass().getName(), e.getMessage()));
}
try {
api.generateArtifactNames();
} catch (SemanticVersioningException e) {
errors.add(String.format("%s: %s", e.getClass().getName(), e.getMessage()));
}
try {
api.calculateVersions();
} catch (SemanticVersioningException e) {
errors.add(String.format("%s: %s", e.getClass().getName(), e.getMessage()));
}
switch (returnType) {
case JSON:
System.out.println(api.getJson(prettyPrint, errors)); // NOPMD command line output
break;
case KEY_VALUE:
System.out.println(api.getKeyValue(errors)); // NOPMD command line output
break;
case XML:
System.out.println(api.getXml(prettyPrint, errors)); // NOPMD command line output
break;
case ENV_FILE:
ConfigutationWriter.writeFile(api.getEnvFileData(errors), Globals.BUILD_INFO_FILE_NAME,
config.getWorkingDirectory(), true);
break;
case VERSION_LIST:
System.out.println(
String.join(System.lineSeparator(), api.getVersionListAsStringList(versionFilterType))); // NOPMD
// command
// line
// output
break;
default:
new CliParameterException("returnType not supported", CliReturnType.NONE).printStackTrace(); // NOPMD
// output
if (spec != null) {
spec.commandLine().usage(System.err);
}
return 1;
}
return 0;
}
public void setReturnType(CliReturnType returnType) {
this.returnType = returnType;
}
public void setHelpRequested(boolean helpRequested) {
this.helpRequested = helpRequested;
}
public void setShowVersion(boolean showVersion) {
this.showVersion = showVersion;
}
public void setPrettyPrint(boolean prettyPrint) {
this.prettyPrint = prettyPrint;
}
public void setMajorVersion(int majorVersion) {
this.majorVersion = majorVersion;
}
public void setMinorVersion(int minorVersion) {
this.minorVersion = minorVersion;
}
public void setArtifactName(String artifactName) {
this.artifactName = artifactName;
}
public void setBranchName(String branchName) {
this.branchName = branchName;
}
public void setRepositoryUrl(String repositoryUrl) {
this.repositoryUrl = repositoryUrl;
}
public void setWorkingDirectory(String workingDirectory) {
this.workingDirectory = workingDirectory;
}
public void setEnvRequested(boolean envRequested) {
this.envRequested = envRequested;
}
public void setPropertiesFile(String propertiesFile) {
this.propertiesFile = propertiesFile;
}
public void setPropertiesRequested(boolean propertiesRequested) {
this.propertiesRequested = propertiesRequested;
}
public void setArtifactTypes(List artifactTypes) {
this.artifactTypes = artifactTypes;
}
public void setVersionFilterType(VersionTypes versionFilterType) {
this.versionFilterType = versionFilterType;
}
public void setConfig(Configuration config) {
this.config = config;
}
public SemanticVersioningApi getApi() {
return api;
}
/**
* Entrypoint for the application
*
* @param args enter --help on the command-line to see options
*/
public static void main(String... args) {
int exitCode =
new CommandLine(new ApiCli()).setCaseInsensitiveEnumValuesAllowed(true).execute(args);
System.exit(exitCode);
}
}