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.
/*
* SonarScanner Java Library
* Copyright (C) 2011-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonarsource.scanner.lib;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Utility class to load configuration from environment variables.
*/
public class EnvironmentConfig {
private static final Logger LOG = LoggerFactory.getLogger(EnvironmentConfig.class);
private static final String SONAR_SCANNER_JSON_PARAMS = "SONAR_SCANNER_JSON_PARAMS";
private static final String SONARQUBE_SCANNER_PARAMS = "SONARQUBE_SCANNER_PARAMS";
private static final String GENERIC_ENV_PREFIX = "SONAR_SCANNER_";
private static final String SONAR_HOST_URL_ENV_VAR = "SONAR_HOST_URL";
private static final String SONAR_USER_HOME_ENV_VAR = "SONAR_USER_HOME";
private static final String TOKEN_ENV_VARIABLE = "SONAR_TOKEN";
private EnvironmentConfig() {
// only static methods
}
public static Map load() {
return load(System.getenv());
}
public static Map load(Map env) {
var loadedProps = new HashMap();
Optional.ofNullable(env.get(SONAR_HOST_URL_ENV_VAR)).ifPresent(url -> loadedProps.put(ScannerProperties.HOST_URL, url));
Optional.ofNullable(env.get(SONAR_USER_HOME_ENV_VAR)).ifPresent(path -> loadedProps.put(ScannerProperties.SONAR_USER_HOME, path));
Optional.ofNullable(env.get(TOKEN_ENV_VARIABLE)).ifPresent(path -> loadedProps.put(ScannerProperties.SONAR_TOKEN, path));
env.forEach((key, value) -> {
if (!key.equals(SONAR_SCANNER_JSON_PARAMS) && key.startsWith(GENERIC_ENV_PREFIX)) {
processEnvVariable(key, value, loadedProps);
}
});
var jsonParams = env.get(SONAR_SCANNER_JSON_PARAMS);
var oldJsonParams = env.get(SONARQUBE_SCANNER_PARAMS);
if (jsonParams != null) {
if (oldJsonParams != null && !oldJsonParams.equals(jsonParams)) {
LOG.warn("Ignoring environment variable '{}' because '{}' is set", SONARQUBE_SCANNER_PARAMS, SONAR_SCANNER_JSON_PARAMS);
}
parseJsonPropertiesFromEnv(jsonParams, loadedProps, SONAR_SCANNER_JSON_PARAMS);
} else if (oldJsonParams != null) {
parseJsonPropertiesFromEnv(oldJsonParams, loadedProps, SONARQUBE_SCANNER_PARAMS);
}
return loadedProps;
}
private static void parseJsonPropertiesFromEnv(String jsonParams, Map inputProperties, String envVariableName) {
try {
var jsonProperties = new Gson().