com.heroku.sdk.deploy.lib.resolver.ApiKeyResolver Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of heroku-deploy Show documentation
Show all versions of heroku-deploy Show documentation
Library for deploying Java applications to Heroku
package com.heroku.sdk.deploy.lib.resolver;
import com.heroku.sdk.deploy.util.HerokuCli;
import org.eclipse.jgit.transport.NetRC;
import java.io.IOException;
import java.nio.file.Path;
import java.util.Optional;
public class ApiKeyResolver {
public static Optional resolve(Path directory) throws IOException {
Optional environmentApiKey = readApiKeyFromEnvironment();
if (environmentApiKey.isPresent()) {
return environmentApiKey;
}
Optional netRcPassword = ApiKeyResolver.readPasswordFromNetRc();
if (netRcPassword.isPresent()) {
return netRcPassword;
}
return HerokuCli.runAuthToken(directory);
}
private static Optional readPasswordFromNetRc() {
// Reads, depending on operating system, the users default netrc file
NetRC netrc = new NetRC();
return Optional
.ofNullable(netrc.getEntry("api.heroku.com"))
.flatMap(entry -> Optional.ofNullable(entry.password))
.map(String::valueOf);
}
private static Optional readApiKeyFromEnvironment() {
return Optional
.ofNullable(System.getenv("HEROKU_API_KEY"))
.filter(apiKey -> !apiKey.trim().isEmpty());
}
}