Skip to content

Commit

Permalink
Merge pull request #112 from crowdin/fix/exit_with_error_code
Browse files Browse the repository at this point in the history
Return non zero exit code if error happened (fix #77)
  • Loading branch information
dmtr-p authored Apr 17, 2019
2 parents 20f434a + f65235a commit c015edf
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 75 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
**Unreleased**

+ Return non zero exit code if error happened (fix [#77](https://github.com/crowdin/crowdin-cli-2/issues/77))

**Version 2.0.29**
+ Decrease version of log4j (fix [#109](https://github.com/crowdin/crowdin-cli-2/issues/109))

Expand Down
8 changes: 6 additions & 2 deletions src/main/java/com/crowdin/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import com.crowdin.cli.commands.Commands;
import com.crowdin.cli.commands.CrowdinCliCommands;
import com.crowdin.cli.commands.CrowdinCliOptions;
import org.apache.commons.cli.*;
import com.crowdin.cli.utils.ConsoleUtil;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;

public class Cli {

Expand All @@ -17,7 +21,7 @@ public static void main(String[] args) {
c.run(command, commandLine);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
System.exit(1);
ConsoleUtil.exitError();
}
}
}
60 changes: 30 additions & 30 deletions src/main/java/com/crowdin/cli/commands/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ private void initialize(String resultCmd, CommandLine commandLine) {
if (this.isDebug) {
e.printStackTrace();
}
System.exit(0);
ConsoleUtil.exitError();
}
} else {
System.out.println("Configuration file '" + this.configFile.getAbsolutePath() + "' does not exist");
System.exit(0);
ConsoleUtil.exitError();
}
if (this.cliConfig != null) {
try {
Expand All @@ -113,11 +113,11 @@ private void initialize(String resultCmd, CommandLine commandLine) {
if (this.isDebug) {
e.printStackTrace();
}
System.exit(0);
ConsoleUtil.exitError();
}
} else {
System.out.println("Configuration file '" + this.configFile.getAbsolutePath() + "' does not exist");
System.exit(0);
ConsoleUtil.exitError();
}
if (this.identity != null && this.identity.isFile()) {
this.propertiesBean = this.readIdentityProperties(this.propertiesBean);
Expand Down Expand Up @@ -157,7 +157,7 @@ private PropertiesBean readIdentityProperties(PropertiesBean propertiesBean) {
if (this.isDebug) {
e.printStackTrace();
}
System.exit(0);
ConsoleUtil.exitError();
}

if (this.identityCliConfig != null) {
Expand Down Expand Up @@ -213,11 +213,11 @@ public void run(String resultCmd, CommandLine commandLine) {

if (commandLine == null) {
System.out.println(RESOURCE_BUNDLE.getString("commandline_null"));
System.exit(0);
ConsoleUtil.exitError();
}
if (resultCmd == null) {
System.out.println(RESOURCE_BUNDLE.getString("command_not_found"));
System.exit(0);
ConsoleUtil.exitError();
}

this.branch = this.commandUtils.getBranch(commandLine);
Expand Down Expand Up @@ -411,7 +411,7 @@ private void createBranch(String name) {
if (this.isDebug) {
e.printStackTrace();
}
System.exit(0);
ConsoleUtil.exitError();
}
JSONObject branch = parser.parseJson(clientResponse.getEntity(String.class));
if (this.isVerbose) {
Expand All @@ -428,7 +428,7 @@ private void createBranch(String name) {
if (branch != null && branch.getJSONObject(RESPONSE_ERROR) != null && branch.getJSONObject(RESPONSE_ERROR).getString(RESPONSE_MESSAGE) != null) {
System.out.println(branch.getJSONObject(RESPONSE_ERROR).getString(RESPONSE_MESSAGE));
}
System.exit(0);
ConsoleUtil.exitError();
}
}

Expand Down Expand Up @@ -653,7 +653,7 @@ public void run() {
if (noFiles) {
System.out.println("Error: No source files to upload.\n" +
"Check your configuration file to ensure that they contain valid directives.");
System.exit(0);
ConsoleUtil.exitError();
}
return result;
}
Expand Down Expand Up @@ -840,7 +840,7 @@ public void run() {
public void initCli() {
if (credentials == null) {
System.out.println(RESOURCE_BUNDLE.getString("wrong_connection"));
System.exit(0);
ConsoleUtil.exitError();
}
if (credentials.getProjectIdentifier() == null || credentials.getProjectKey() == null || credentials.getBaseUrl() == null
||credentials.getProjectIdentifier().isEmpty() || credentials.getProjectKey().isEmpty() || credentials.getBaseUrl().isEmpty()) {
Expand All @@ -853,7 +853,7 @@ public void initCli() {
if (credentials.getBaseUrl() == null || credentials.getBaseUrl().isEmpty()) {
System.out.println("Base url is empty");
}
System.exit(0);
ConsoleUtil.exitError();
}
CrowdinApiClient crwdn = new Crwdn();
JSONObject result = null;
Expand All @@ -875,11 +875,11 @@ public void initCli() {
if (isDebug) {
e.printStackTrace();
}
System.exit(0);
ConsoleUtil.exitError();
}
if (result == null) {
System.out.println(RESOURCE_BUNDLE.getString("initialisation_failed"));
System.exit(0);
ConsoleUtil.exitError();
}
if (result.has(RESPONSE_SUCCESS)) {
Boolean responseStatus = result.getBoolean(RESPONSE_SUCCESS);
Expand All @@ -895,15 +895,15 @@ public void initCli() {
JSONObject error = result.getJSONObject(RESPONSE_ERROR);
if (error == null) {
System.out.println("Initialization failed");
System.exit(0);
ConsoleUtil.exitError();
}
if (error.has(RESPONSE_CODE)) {
if ("0".equals(error.get(RESPONSE_CODE).toString())) {
if (error.has(RESPONSE_MESSAGE)) {
String message = error.getString(RESPONSE_MESSAGE);
if (message != null) {
System.out.println(message);
System.exit(0);
ConsoleUtil.exitError();
}
}
}
Expand All @@ -912,7 +912,7 @@ public void initCli() {
}
} else {
System.out.println(RESOURCE_BUNDLE.getString("initialisation_failed"));
System.exit(0);
ConsoleUtil.exitError();
}
}

Expand Down Expand Up @@ -958,7 +958,7 @@ public void run() {
if (isDebug) {
e.printStackTrace();
}
System.exit(0);
ConsoleUtil.exitError();
}
result = parser.parseJson(clientResponse.getEntity(String.class));
if (result != null) {
Expand All @@ -979,7 +979,7 @@ public void run() {
System.out.println("error");
System.out.println("branch '" + branch + "' does not exist in the project");
}
System.exit(0);
ConsoleUtil.exitError();
}
}
if (isVerbose) {
Expand Down Expand Up @@ -1170,7 +1170,7 @@ public void help(String resultCmd) {
private void dryrunSources(CommandLine commandLine) {
List<String> files = this.list(SOURCES, "sources");
if (files.size() < 1 ) {
System.exit(0);
ConsoleUtil.exitError();
}
commandUtils.sortFilesName(files);
String commonPath = "";
Expand Down Expand Up @@ -1333,7 +1333,7 @@ private void dryrunProject(CommandLine commandLine) {
private void lint() {
if (cliConfig == null) {
System.out.println(RESOURCE_BUNDLE.getString("configuration_file_empty"));
System.exit(0);
ConsoleUtil.exitError();
} else {
CliProperties cliProperties = new CliProperties();
PropertiesBean propertiesBean = cliProperties.loadProperties(cliConfig);
Expand All @@ -1343,20 +1343,20 @@ private void lint() {
}
if (propertiesBean == null && propertiesBeanIdentity == null) {
System.out.println(RESOURCE_BUNDLE.getString("configuration_file_empty"));
System.exit(0);
ConsoleUtil.exitError();
}

if (propertiesBean == null || (propertiesBean != null && propertiesBean.getProjectIdentifier() == null)) {
if (propertiesBeanIdentity == null || (propertiesBeanIdentity != null && propertiesBeanIdentity.getProjectIdentifier() == null)) {
System.out.println(RESOURCE_BUNDLE.getString("error_missed_project_identifier"));
System.exit(0);
ConsoleUtil.exitError();
}
}

if (propertiesBean == null || (propertiesBean != null && propertiesBean.getApiKey() == null)) {
if (propertiesBeanIdentity == null || (propertiesBeanIdentity != null && propertiesBeanIdentity.getApiKey() == null)) {
System.out.println(RESOURCE_BUNDLE.getString("error_missed_api_key"));
System.exit(0);
ConsoleUtil.exitError();
}
}

Expand All @@ -1365,7 +1365,7 @@ private void lint() {
String baseUrl = Utils.getBaseUrl();
if (baseUrl == null || baseUrl.isEmpty()) {
System.out.println(RESOURCE_BUNDLE.getString("missed_base_url"));
System.exit(0);
ConsoleUtil.exitError();
}
}
}
Expand All @@ -1383,25 +1383,25 @@ private void lint() {
File base = new File(basePath);
if (!base.exists()) {
System.out.println(RESOURCE_BUNDLE.getString("bad_base_path"));
System.exit(0);
ConsoleUtil.exitError();
}
}

if (propertiesBean.getFiles() == null) {
System.out.println(RESOURCE_BUNDLE.getString("error_missed_section_files"));
System.exit(0);
ConsoleUtil.exitError();
} else if (propertiesBean.getFiles().isEmpty()) {
System.out.println(RESOURCE_BUNDLE.getString("empty_section_file"));
System.exit(0);
ConsoleUtil.exitError();
} else {
for (FileBean fileBean : propertiesBean.getFiles()) {
if (fileBean.getSource() == null || fileBean.getSource().isEmpty()) {
System.out.println(RESOURCE_BUNDLE.getString("error_empty_section_source_or_translation"));
System.exit(0);
ConsoleUtil.exitError();
}
if (fileBean.getTranslation() == null || fileBean.getTranslation().isEmpty()) {
System.out.println(RESOURCE_BUNDLE.getString("error_empty_section_source_or_translation"));
System.exit(0);
ConsoleUtil.exitError();
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/crowdin/cli/properties/CliProperties.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.crowdin.cli.properties;

import com.crowdin.cli.utils.ConsoleUtil;
import com.crowdin.cli.utils.MessageSource;
import com.crowdin.cli.utils.Utils;

Expand Down Expand Up @@ -76,7 +77,7 @@ public class CliProperties {
public PropertiesBean loadProperties(HashMap<String, Object> properties) {
if (properties == null || properties.isEmpty()) {
System.out.println(RESOURCE_BUNDLE.getString("error_empty_properties_file"));
System.exit(0);
ConsoleUtil.exitError();
}
PropertiesBean pb = new PropertiesBean();
for (Map.Entry<String, Object> property : properties.entrySet()) {
Expand Down Expand Up @@ -243,7 +244,7 @@ public PropertiesBean validateProperties(PropertiesBean pb) {
//Property bean
if (pb == null) {
System.out.println(RESOURCE_BUNDLE.getString("error_property_bean_null"));
System.exit(0);
ConsoleUtil.exitError();
}
//Preserve hierarchy
if (pb.getPreserveHierarchy() == null) {
Expand All @@ -252,7 +253,7 @@ public PropertiesBean validateProperties(PropertiesBean pb) {
if (pb.getBasePath() != null && !pb.getBasePath().isEmpty()) {
if (!Paths.get(pb.getBasePath()).isAbsolute()){
System.out.println(RESOURCE_BUNDLE.getString("bad_base_path"));
System.exit(0);
ConsoleUtil.exitError();
}
} else {
pb.setBasePath("");
Expand Down Expand Up @@ -386,7 +387,7 @@ public PropertiesBean validateProperties(PropertiesBean pb) {
}
}
if (!hasValidFile) {
System.exit(0);
ConsoleUtil.exitError();
}
}
return pb;
Expand Down
Loading

0 comments on commit c015edf

Please sign in to comment.