summaryrefslogtreecommitdiffstats
path: root/security-tools/src/main/java/com/yahoo/vespa/security/tool/securityenv/UnixShell.java
blob: 74630b4e1f4aa5c483ed7e2d5fcd54c811282729 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.security.tool.securityenv;

import java.io.PrintStream;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Definition of some unix shell variants and how to export environments variable for those supported.
 * The output format is inspired by ssh-agent's output.
 *
 * @author bjorncs
 */
enum UnixShell {
    BOURNE("bourne", List.of("bash", "sh")) {
        @Override
        void writeOutputVariables(PrintStream out, Map<String, String> outputVariables) {
            outputVariables.forEach((name, value) -> {
                out.print(name);
                out.print("=\"");
                out.print(value); // note: value is assumed to need no escaping
                out.print("\"; export ");
                out.print(name);
                out.println(';');
            });
        }
    },
    CSHELL("cshell", List.of("csh", "fish")) {
        @Override
        void writeOutputVariables(PrintStream out, Map<String, String> outputVariables) {
            outputVariables.forEach((name, value) -> {
                out.print("setenv ");
                out.print(name);
                out.print(" \"");
                out.print(value); // note: value is assumed to need no escaping
                out.println("\";");
            });
        }
    };

    private static final UnixShell DEFAULT = BOURNE;

    private final String configName;
    private final List<String> knownShellBinaries;

    UnixShell(String configName, List<String> knownShellBinaries) {
        this.configName = configName;
        this.knownShellBinaries = knownShellBinaries;
    }

    abstract void writeOutputVariables(PrintStream out, Map<String, String> outputVariables);

    String configName() {
        return configName;
    }

    static UnixShell fromConfigName(String configName) {
        return Arrays.stream(values())
                .filter(shell -> shell.configName.equals(configName))
                .findAny()
                .orElseThrow(() -> new IllegalArgumentException("Unknown shell: " + configName));
    }

    static UnixShell detect(String shellEnvVariable) {
        if (shellEnvVariable == null || shellEnvVariable.isEmpty()) return DEFAULT;
        int lastSlash = shellEnvVariable.lastIndexOf('/');
        String shellName = lastSlash != -1 ? shellEnvVariable.substring(lastSlash + 1) : shellEnvVariable;
        return Arrays.stream(values())
                .filter(shell -> shell.knownShellBinaries.contains(shellName))
                .findAny()
                .orElse(DEFAULT);
    }
}