@Override public String getBranchNameFor(Environment environment) { String[] tokens = environment.getName().split("/"); String branchName = tokens[0].trim(); if (branchName.isEmpty()) { branchName = "master"; } return branchName; } }
@Override public Path getPathFor(Environment environment) { String[] tokens = environment.getName().split("/"); return FileSystems.getDefault().getPath("", Arrays.copyOfRange(tokens, 1, tokens.length)); } }
/** * Format the provided {@link Environment} to ensure it ends with an underscore * * @param environment The provided {@link Environment} context * @return The formatted {@link String} of the {@link Environment} context */ private static String formatEnvironmentContext(Environment environment) { String environmentName = environment.getName(); if (environmentName == null || environmentName.isEmpty()) { return ""; } else { return environmentName.endsWith("_") ? environmentName : environmentName + "_"; } } }
/** * Get configuration set for a given {@code environment} from the cache. For cache to be seeded * you have to call the {@link #reload(Environment)} method before calling this method. Otherwise * the method will throw {@link MissingEnvironmentException}. * * @param environment environment to use * @return configuration set for {@code environment} * @throws MissingEnvironmentException when there's no config for the given environment in the cache */ @Override public Properties getConfiguration(Environment environment) { if (cachedConfigurationPerEnvironment.containsKey(environment.getName())) { return cachedConfigurationPerEnvironment.get(environment.getName()); } else { throw new MissingEnvironmentException(environment.getName()); } }
/** * Reload configuration set for a given {@code environment} from this source in a form of {@link Properties}. * After reload completes the configuration can be accesses via {@link #getConfiguration(Environment)} method. * * @param environment environment to reload * @throws MissingEnvironmentException when requested environment couldn't be found * @throws IllegalStateException when unable to fetch configuration */ public void reload(Environment environment) { Properties configuration = underlyingSource.getConfiguration(environment); cachedConfigurationPerEnvironment.put(environment.getName(), configuration); } }
@Override public Properties getConfiguration(Environment environment) { LOG.trace("Requesting configuration for environment: " + environment.getName()); if (!initialized) { throw new IllegalStateException("Configuration source has to be successfully initialized before you request configuration."); } reload(); Properties properties = new Properties(); String path = environment.getName(); if (path.startsWith("/")) { path = path.substring(1); } if (path.length() > 0 && !path.endsWith("/")) { path = path + "/"; } for (Map.Entry<String, String> entry : consulValues.entrySet()) { if (entry.getKey().startsWith(path)) { properties.put(entry.getKey().substring(path.length()).replace("/", "."), entry.getValue()); } } return properties; }
/** * Get configuration set for a given {@code environment} from this source in a form of {@link Properties}. * The configuration set is a result of the first {@link ConfigurationSource#getConfiguration(Environment)} * call to underlying sources that succeeds. Sources are called in a provided order. * * @param environment environment to use * @return configuration set for {@code environment} from the first source that works * @throws MissingEnvironmentException when requested environment couldn't be found in any of the underlying source * @throws IllegalStateException when unable to fetch configuration from any of the underlying sources */ @Override public Properties getConfiguration(Environment environment) { boolean allMissEnvironment = true; for (ConfigurationSource source : sources) { try { return source.getConfiguration(environment); } catch (MissingEnvironmentException e) { // NOP } catch (IllegalStateException e) { allMissEnvironment = false; } } if (allMissEnvironment) { throw new MissingEnvironmentException(environment.getName()); } throw new IllegalStateException(); }
@Override public Properties getConfiguration(Environment environment) { if (!initialized) { throw new IllegalStateException("Configuration source has to be successfully initialized before you request configuration."); } reload(); try { checkoutToBranch(branchResolver.getBranchNameFor(environment)); } catch (GitAPIException e) { throw new MissingEnvironmentException(environment.getName(), e); } Properties properties = new Properties(); List<Path> paths = new ArrayList<>(); for (Path path : configFilesProvider.getConfigFiles()) { paths.add(clonedRepoPath.resolve(pathResolver.getPathFor(environment)).resolve(path)); } for (Path path : paths) { try (InputStream input = new FileInputStream(path.toFile())) { PropertiesProvider provider = propertiesProviderSelector.getProvider(path.getFileName().toString()); properties.putAll(provider.getProperties(input)); } catch (IOException e) { throw new IllegalStateException("Unable to load configuration from " + path.toString() + " file", e); } } return properties; }
@Test void resolvesEmptyStringToEmptyPath() { when(environment.getName()).thenReturn("us-west-1/"); assertThat(pathResolver.getPathFor(environment).toString()).isEqualTo(""); }
@Test void discardsFirstToken() { when(environment.getName()).thenReturn("us-west-1/local/path"); assertThat(pathResolver.getPathFor(environment).toString()).isEqualTo("local/path"); }
@Test void ignoresMissingFirstToken() { when(environment.getName()).thenReturn("/local/path"); assertThat(pathResolver.getPathFor(environment).toString()).isEqualTo("local/path"); }
@Test void treatsMissingPathAsEmptyPath() { when(environment.getName()).thenReturn("us-west-1/"); assertThat(pathResolver.getPathFor(environment).toString()).isEqualTo(""); } }
@Test void resolvesEmptyStringToMaster() { when(environment.getName()).thenReturn(""); assertThat(branchResolver.getBranchNameFor(environment)).isEqualTo("master"); }
@Test void resolvesWhitespacesToMaster() { when(environment.getName()).thenReturn(" "); assertThat(branchResolver.getBranchNameFor(environment)).isEqualTo("master"); }
@Test void supportsSingleToken() { when(environment.getName()).thenReturn("us-west-1"); assertThat(branchResolver.getBranchNameFor(environment)).isEqualTo("us-west-1"); }
@Test void usesFirstTokenAsBranchName() { when(environment.getName()).thenReturn("us-west-1/local/path"); assertThat(branchResolver.getBranchNameFor(environment)).isEqualTo("us-west-1"); } }