private void chooseSshKeyPairs()

in plugin/src/main/java/io/fabric8/maven/plugin/mojo/internal/ImportMojo.java [292:336]


    private void chooseSshKeyPairs(Map<String, String> secretData, String host) throws MojoExecutionException {
        String homeDir = System.getProperty("user.home", ".");
        File sshDir = new File(homeDir, ".ssh");
        SortedMap<String, String> keyPairs = new TreeMap<>();
        if (sshDir.isDirectory() && sshDir.exists()) {
            File[] files = sshDir.listFiles();
            if (files != null) {
                for (File file : files) {
                    String publicName = file.getName();
                    if (file.isFile() && publicName.endsWith(".pub")) {
                        String privateName = Strings.stripSuffix(publicName, ".pub");
                        if (new File(sshDir, privateName).isFile()) {
                            keyPairs.put(privateName, publicName);
                        }
                    }
                }
            }
        }

        if (keyPairs.isEmpty()) {
            log.warn("No SSH key pairs could be found in %s to choose from!", sshDir);
            log.warn("You may want to clone the git repository over https:// instead to avoid ssh key pairs?");
        } else {
            if (keyPairs.size() == 0) {
                String privateName = keyPairs.firstKey();
                importSshKeys(secretData, sshDir, privateName, keyPairs.get(privateName));
            } else {
                List<String> privateKeys = new ArrayList<>(keyPairs.keySet());
                String privateKey = null;
                try {
                    privateKey = prompter.prompt("Which public / private key pair do you wish to use for SSH authentication with host: " + host, privateKeys);
                } catch (PrompterException e) {
                    log.warn("Failed to get user input: %s", e);
                }
                if (Strings.isNotBlank(privateKey)) {
                    String publicKey = keyPairs.get(privateKey);
                    if (Strings.isNullOrBlank(publicKey)) {
                        log.warn("Invalid answer: %s when available values are: %s", privateKey, privateKeys);
                    } else {
                        importSshKeys(secretData, sshDir, privateKey, publicKey);
                    }
                }
            }
        }
    }