Skip to content

Commit 01b5a46

Browse files
authored
Pass main class by environment variable on Windows (#31156)
A previous refactoring of the CLI scripts migrated all of the CLI tools to shell to a common script, elasticsearch-cli. This approach is fine in Bash where it is easy to tear arguments apart but it doesn't work so well on Windows where quoting is insane. To avoid having to tear the arguments apart to separate the first argument to elasticsearch-cli from the remaining arguments, we instead choose a strategy where we can avoid tearing the arguments apart. To do this, we will instead pass the main class by an environment variable and then we can pass the arguments straight through. This will let us avoid awful quoting issues on Windows. This is the Windows side of that effort and the Bash side was in a previous commit.
1 parent 95795c8 commit 01b5a46

12 files changed

+13
-18
lines changed

distribution/src/bin/elasticsearch-cli.bat

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ if defined ES_ADDITIONAL_SOURCES (
66
)
77
)
88

9-
for /f "tokens=1*" %%a in ("%*") do (
10-
set main_class=%%a
11-
set arguments=%%b
12-
)
13-
149
if defined ES_ADDITIONAL_CLASSPATH_DIRECTORIES (
1510
for %%a in ("%ES_ADDITIONAL_CLASSPATH_DIRECTORIES:;=","%") do (
1611
set ES_CLASSPATH=!ES_CLASSPATH!;!ES_HOME!/%%a/*
@@ -24,5 +19,5 @@ if defined ES_ADDITIONAL_CLASSPATH_DIRECTORIES (
2419
-Des.distribution.flavor="%ES_DISTRIBUTION_FLAVOR%" ^
2520
-Des.distribution.type="%ES_DISTRIBUTION_TYPE%" ^
2621
-cp "%ES_CLASSPATH%" ^
27-
%main_class% ^
28-
%arguments%
22+
"%ES_MAIN_CLASS%" ^
23+
%*

distribution/src/bin/elasticsearch-keystore.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
setlocal enabledelayedexpansion
44
setlocal enableextensions
55

6+
set ES_MAIN_CLASS=org.elasticsearch.common.settings.KeyStoreCli
67
call "%~dp0elasticsearch-cli.bat" ^
7-
org.elasticsearch.common.settings.KeyStoreCli ^
88
%%* ^
99
|| exit /b 1
1010

distribution/src/bin/elasticsearch-plugin.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
setlocal enabledelayedexpansion
44
setlocal enableextensions
55

6+
set ES_MAIN_CLASS=org.elasticsearch.plugins.PluginCli
67
set ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/plugin-cli
78
call "%~dp0elasticsearch-cli.bat" ^
8-
org.elasticsearch.plugins.PluginCli ^
99
%%* ^
1010
|| exit /b 1
1111

distribution/src/bin/elasticsearch-translog.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
setlocal enabledelayedexpansion
44
setlocal enableextensions
55

6+
set ES_MAIN_CLASS=org.elasticsearch.index.translog.TranslogToolCli
67
call "%~dp0elasticsearch-cli.bat" ^
7-
org.elasticsearch.index.translog.TranslogToolCli ^
88
%%* ^
99
|| exit /b 1
1010

x-pack/plugin/security/src/main/bin/elasticsearch-certgen.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License.
77
setlocal enabledelayedexpansion
88
setlocal enableextensions
99

10+
set ES_MAIN_CLASS=org.elasticsearch.xpack.core.ssl.CertificateGenerateTool
1011
set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env
1112
call "%~dp0elasticsearch-cli.bat" ^
12-
org.elasticsearch.xpack.core.ssl.CertificateGenerateTool ^
1313
%%* ^
1414
|| exit /b 1
1515

x-pack/plugin/security/src/main/bin/elasticsearch-certutil.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License.
77
setlocal enabledelayedexpansion
88
setlocal enableextensions
99

10+
set ES_MAIN_CLASS=org.elasticsearch.xpack.core.ssl.CertificateTool
1011
set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env
1112
call "%~dp0elasticsearch-cli.bat" ^
12-
org.elasticsearch.xpack.core.ssl.CertificateTool ^
1313
%%* ^
1414
|| exit /b 1
1515

x-pack/plugin/security/src/main/bin/elasticsearch-migrate.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License.
77
setlocal enabledelayedexpansion
88
setlocal enableextensions
99

10+
set ES_MAIN_CLASS=org.elasticsearch.xpack.security.authc.esnative.ESNativeRealmMigrateTool
1011
set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env
1112
call "%~dp0elasticsearch-cli.bat" ^
12-
org.elasticsearch.xpack.security.authc.esnative.ESNativeRealmMigrateTool ^
1313
%%* ^
1414
|| exit /b 1
1515

x-pack/plugin/security/src/main/bin/elasticsearch-saml-metadata.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License.
77
setlocal enabledelayedexpansion
88
setlocal enableextensions
99

10+
set ES_MAIN_CLASS=org.elasticsearch.xpack.security.authc.saml.SamlMetadataCommand
1011
set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env
1112
call "%~dp0elasticsearch-cli.bat" ^
12-
org.elasticsearch.xpack.security.authc.saml.SamlMetadataCommand ^
1313
%%* ^
1414
|| exit /b 1
1515

x-pack/plugin/security/src/main/bin/elasticsearch-setup-passwords.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License.
77
setlocal enabledelayedexpansion
88
setlocal enableextensions
99

10+
set ES_MAIN_CLASS=org.elasticsearch.xpack.security.authc.esnative.tool.SetupPasswordTool
1011
set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env
1112
call "%~dp0elasticsearch-cli.bat" ^
12-
org.elasticsearch.xpack.security.authc.esnative.tool.SetupPasswordTool ^
1313
%%* ^
1414
|| exit /b 1
1515

x-pack/plugin/security/src/main/bin/elasticsearch-syskeygen.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License.
77
setlocal enabledelayedexpansion
88
setlocal enableextensions
99

10+
set ES_MAIN_CLASS=org.elasticsearch.xpack.security.crypto.tool.SystemKeyTool
1011
set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env
1112
call "%~dp0elasticsearch-cli.bat" ^
12-
org.elasticsearch.xpack.security.crypto.tool.SystemKeyTool ^
1313
%%* ^
1414
|| exit /b 1
1515

x-pack/plugin/security/src/main/bin/elasticsearch-users.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License.
77
setlocal enabledelayedexpansion
88
setlocal enableextensions
99

10+
set ES_MAIN_CLASS=org.elasticsearch.xpack.security.authc.file.tool.UsersTool
1011
set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env
1112
call "%~dp0elasticsearch-cli.bat" ^
12-
org.elasticsearch.xpack.security.authc.file.tool.UsersTool ^
1313
%%* ^
1414
|| exit /b 1
1515

x-pack/plugin/watcher/src/main/bin/elasticsearch-croneval.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License.
77
setlocal enabledelayedexpansion
88
setlocal enableextensions
99

10+
set ES_MAIN_CLASS=org.elasticsearch.xpack.watcher.trigger.schedule.tool.CronEvalTool
1011
set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-watcher-env
1112
call "%~dp0elasticsearch-cli.bat" ^
12-
org.elasticsearch.xpack.watcher.trigger.schedule.tool.CronEvalTool ^
1313
%%* ^
1414
|| exit /b 1
1515

0 commit comments

Comments
 (0)