19
19
20
20
package org .elasticsearch .common .settings ;
21
21
22
- import java .nio .file .Files ;
23
- import java .nio .file .Path ;
24
- import java .util .Arrays ;
25
- import java .util .List ;
26
-
27
22
import joptsimple .OptionSet ;
28
23
import joptsimple .OptionSpec ;
29
24
import org .elasticsearch .cli .ExitCodes ;
33
28
import org .elasticsearch .common .io .PathUtils ;
34
29
import org .elasticsearch .env .Environment ;
35
30
31
+ import java .nio .file .Files ;
32
+ import java .nio .file .Path ;
33
+ import java .util .Arrays ;
34
+ import java .util .List ;
35
+
36
36
/**
37
37
* A subcommand for the keystore cli which adds a file setting.
38
38
*/
@@ -47,41 +47,45 @@ class AddFileKeyStoreCommand extends BaseKeyStoreCommand {
47
47
// jopt simple has issue with multiple non options, so we just get one set of them here
48
48
// and convert to File when necessary
49
49
// see https://github.com/jopt-simple/jopt-simple/issues/103
50
- this .arguments = parser .nonOptions ("setting [filepath] " );
50
+ this .arguments = parser .nonOptions ("( setting path)+ " );
51
51
}
52
52
53
53
@ Override
54
54
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 );
56
56
if (argumentValues .size () == 0 ) {
57
57
throw new UserException (ExitCodes .USAGE , "Missing setting name" );
58
58
}
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
+
60
63
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
+ }
65
73
}
66
- }
67
74
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 ));
78
81
}
79
- keyStore . setFile ( setting , Files . readAllBytes ( file ));
82
+
80
83
keyStore .save (env .configFile (), getKeyStorePassword ().getChars ());
81
84
}
82
85
83
86
@ SuppressForbidden (reason = "file arg for cli" )
84
87
private Path getPath (String file ) {
85
88
return PathUtils .get (file );
86
89
}
90
+
87
91
}
0 commit comments