Search in subdirectories of main config repo

... in addition to top level. That way users can
organize their config files, e.g. by subsystem.
Subdirectories have *higher* precedence than the
top level. Users configure which subdirectories to
search through spring.cloud.config.server.git.searchPaths
(array or String, so csv in config file, default empty).

Fixes gh-36
This commit is contained in:
Dave Syer
2014-11-13 18:15:31 +00:00
parent 776ec68280
commit 4669102be6
23 changed files with 159 additions and 5 deletions

View File

@@ -28,10 +28,14 @@ public class ConfigServerTestUtils {
public static String prepareLocalRepo() throws IOException {
return prepareLocalRepo("target/test-classes", "config-repo", "target/config");
}
public static String prepareLocalRepo(String buildDir, String repoPath, String checkoutDir) throws IOException {
public static String prepareLocalRepo(String repoPath) throws IOException {
return prepareLocalRepo("target/test-classes", repoPath, "target/config");
}
public static String prepareLocalRepo(String buildDir, String repoPath,
String checkoutDir) throws IOException {
if (!repoPath.startsWith("/")) {
repoPath = "/" + repoPath;
}

View File

@@ -21,6 +21,7 @@ import static org.springframework.util.StringUtils.hasText;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
@@ -72,6 +73,8 @@ public class JGitEnvironmentRepository implements EnvironmentRepository {
private boolean initialized;
private String[] searchPaths = new String[0];
public JGitEnvironmentRepository(ConfigurableEnvironment environment) {
this.environment = environment;
try {
@@ -112,6 +115,14 @@ public class JGitEnvironmentRepository implements EnvironmentRepository {
public File getBasedir() {
return basedir;
}
public void setSearchPaths(String... searchPaths) {
this.searchPaths = searchPaths;
}
public String[] getSearchPaths() {
return searchPaths;
}
public String getUsername() {
return username;
@@ -148,11 +159,22 @@ public class JGitEnvironmentRepository implements EnvironmentRepository {
if (shouldPull(git, ref)) {
pull(git, label, ref);
}
String search = basedir.toURI().toString();
environment.setSearchLocations(search);
environment.setSearchLocations(getSearchLocations(basedir));
return clean(environment.findOne(application, profile, label));
}
private String[] getSearchLocations(File dir) {
List<String> locations = new ArrayList<String>();
locations.add(dir.toURI().toString());
for (String path : searchPaths) {
File file = new File(basedir, path);
if (file.isDirectory()) {
locations.add(file.toURI().toString());
}
}
return locations.toArray(new String[0]);
}
private Ref checkout(Git git, String label) throws GitAPIException {
CheckoutCommand checkout = git.checkout();
if (shouldTrack(git, label)) {

View File

@@ -0,0 +1,88 @@
/*
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.config.server;
import static org.junit.Assert.assertEquals;
import java.io.File;
import java.io.IOException;
import org.eclipse.jgit.util.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.config.Environment;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* @author Dave Syer
*
*/
public class JGitEnvironmentRepositoryIntegrationTests {
private ConfigurableApplicationContext context;
private File basedir = new File("target/config");
@Before
public void init() throws Exception {
if (basedir.exists()) {
FileUtils.delete(basedir, FileUtils.RECURSIVE);
}
}
@After
public void close() {
if (context != null) {
context.close();
}
}
@Test
public void vanilla() throws IOException {
String uri = ConfigServerTestUtils.prepareLocalRepo();
context = new SpringApplicationBuilder(TestConfiguration.class).web(false)
.properties("spring.cloud.config.server.git.uri=" + uri).run();
EnvironmentRepository repository = context.getBean(EnvironmentRepository.class);
repository.findOne("bar", "staging", "master");
Environment environment = repository.findOne("bar", "staging", "master");
assertEquals(2, environment.getPropertySources().size());
}
@Test
public void nested() throws IOException {
String uri = ConfigServerTestUtils.prepareLocalRepo("another-config-repo");
context = new SpringApplicationBuilder(TestConfiguration.class)
.web(false)
.properties("spring.cloud.config.server.git.uri=" + uri,
"spring.cloud.config.server.git.searchPaths=sub").run();
EnvironmentRepository repository = context.getBean(EnvironmentRepository.class);
repository.findOne("bar", "staging", "master");
Environment environment = repository.findOne("bar", "staging", "master");
assertEquals(2, environment.getPropertySources().size());
}
@Configuration
@Import({ PropertyPlaceholderAutoConfiguration.class, ConfigServerConfiguration.class })
protected static class TestConfiguration {
}
}

View File

@@ -20,6 +20,7 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import org.eclipse.jgit.util.FileUtils;
import org.junit.Before;
@@ -57,6 +58,18 @@ public class JGitEnvironmentRepositoryTests {
.getPropertySources().get(0).getName());
}
@Test
public void nested() throws IOException {
String uri = ConfigServerTestUtils.prepareLocalRepo("another-config-repo");
repository.setUri(uri);
repository.setSearchPaths(new String[] {"sub"});
repository.findOne("bar", "staging", "master");
Environment environment = repository.findOne("bar", "staging", "master");
assertEquals(2, environment.getPropertySources().size());
assertEquals(repository.getUri() + "/sub/application.yml", environment
.getPropertySources().get(0).getName());
}
@Test
public void branch() {
repository.setBasedir(basedir);

View File

@@ -0,0 +1 @@
Move application.yml to subdir

View File

@@ -0,0 +1,2 @@
7df4a26d5437d9d4090cd5809967f870444cde8f not-for-merge branch 'raw' of dsyer@localhost:/home/dsyer/dev/platform/config/spring-platform-config-server/target/test-classes/config-repo
7df4a26d5437d9d4090cd5809967f870444cde8f not-for-merge tag 'foo' of dsyer@localhost:/home/dsyer/dev/platform/config/spring-platform-config-server/target/test-classes/config-repo

View File

@@ -0,0 +1 @@
ref: refs/heads/master

View File

@@ -0,0 +1,5 @@
[core]
repositoryformatversion = 0
filemode = true
logallrefupdates = true
[branch "raw"]

View File

@@ -0,0 +1,3 @@
1 1
7df4a26d5437d9d4090cd5809967f870444cde8f 9f01fb972bc9617e4ea59f5c8ee3ceb5ff515cd0 9f01fb972bc9617e4ea59f5c8ee3ceb5ff515cd0
1

View File

@@ -0,0 +1,3 @@
0000000000000000000000000000000000000000 7df4a26d5437d9d4090cd5809967f870444cde8f Dave Syer <dsyer@gopivotal.com> 1406860717 -0700
7df4a26d5437d9d4090cd5809967f870444cde8f 9f01fb972bc9617e4ea59f5c8ee3ceb5ff515cd0 Dave Syer <dsyer@pivotal.io> 1415902155 +0000 checkout: moving from raw to master
9f01fb972bc9617e4ea59f5c8ee3ceb5ff515cd0 c4bd92016dc14b9fe376d9c3f7af1c9d22d44ee4 Dave Syer <dsyer@pivotal.io> 1415902338 +0000 commit: Move application.yml to subdir

View File

@@ -0,0 +1,2 @@
0000000000000000000000000000000000000000 9f01fb972bc9617e4ea59f5c8ee3ceb5ff515cd0 Dave Syer <dsyer@gopivotal.com> 1406860776 -0700 branch: Created from 9f01fb972bc9617e4ea59f5c8ee3ceb5ff515cd0
9f01fb972bc9617e4ea59f5c8ee3ceb5ff515cd0 c4bd92016dc14b9fe376d9c3f7af1c9d22d44ee4 Dave Syer <dsyer@pivotal.io> 1415902338 +0000 commit: Move application.yml to subdir

View File

@@ -0,0 +1 @@
0000000000000000000000000000000000000000 7df4a26d5437d9d4090cd5809967f870444cde8f Dave Syer <dsyer@gopivotal.com> 1406860717 -0700

View File

@@ -0,0 +1 @@
x+)JMU01f040031QH,(<28><>LN,<2C><><EFBFBD>ӫ<EFBFBD><D3AB>ah<61>xH<78>S<EFBFBD>O<EFBFBD>}jI<6A><49><EFBFBD><EFBFBD><EFBFBD><EFBFBD><07> <01>x<12>

View File

@@ -0,0 +1,2 @@
x<01><>1
1E<>s<EFBFBD><73><05>$<24><>

View File

@@ -0,0 +1 @@
c4bd92016dc14b9fe376d9c3f7af1c9d22d44ee4

View File

@@ -0,0 +1 @@
7df4a26d5437d9d4090cd5809967f870444cde8f

View File

@@ -0,0 +1 @@
7df4a26d5437d9d4090cd5809967f870444cde8f

View File

@@ -0,0 +1,2 @@
info:
foo: bar