// Path: heroku-deploy/src/main/java/com/heroku/sdk/deploy/util/HerokuCli.java
// public class HerokuCli {
// 
//     public static Optional<String> runAuthToken(Path workingDirectory) throws IOException {
//         return Optional.ofNullable(runRaw(workingDirectory,"auth:token").get(0));
//     }
// 
//     private static List<String> runRaw(Path workingDirectory, String... command) throws IOException {
//         List<String> fullCommand =  new ArrayList<>(Arrays.asList(command));
//         fullCommand.add(0, "heroku");
// 
//         ProcessBuilder processBuilder = new ProcessBuilder(fullCommand);
//         processBuilder.directory(workingDirectory.toFile());
//         Process process = processBuilder.start();
// 
//         try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
//             return bufferedReader.lines().collect(Collectors.toList());
//         }
//     }
// }


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;
package com.heroku.sdk.deploy.lib.resolver;



public class ApiKeyResolver {

    public static Optional<String> resolve(Path directory) throws IOException {
        Optional<String> environmentApiKey = readApiKeyFromEnvironment();
        if (environmentApiKey.isPresent()) {
            return environmentApiKey;
        }

        Optional<String> netRcPassword = ApiKeyResolver.readPasswordFromNetRc();
        if (netRcPassword.isPresent()) {
            return netRcPassword;
        }

// Path: heroku-deploy/src/main/java/com/heroku/sdk/deploy/util/HerokuCli.java
// public class HerokuCli {
// 
//     public static Optional<String> runAuthToken(Path workingDirectory) throws IOException {
//         return Optional.ofNullable(runRaw(workingDirectory,"auth:token").get(0));
//     }
// 
//     private static List<String> runRaw(Path workingDirectory, String... command) throws IOException {
//         List<String> fullCommand =  new ArrayList<>(Arrays.asList(command));
//         fullCommand.add(0, "heroku");
// 
//         ProcessBuilder processBuilder = new ProcessBuilder(fullCommand);
//         processBuilder.directory(workingDirectory.toFile());
//         Process process = processBuilder.start();
// 
//         try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
//             return bufferedReader.lines().collect(Collectors.toList());
//         }
//     }
// }


// Path: heroku-deploy/src/main/java/com/heroku/sdk/deploy/lib/resolver/ApiKeyResolver.java
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;

package com.heroku.sdk.deploy.lib.resolver;



public class ApiKeyResolver {

    public static Optional<String> resolve(Path directory) throws IOException {
        Optional<String> environmentApiKey = readApiKeyFromEnvironment();
        if (environmentApiKey.isPresent()) {
            return environmentApiKey;
        }

        Optional<String> netRcPassword = ApiKeyResolver.readPasswordFromNetRc();
        if (netRcPassword.isPresent()) {
            return netRcPassword;
        }

        return HerokuCli.runAuthToken(directory);