Skip to content

Commit

Permalink
Add support for legacy config directory on macOS
Browse files Browse the repository at this point in the history
See discussion in dirs-dev/directories-jvm#34
  • Loading branch information
alexarchambault committed Jul 1, 2020
1 parent 1ad09b3 commit 8059cfb
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 29 deletions.
2 changes: 1 addition & 1 deletion doc/docs/other-credentials.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Only `--credentials` and `--credential-file` are considered then.

Credentials can be passed via property files too. By default,
`~/.config/coursier/credentials.properties` is read
(`~/Library/Preferences/Coursier/credentials.properties` on OS X).
(`~/Library/Application Support/Coursier/credentials.properties` on OS X).

### Format

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,20 @@ object CacheDefaults {
def credentials: Seq[Credentials] =
if (credentialPropOpt.isEmpty) {
// Warn if those files have group and others read permissions?
val configDir = coursier.paths.CoursierPaths.configDirectory()
val mainCredentialsFile = new File(configDir, "credentials.properties")
val configDirs = coursier.paths.CoursierPaths.configDirectories().toSeq
val mainCredentialsFiles = configDirs.map(configDir => new File(configDir, "credentials.properties"))
val otherFiles = {
// delay listing files until credentials are really needed?
val dir = new File(configDir, "credentials")
val files = dir.listFiles(new FilenameFilter {
val dirs = configDirs.map(configDir => new File(configDir, "credentials"))
val files = dirs.flatMap(_.listFiles(new FilenameFilter {
def accept(dir: File, name: String): Boolean =
!name.startsWith(".") && name.endsWith(".properties")
})
}))
Option(files).toSeq.flatten.map { f =>
FileCredentials(f.getAbsolutePath, optional = true) // non optional?
}
}
FileCredentials(mainCredentialsFile.getAbsolutePath, optional = true) +: otherFiles
mainCredentialsFiles.map(f => FileCredentials(f.getAbsolutePath, optional = true)) ++ otherFiles
} else
credentialPropOpt
.filter(isPropFile)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object Install extends CaseApp[InstallOptions] {

// TODO Move to install module

val configDir = coursier.paths.CoursierPaths.configDirectory()
val configDir = coursier.paths.CoursierPaths.defaultConfigDirectory()
val channelDir = new File(configDir, "channels")

// FIXME May not be fine with concurrency (two process doing this in parallel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ object SharedChannelParams {

val fileChannelsV =
if (options.fileChannels) {
val configDir = coursier.paths.CoursierPaths.configDirectory()
val channelDir = new File(configDir, "channels")
val files = Option(channelDir.listFiles())
.getOrElse(Array.empty[File])
val configDirs = coursier.paths.CoursierPaths.configDirectories().toSeq
val files = configDirs
.flatMap { configDir =>
val channelDir = new File(configDir, "channels")
Option(channelDir.listFiles()).getOrElse(Array.empty[File])
}
.filter(f => !f.getName.startsWith("."))
val rawChannels = files.toList.flatMap { f =>
val b = Files.readAllBytes(f.toPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import coursier.parse.RepositoryParser
abstract class PlatformResolve {

def defaultMirrorConfFiles: Seq[MirrorConfFile] = {
val files = Seq(coursier.paths.Mirror.defaultConfigFile()) ++
val files = coursier.paths.Mirror.defaultConfigFiles().toSeq ++
Option(coursier.paths.Mirror.extraConfigFile()).toSeq
files.map { f =>
// Warn if f has group and others read permissions?
Expand Down
39 changes: 29 additions & 10 deletions modules/paths/src/main/java/coursier/paths/CoursierPaths.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private CoursierPaths() {
private static volatile File jvmCacheDirectory0 = null;

private static final Object configDirectoryLock = new Object();
private static volatile File configDirectory0 = null;
private static volatile File[] configDirectories0 = null;

private static final Object dataLocalDirectoryLock = new Object();
private static volatile File dataLocalDirectory0 = null;
Expand Down Expand Up @@ -94,28 +94,47 @@ private static ProjectDirectories coursierDirectories() throws IOException {
return coursierDirectories0;
}

private static String computeConfigDirectory() throws IOException {
private static File[] computeConfigDirectories() throws IOException {
String path = System.getenv("COURSIER_CONFIG_DIR");

if (path == null)
path = System.getProperty("coursier.config-dir");

if (path != null)
return path;

return coursierDirectories().configDir;
return new File[] { new File(path).getAbsoluteFile() };

String configDir = coursierDirectories().configDir;
String preferenceDir = coursierDirectories().preferenceDir;
if (configDir.equals(preferenceDir))
return new File[] {
new File(configDir).getAbsoluteFile(),
};
else
return new File[] {
new File(configDir).getAbsoluteFile(),
new File(preferenceDir).getAbsoluteFile()
};
}

public static File configDirectory() throws IOException {
public static File[] configDirectories() throws IOException {

if (configDirectory0 == null)
if (configDirectories0 == null)
synchronized (configDirectoryLock) {
if (configDirectory0 == null) {
configDirectory0 = new File(computeConfigDirectory()).getAbsoluteFile();
if (configDirectories0 == null) {
configDirectories0 = computeConfigDirectories();
}
}

return configDirectory0;
return configDirectories0.clone();
}

@Deprecated
public static File configDirectory() throws IOException {
return configDirectories()[0];
}

public static File defaultConfigDirectory() throws IOException {
return configDirectories()[0];
}

private static String computeDataLocalDirectory() throws IOException {
Expand Down
21 changes: 15 additions & 6 deletions modules/paths/src/main/java/coursier/paths/Mirror.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,30 @@ public static Mirror of(List<String> from, String to, String type) {
return new Mirror(from, to, type);
}

public static File defaultConfigFile() throws IOException {
public static File[] defaultConfigFiles() throws IOException {

String fromEnv = System.getenv("COURSIER_MIRRORS");
if (fromEnv != null && !fromEnv.isEmpty()) {
return new File(fromEnv);
return new File[] { new File(fromEnv) };
}

String fromProps = System.getProperty("coursier.mirrors");
if (fromProps != null && !fromProps.isEmpty()) {
return new File(fromProps);
return new File[] { new File(fromProps) };
}

File configDir = coursier.paths.CoursierPaths.configDirectory();
File propFile = new File(configDir, "mirror.properties");
return propFile;
File[] configDirs = coursier.paths.CoursierPaths.configDirectories();
ArrayList<File> configFiles = new ArrayList<>();
for (File configDir : configDirs) {
File propFile = new File(configDir, "mirror.properties");
configFiles.add(propFile);
}
return configFiles.toArray(new File[configFiles.size()]);
}

@Deprecated
public static File defaultConfigFile() throws IOException {
return defaultConfigFiles()[0];
}

public static File extraConfigFile() throws IOException {
Expand Down

0 comments on commit 8059cfb

Please sign in to comment.