forked from mthmulders/mcs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mthmuldersGH-323: use spotify code formatter
- Loading branch information
1 parent
95414af
commit 71bcea8
Showing
63 changed files
with
2,421 additions
and
2,267 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
19
src/main/java/it/mulders/mcs/cli/ClasspathVersionProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}; | ||
} | ||
} | ||
} |
Oops, something went wrong.