Skip to content

Commit

Permalink
mthmuldersGH-323: use spotify code formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhaskar1312 committed May 7, 2024
1 parent 95414af commit 71bcea8
Show file tree
Hide file tree
Showing 63 changed files with 2,421 additions and 2,267 deletions.
14 changes: 14 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
<junit-pioneer.version>2.2.0</junit-pioneer.version>

<surefire.version>3.2.5</surefire.version>
<fmt-maven-plugin.version>2.23</fmt-maven-plugin.version>
<!-- 2.9.x is the most up-to-date version that still runs on Java 8.-->

<artifactsDir>target/distributions</artifactsDir>
<executable-suffix></executable-suffix>
Expand Down Expand Up @@ -149,6 +151,18 @@
</extensions>

<plugins>
<plugin>
<groupId>com.spotify.fmt</groupId>
<artifactId>fmt-maven-plugin</artifactId>
<version>${fmt-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand Down
112 changes: 59 additions & 53 deletions src/main/java/it/mulders/mcs/App.java
Original file line number Diff line number Diff line change
@@ -1,78 +1,84 @@
package it.mulders.mcs;

import java.net.URI;
import java.net.URISyntaxException;

import it.mulders.mcs.cli.Cli;
import it.mulders.mcs.cli.CommandClassFactory;
import it.mulders.mcs.cli.SystemPropertyLoader;
import it.mulders.mcs.common.McsExecutionExceptionHandler;
import java.net.URI;
import java.net.URISyntaxException;
import picocli.CommandLine;

public class App {
public static void main(final String... args) {
System.exit(doMain(args));
}
public static void main(final String... args) {
System.exit(doMain(args));
}

// Visible for testing
static int doMain(final String... originalArgs) {
return doMain(new Cli(), new SystemPropertyLoader(), originalArgs);
}
// Visible for testing
static int doMain(final String... originalArgs) {
return doMain(new Cli(), new SystemPropertyLoader(), originalArgs);
}

static int doMain(final Cli cli, final SystemPropertyLoader systemPropertyLoader, final String... originalArgs) {
System.setProperties(systemPropertyLoader.getProperties());
setUpProxy();
var program = new CommandLine(cli, new CommandClassFactory(cli))
.setExecutionExceptionHandler(new McsExecutionExceptionHandler());
static int doMain(
final Cli cli,
final SystemPropertyLoader systemPropertyLoader,
final String... originalArgs) {
System.setProperties(systemPropertyLoader.getProperties());
setUpProxy();
var program =
new CommandLine(cli, new CommandClassFactory(cli))
.setExecutionExceptionHandler(new McsExecutionExceptionHandler());

var args = isInvocationWithoutSearchCommand(program, originalArgs)
var args =
isInvocationWithoutSearchCommand(program, originalArgs)
? prependSearchCommandToArgs(originalArgs)
: originalArgs;

return program.execute(args);
}
return program.execute(args);
}

private static void setUpProxy() {
var httpProxy = System.getenv( "HTTP_PROXY" );
var httpsProxy = System.getenv( "HTTPS_PROXY" );
private static void setUpProxy() {
var httpProxy = System.getenv("HTTP_PROXY");
var httpsProxy = System.getenv("HTTPS_PROXY");

try {
if ( httpProxy != null && !httpProxy.isEmpty() ) {
final URI uri = new URI( httpProxy );
try {
if (httpProxy != null && !httpProxy.isEmpty()) {
final URI uri = new URI(httpProxy);

System.setProperty( "http.proxyHost", uri.getHost() );
System.setProperty( "http.proxyPort", Integer.toString( uri.getPort() ) );
}
System.setProperty("http.proxyHost", uri.getHost());
System.setProperty("http.proxyPort", Integer.toString(uri.getPort()));
}

if ( httpsProxy != null && !httpsProxy.isEmpty() ) {
final URI uri = new URI( httpsProxy );
System.setProperty( "https.proxyHost", uri.getHost() );
System.setProperty( "https.proxyPort", Integer.toString( uri.getPort() ) );
}
} catch ( URISyntaxException e ) {
System.err.println("Error while setting up proxy from environment: HTTP_PROXY=[%s], HTTPS_PROXY=[%s]".formatted( httpProxy, httpsProxy ) );
}
if (httpsProxy != null && !httpsProxy.isEmpty()) {
final URI uri = new URI(httpsProxy);
System.setProperty("https.proxyHost", uri.getHost());
System.setProperty("https.proxyPort", Integer.toString(uri.getPort()));
}
} catch (URISyntaxException e) {
System.err.println(
"Error while setting up proxy from environment: HTTP_PROXY=[%s], HTTPS_PROXY=[%s]"
.formatted(httpProxy, httpsProxy));
}
}

static boolean isInvocationWithoutSearchCommand(CommandLine program, String... args) {
try {
program.parseArgs(args);
return false;
} catch (CommandLine.ParameterException pe1) {
try {
program.parseArgs(prependSearchCommandToArgs(args));
return true;
} catch (CommandLine.ParameterException pe2) {
return false;
}
}
static boolean isInvocationWithoutSearchCommand(CommandLine program, String... args) {
try {
program.parseArgs(args);
return false;
} catch (CommandLine.ParameterException pe1) {
try {
program.parseArgs(prependSearchCommandToArgs(args));
return true;
} catch (CommandLine.ParameterException pe2) {
return false;
}
}
}

static String[] prependSearchCommandToArgs(String... originalArgs) {
var args = new String[originalArgs.length + 1];
args[0] = "search";
System.arraycopy(originalArgs, 0, args, 1, originalArgs.length);
static String[] prependSearchCommandToArgs(String... originalArgs) {
var args = new String[originalArgs.length + 1];
args[0] = "search";
System.arraycopy(originalArgs, 0, args, 1, originalArgs.length);

return args;
}
return args;
}
}
19 changes: 9 additions & 10 deletions src/main/java/it/mulders/mcs/cli/ClasspathVersionProvider.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package it.mulders.mcs.cli;

import picocli.CommandLine;

import java.util.Properties;
import picocli.CommandLine;

/**
* {@link CommandLine.IVersionProvider} implementation that returns version information from a
* {@code /mcs.properties} file in the classpath.
*/
public class ClasspathVersionProvider implements CommandLine.IVersionProvider {
@Override
public String[] getVersion() throws Exception {
var properties = new Properties();
try (var stream = getClass().getResourceAsStream("/mcs.properties")) {
properties.load(stream);
var version = String.format("mcs v%s", properties.getProperty("mcs.version"));
return new String[] { version };
}
@Override
public String[] getVersion() throws Exception {
var properties = new Properties();
try (var stream = getClass().getResourceAsStream("/mcs.properties")) {
properties.load(stream);
var version = String.format("mcs v%s", properties.getProperty("mcs.version"));
return new String[] {version};
}
}
}
Loading

0 comments on commit 71bcea8

Please sign in to comment.