aboutsummaryrefslogtreecommitdiffstats
path: root/src/main
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2024-04-30 14:59:03 +0200
committerJulien HENRY <julien.henry@sonarsource.com>2024-05-03 13:07:43 +0200
commitb04cd6ba08a93a88553c1dfceaa725ced1ba381c (patch)
tree762512e8d5a70bd6981706c5d779a982c57ad75a /src/main
parentfd82dd7b5a54d2042da17baff7088329123797ac (diff)
downloadsonar-scanner-cli-b04cd6ba08a93a88553c1dfceaa725ced1ba381c.tar.gz
sonar-scanner-cli-b04cd6ba08a93a88553c1dfceaa725ced1ba381c.zip
SCANCLI-145 Update to the scanner library 3.0
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/org/sonarsource/scanner/cli/Cli.java6
-rw-r--r--src/main/java/org/sonarsource/scanner/cli/Conf.java6
-rw-r--r--src/main/java/org/sonarsource/scanner/cli/Logs.java34
-rw-r--r--src/main/java/org/sonarsource/scanner/cli/Main.java52
-rw-r--r--src/main/java/org/sonarsource/scanner/cli/ScannerEngineBootstrapperFactory.java (renamed from src/main/java/org/sonarsource/scanner/cli/ScannerFactory.java)38
5 files changed, 71 insertions, 65 deletions
diff --git a/src/main/java/org/sonarsource/scanner/cli/Cli.java b/src/main/java/org/sonarsource/scanner/cli/Cli.java
index aafed18..4de2a36 100644
--- a/src/main/java/org/sonarsource/scanner/cli/Cli.java
+++ b/src/main/java/org/sonarsource/scanner/cli/Cli.java
@@ -20,7 +20,6 @@
package org.sonarsource.scanner.cli;
import java.util.Properties;
-import org.sonarsource.scanner.api.ScannerProperties;
import static java.util.Arrays.asList;
@@ -73,10 +72,7 @@ class Cli {
private int processNextArg(String[] args, int pos) {
String arg = args[pos];
- if (pos == 0 && arg.charAt(0) != '-') {
- props.setProperty(ScannerProperties.TASK, arg);
-
- } else if (asList("-h", "--help").contains(arg)) {
+ if (asList("-h", "--help").contains(arg)) {
printUsage();
exit.exit(Exit.SUCCESS);
diff --git a/src/main/java/org/sonarsource/scanner/cli/Conf.java b/src/main/java/org/sonarsource/scanner/cli/Conf.java
index 19b020e..4e9a53a 100644
--- a/src/main/java/org/sonarsource/scanner/cli/Conf.java
+++ b/src/main/java/org/sonarsource/scanner/cli/Conf.java
@@ -31,7 +31,7 @@ import java.util.List;
import java.util.Map;
import java.util.Properties;
import javax.annotation.Nullable;
-import org.sonarsource.scanner.api.Utils;
+import org.sonarsource.scanner.lib.EnvironmentConfig;
class Conf {
private static final String SCANNER_HOME = "scanner.home";
@@ -79,8 +79,8 @@ class Conf {
return resolver.resolve();
}
- private Properties loadEnvironmentProperties() {
- return Utils.loadEnvironmentProperties(env);
+ private Map<String, String> loadEnvironmentProperties() {
+ return EnvironmentConfig.load(logger.getLogOutputAdapter());
}
private Properties loadGlobalProperties() {
diff --git a/src/main/java/org/sonarsource/scanner/cli/Logs.java b/src/main/java/org/sonarsource/scanner/cli/Logs.java
index 038e78f..f8b4ce3 100644
--- a/src/main/java/org/sonarsource/scanner/cli/Logs.java
+++ b/src/main/java/org/sonarsource/scanner/cli/Logs.java
@@ -22,6 +22,7 @@ package org.sonarsource.scanner.cli;
import java.io.PrintStream;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
+import org.sonarsource.scanner.lib.LogOutput;
public class Logs {
private DateTimeFormatter timeFormatter;
@@ -77,4 +78,37 @@ public class Logs {
stream.println(msg);
}
}
+
+ /**
+ * Adapter for the scanner library.
+ */
+ public LogOutput getLogOutputAdapter() {
+ return new LogOutputAdapter(this);
+ }
+
+ static class LogOutputAdapter implements LogOutput {
+ private final Logs logs;
+
+ public LogOutputAdapter(Logs logs) {
+ this.logs = logs;
+ }
+
+ @Override
+ public void log(String formattedMessage, Level level) {
+ switch (level) {
+ case TRACE, DEBUG:
+ logs.debug(formattedMessage);
+ break;
+ case ERROR:
+ logs.error(formattedMessage);
+ break;
+ case WARN:
+ logs.warn(formattedMessage);
+ break;
+ case INFO:
+ default:
+ logs.info(formattedMessage);
+ }
+ }
+ }
}
diff --git a/src/main/java/org/sonarsource/scanner/cli/Main.java b/src/main/java/org/sonarsource/scanner/cli/Main.java
index ff7f96f..787cc80 100644
--- a/src/main/java/org/sonarsource/scanner/cli/Main.java
+++ b/src/main/java/org/sonarsource/scanner/cli/Main.java
@@ -19,11 +19,11 @@
*/
package org.sonarsource.scanner.cli;
-import java.util.Locale;
import java.util.Map;
import java.util.Properties;
-import org.sonarsource.scanner.api.EmbeddedScanner;
-import org.sonarsource.scanner.api.ScanProperties;
+import org.sonarsource.scanner.lib.ScanProperties;
+import org.sonarsource.scanner.lib.ScannerEngineBootstrapper;
+import org.sonarsource.scanner.lib.ScannerEngineFacade;
/**
* Arguments :
@@ -42,15 +42,15 @@ public class Main {
private final Exit exit;
private final Cli cli;
private final Conf conf;
- private EmbeddedScanner embeddedScanner;
- private final ScannerFactory runnerFactory;
+ private ScannerEngineBootstrapper scannerEngineBootstrapper;
+ private final ScannerEngineBootstrapperFactory bootstrapperFactory;
private final Logs logger;
- Main(Exit exit, Cli cli, Conf conf, ScannerFactory runnerFactory, Logs logger) {
+ Main(Exit exit, Cli cli, Conf conf, ScannerEngineBootstrapperFactory bootstrapperFactory, Logs logger) {
this.exit = exit;
this.cli = cli;
this.conf = conf;
- this.runnerFactory = runnerFactory;
+ this.bootstrapperFactory = bootstrapperFactory;
this.logger = logger;
}
@@ -58,11 +58,11 @@ public class Main {
Logs logs = new Logs(System.out, System.err);
Exit exit = new Exit();
Cli cli = new Cli(exit, logs).parse(args);
- Main main = new Main(exit, cli, new Conf(cli, logs, System.getenv()), new ScannerFactory(logs), logs);
- main.execute();
+ Main main = new Main(exit, cli, new Conf(cli, logs, System.getenv()), new ScannerEngineBootstrapperFactory(logs), logs);
+ main.analyze();
}
- void execute() {
+ void analyze() {
Stats stats = new Stats(logger).start();
int status = Exit.INTERNAL_ERROR;
@@ -71,15 +71,12 @@ public class Main {
checkSkip(p);
configureLogging(p);
init(p);
- embeddedScanner.start();
- if (isSonarCloud(p)) {
- logger.info("Analyzing on SonarCloud");
- } else {
- String serverVersion = embeddedScanner.serverVersion();
- logger.info(String.format("Analyzing on SonarQube server %s", serverVersion));
+ try (var engine = scannerEngineBootstrapper.bootstrap()) {
+ logServerType(engine);
+ engine.analyze((Map) p);
+ displayExecutionResult(stats, "SUCCESS");
+ status = Exit.SUCCESS;
}
- execute(stats, p);
- status = Exit.SUCCESS;
} catch (Throwable e) {
displayExecutionResult(stats, "FAILURE");
showError("Error during SonarScanner execution", e, cli.isDebugEnabled());
@@ -89,13 +86,13 @@ public class Main {
}
}
- static boolean isSonarCloud(Properties props) {
- String hostUrl = props.getProperty(Conf.PROPERTY_SONAR_HOST_URL);
- if (hostUrl != null) {
- return hostUrl.toLowerCase(Locale.ENGLISH).contains("sonarcloud");
+ private void logServerType(ScannerEngineFacade engine) {
+ if (engine.isSonarCloud()) {
+ logger.info("Analyzing on SonarCloud");
+ } else {
+ String serverVersion = engine.getServerVersion();
+ logger.info(String.format("Analyzing on SonarQube server %s", serverVersion));
}
-
- return false;
}
private void checkSkip(Properties properties) {
@@ -111,7 +108,7 @@ public class Main {
exit.exit(Exit.SUCCESS);
}
- embeddedScanner = runnerFactory.create(p, cli.getInvokedFrom());
+ scannerEngineBootstrapper = bootstrapperFactory.create(p, cli.getInvokedFrom());
}
private void configureLogging(Properties props) {
@@ -122,11 +119,6 @@ public class Main {
}
}
- private void execute(Stats stats, Properties p) {
- embeddedScanner.execute((Map) p);
- displayExecutionResult(stats, "SUCCESS");
- }
-
private void displayExecutionResult(Stats stats, String resultMsg) {
logger.info(SEPARATOR);
logger.info("EXECUTION " + resultMsg);
diff --git a/src/main/java/org/sonarsource/scanner/cli/ScannerFactory.java b/src/main/java/org/sonarsource/scanner/cli/ScannerEngineBootstrapperFactory.java
index d27165e..b67254c 100644
--- a/src/main/java/org/sonarsource/scanner/cli/ScannerFactory.java
+++ b/src/main/java/org/sonarsource/scanner/cli/ScannerEngineBootstrapperFactory.java
@@ -21,47 +21,31 @@ package org.sonarsource.scanner.cli;
import java.util.Map;
import java.util.Properties;
-import org.sonarsource.scanner.api.EmbeddedScanner;
-import org.sonarsource.scanner.api.LogOutput;
+import org.sonarsource.scanner.lib.ScannerEngineBootstrapper;
-class ScannerFactory {
+class ScannerEngineBootstrapperFactory {
private final Logs logger;
- public ScannerFactory(Logs logger) {
+ public ScannerEngineBootstrapperFactory(Logs logger) {
this.logger = logger;
}
- EmbeddedScanner create(Properties props, String isInvokedFrom) {
+ ScannerEngineBootstrapper create(Properties props, String isInvokedFrom) {
String appName = "ScannerCLI";
String appVersion = ScannerVersion.version();
- if (!isInvokedFrom.equals("") && isInvokedFrom.contains("/")) {
+ if (isInvokedFrom.contains("/")) {
appName = isInvokedFrom.split("/")[0];
appVersion = isInvokedFrom.split("/")[1];
}
- return EmbeddedScanner.create(appName, appVersion, new DefaultLogOutput())
- .addGlobalProperties((Map) props);
+ return newScannerEngineBootstrapper(appName, appVersion)
+ .addBootstrapProperties((Map) props);
}
- class DefaultLogOutput implements LogOutput {
- @Override
- public void log(String formattedMessage, Level level) {
- switch (level) {
- case TRACE:
- case DEBUG:
- logger.debug(formattedMessage);
- break;
- case ERROR:
- logger.error(formattedMessage);
- break;
- case WARN:
- logger.warn(formattedMessage);
- break;
- case INFO:
- default:
- logger.info(formattedMessage);
- }
- }
+ ScannerEngineBootstrapper newScannerEngineBootstrapper(String appName, String appVersion) {
+ return new ScannerEngineBootstrapper(appName, appVersion, logger.getLogOutputAdapter());
}
+
+
}