summaryrefslogtreecommitdiff
path: root/core/src/main/java/org/elasticsearch/common/cli/Terminal.java
blob: 82898b3e45738a9234098276a60375407779e178 (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch licenses this file to you under
 * the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package org.elasticsearch.common.cli;

import org.apache.commons.cli.CommandLine;
import org.elasticsearch.common.SuppressForbidden;

import java.io.BufferedReader;
import java.io.Console;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Locale;

/**
*
*/
@SuppressForbidden(reason = "System#out")
public abstract class Terminal {

    public static final String DEBUG_SYSTEM_PROPERTY = "es.cli.debug";

    public static final Terminal DEFAULT = ConsoleTerminal.supported() ? new ConsoleTerminal() : new SystemTerminal();

    public static enum Verbosity {
        SILENT(0), NORMAL(1), VERBOSE(2);

        private final int level;

        private Verbosity(int level) {
            this.level = level;
        }

        public boolean enabled(Verbosity verbosity) {
            return level >= verbosity.level;
        }

        public static Verbosity resolve(CommandLine cli) {
            if (cli.hasOption("s")) {
                return SILENT;
            }
            if (cli.hasOption("v")) {
                return VERBOSE;
            }
            return NORMAL;
        }
    }

    private Verbosity verbosity = Verbosity.NORMAL;
    private final boolean isDebugEnabled;

    public Terminal() {
        this(Verbosity.NORMAL);
    }

    public Terminal(Verbosity verbosity) {
        this.verbosity = verbosity;
        this.isDebugEnabled = "true".equals(System.getProperty(DEBUG_SYSTEM_PROPERTY, "false"));
    }

    public void verbosity(Verbosity verbosity) {
        this.verbosity = verbosity;
    }

    public Verbosity verbosity() {
        return verbosity;
    }

    public abstract String readText(String text, Object... args);

    public abstract char[] readSecret(String text, Object... args);

    protected abstract void printStackTrace(Throwable t);

    public void println() {
        println(Verbosity.NORMAL);
    }

    public void println(String msg, Object... args) {
        println(Verbosity.NORMAL, msg, args);
    }

    public void print(String msg, Object... args) {
        print(Verbosity.NORMAL, msg, args);
    }

    public void println(Verbosity verbosity) {
        println(verbosity, "");
    }

    public void println(Verbosity verbosity, String msg, Object... args) {
        print(verbosity, msg + System.lineSeparator(), args);
    }

    public void print(Verbosity verbosity, String msg, Object... args) {
        if (this.verbosity.enabled(verbosity)) {
            doPrint(msg, args);
        }
    }

    public void printError(String msg, Object... args) {
        println(Verbosity.SILENT, "ERROR: " + msg, args);
    }

    public void printError(Throwable t) {
        printError("%s", t.toString());
        if (isDebugEnabled) {
            printStackTrace(t);
        }
    }

    public void printWarn(String msg, Object... args) {
        println(Verbosity.SILENT, "WARN: " + msg, args);
    }

    protected abstract void doPrint(String msg, Object... args);

    public abstract PrintWriter writer();

    private static class ConsoleTerminal extends Terminal {

        final Console console = System.console();

        static boolean supported() {
            return System.console() != null;
        }

        @Override
        public void doPrint(String msg, Object... args) {
            console.printf(msg, args);
            console.flush();
        }

        @Override
        public String readText(String text, Object... args) {
            return console.readLine(text, args);
        }

        @Override
        public char[] readSecret(String text, Object... args) {
            return console.readPassword(text, args);
        }

        @Override
        public PrintWriter writer() {
            return console.writer();
        }

        @Override
        public void printStackTrace(Throwable t) {
            t.printStackTrace(console.writer());
        }
    }

    @SuppressForbidden(reason = "System#out")
    private static class SystemTerminal extends Terminal {

        private final PrintWriter printWriter = new PrintWriter(System.out);

        @Override
        public void doPrint(String msg, Object... args) {
            System.out.print(String.format(Locale.ROOT, msg, args));
        }

        @Override
        public String readText(String text, Object... args) {
            print(text, args);
            BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
            try {
                return reader.readLine();
            } catch (IOException ioe) {
                throw new RuntimeException(ioe);
            }
        }

        @Override
        public char[] readSecret(String text, Object... args) {
            return readText(text, args).toCharArray();
        }

        @Override
        public void printStackTrace(Throwable t) {
            t.printStackTrace(printWriter);
        }

        @Override
        public PrintWriter writer() {
            return printWriter;
        }
    }
}