1919
2020package org .elasticsearch .common .settings ;
2121
22- import java .nio .file .Files ;
23- import java .nio .file .Path ;
24- import java .util .Arrays ;
25- import java .util .List ;
26-
2722import joptsimple .OptionSet ;
2823import joptsimple .OptionSpec ;
2924import org .elasticsearch .cli .ExitCodes ;
3328import org .elasticsearch .common .io .PathUtils ;
3429import org .elasticsearch .env .Environment ;
3530
31+ import java .nio .file .Files ;
32+ import java .nio .file .Path ;
33+ import java .util .Arrays ;
34+ import java .util .List ;
35+
3636/**
3737 * A subcommand for the keystore cli which adds a file setting.
3838 */
@@ -47,41 +47,45 @@ class AddFileKeyStoreCommand extends BaseKeyStoreCommand {
4747 // jopt simple has issue with multiple non options, so we just get one set of them here
4848 // and convert to File when necessary
4949 // see https://github.com/jopt-simple/jopt-simple/issues/103
50- this .arguments = parser .nonOptions ("setting [filepath] " );
50+ this .arguments = parser .nonOptions ("( setting path)+ " );
5151 }
5252
5353 @ Override
5454 protected void executeCommand (Terminal terminal , OptionSet options , Environment env ) throws Exception {
55- List <String > argumentValues = arguments .values (options );
55+ final List <String > argumentValues = arguments .values (options );
5656 if (argumentValues .size () == 0 ) {
5757 throw new UserException (ExitCodes .USAGE , "Missing setting name" );
5858 }
59- String setting = argumentValues .get (0 );
59+ if (argumentValues .size () % 2 != 0 ) {
60+ throw new UserException (ExitCodes .USAGE , "settings and filenames must come in pairs" );
61+ }
62+
6063 final KeyStoreWrapper keyStore = getKeyStore ();
61- if (keyStore .getSettingNames ().contains (setting ) && options .has (forceOption ) == false ) {
62- if (terminal .promptYesNo ("Setting " + setting + " already exists. Overwrite?" , false ) == false ) {
63- terminal .println ("Exiting without modifying keystore." );
64- return ;
64+
65+ for (int i = 0 ; i < argumentValues .size (); i += 2 ) {
66+ final String setting = argumentValues .get (i );
67+
68+ if (keyStore .getSettingNames ().contains (setting ) && options .has (forceOption ) == false ) {
69+ if (terminal .promptYesNo ("Setting " + setting + " already exists. Overwrite?" , false ) == false ) {
70+ terminal .println ("Exiting without modifying keystore." );
71+ return ;
72+ }
6573 }
66- }
6774
68- if (argumentValues .size () == 1 ) {
69- throw new UserException (ExitCodes .USAGE , "Missing file name" );
70- }
71- Path file = getPath (argumentValues .get (1 ));
72- if (Files .exists (file ) == false ) {
73- throw new UserException (ExitCodes .IO_ERROR , "File [" + file .toString () + "] does not exist" );
74- }
75- if (argumentValues .size () > 2 ) {
76- throw new UserException (ExitCodes .USAGE , "Unrecognized extra arguments [" +
77- String .join (", " , argumentValues .subList (2 , argumentValues .size ())) + "] after filepath" );
75+ final Path file = getPath (argumentValues .get (i + 1 ));
76+ if (Files .exists (file ) == false ) {
77+ throw new UserException (ExitCodes .IO_ERROR , "File [" + file .toString () + "] does not exist" );
78+ }
79+
80+ keyStore .setFile (setting , Files .readAllBytes (file ));
7881 }
79- keyStore . setFile ( setting , Files . readAllBytes ( file ));
82+
8083 keyStore .save (env .configFile (), getKeyStorePassword ().getChars ());
8184 }
8285
8386 @ SuppressForbidden (reason = "file arg for cli" )
8487 private Path getPath (String file ) {
8588 return PathUtils .get (file );
8689 }
90+
8791}
0 commit comments