All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.heroku.sdk.deploy.lib.resolver.ApiKeyResolver Maven / Gradle / Ivy

There is a newer version: 3.0.7
Show newest version
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());
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy