1919
2020package org .elasticsearch .common .settings ;
2121
22- import java .io .BufferedReader ;
23- import java .io .CharArrayWriter ;
24- import java .io .InputStream ;
25- import java .io .InputStreamReader ;
26- import java .nio .charset .StandardCharsets ;
27- import java .util .Arrays ;
28-
2922import joptsimple .OptionSet ;
3023import joptsimple .OptionSpec ;
3124import org .elasticsearch .cli .ExitCodes ;
3225import org .elasticsearch .cli .Terminal ;
3326import org .elasticsearch .cli .UserException ;
27+ import org .elasticsearch .common .CheckedFunction ;
3428import org .elasticsearch .env .Environment ;
3529
30+ import java .io .BufferedReader ;
31+ import java .io .CharArrayWriter ;
32+ import java .io .Closeable ;
33+ import java .io .IOException ;
34+ import java .io .InputStream ;
35+ import java .io .InputStreamReader ;
36+ import java .nio .charset .StandardCharsets ;
37+ import java .util .Arrays ;
38+ import java .util .List ;
39+
3640/**
3741 * A subcommand for the keystore cli which adds a string setting.
3842 */
@@ -42,11 +46,11 @@ class AddStringKeyStoreCommand extends BaseKeyStoreCommand {
4246 private final OptionSpec <String > arguments ;
4347
4448 AddStringKeyStoreCommand () {
45- super ("Add a string setting to the keystore" , false );
46- this .stdinOption = parser .acceptsAll (Arrays .asList ("x" , "stdin" ), "Read setting value from stdin" );
49+ super ("Add a string settings to the keystore" , false );
50+ this .stdinOption = parser .acceptsAll (Arrays .asList ("x" , "stdin" ), "Read setting values from stdin" );
4751 this .forceOption = parser .acceptsAll (Arrays .asList ("f" , "force" ),
4852 "Overwrite existing setting without prompting, creating keystore if necessary" );
49- this .arguments = parser .nonOptions ("setting name " );
53+ this .arguments = parser .nonOptions ("setting names " );
5054 }
5155
5256 // pkg private so tests can manipulate
@@ -56,41 +60,53 @@ InputStream getStdin() {
5660
5761 @ Override
5862 protected void executeCommand (Terminal terminal , OptionSet options , Environment env ) throws Exception {
59- String setting = arguments .value (options );
60- if (setting == null ) {
61- throw new UserException (ExitCodes .USAGE , "The setting name can not be null " );
63+ final List < String > settings = arguments .values (options );
64+ if (settings . isEmpty () ) {
65+ throw new UserException (ExitCodes .USAGE , "the setting names can not be empty " );
6266 }
67+
6368 final KeyStoreWrapper keyStore = getKeyStore ();
64- if (keyStore .getSettingNames ().contains (setting ) && options .has (forceOption ) == false ) {
65- if (terminal .promptYesNo ("Setting " + setting + " already exists. Overwrite?" , false ) == false ) {
66- terminal .println ("Exiting without modifying keystore." );
67- return ;
68- }
69- }
7069
71- final char [] value ;
70+ final Closeable closeable ;
71+ final CheckedFunction <String , char [], IOException > valueSupplier ;
7272 if (options .has (stdinOption )) {
73- try (BufferedReader stdinReader = new BufferedReader (new InputStreamReader (getStdin (), StandardCharsets .UTF_8 ));
74- CharArrayWriter writer = new CharArrayWriter ()) {
75- int charInt ;
76- while ((charInt = stdinReader .read ()) != -1 ) {
77- if ((char ) charInt == '\r' || (char ) charInt == '\n' ) {
78- break ;
73+ final BufferedReader stdinReader = new BufferedReader (new InputStreamReader (getStdin (), StandardCharsets .UTF_8 ));
74+ valueSupplier = s -> {
75+ try (CharArrayWriter writer = new CharArrayWriter ()) {
76+ int c ;
77+ while ((c = stdinReader .read ()) != -1 ) {
78+ if ((char ) c == '\r' || (char ) c == '\n' ) {
79+ break ;
80+ }
81+ writer .write ((char ) c );
7982 }
80- writer .write (( char ) charInt );
83+ return writer .toCharArray ( );
8184 }
82- value = writer . toCharArray () ;
83- }
85+ } ;
86+ closeable = stdinReader ;
8487 } else {
85- value = terminal .readSecret ("Enter value for " + setting + ": " );
88+ valueSupplier = s -> terminal .readSecret ("Enter value for " + s + ": " );
89+ closeable = () -> {};
8690 }
8791
88- try {
89- keyStore .setString (setting , value );
90- } catch (IllegalArgumentException e ) {
91- throw new UserException (ExitCodes .DATA_ERROR , e .getMessage ());
92+ try (Closeable ignored = closeable ) {
93+ for (final String setting : settings ) {
94+ if (keyStore .getSettingNames ().contains (setting ) && options .has (forceOption ) == false ) {
95+ if (terminal .promptYesNo ("Setting " + setting + " already exists. Overwrite?" , false ) == false ) {
96+ terminal .println ("Exiting without modifying keystore." );
97+ return ;
98+ }
99+ }
100+
101+ try {
102+ keyStore .setString (setting , valueSupplier .apply (setting ));
103+ } catch (final IllegalArgumentException e ) {
104+ throw new UserException (ExitCodes .DATA_ERROR , e .getMessage ());
105+ }
106+ }
92107 }
93- keyStore .save (env .configFile (), getKeyStorePassword ().getChars ());
94108
109+ keyStore .save (env .configFile (), getKeyStorePassword ().getChars ());
95110 }
111+
96112}
0 commit comments