Skip to content

Commit

Permalink
Pass main class by environment variable on Windows (#31156)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jasontedor authored Jun 7, 2018
1 parent 95795c8 commit 01b5a46
Show file tree
Hide file tree
Showing 12 changed files with 13 additions and 18 deletions.
9 changes: 2 additions & 7 deletions distribution/src/bin/elasticsearch-cli.bat
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ if defined ES_ADDITIONAL_SOURCES (
)
)

for /f "tokens=1*" %%a in ("%*") do (
set main_class=%%a
set arguments=%%b
)

if defined ES_ADDITIONAL_CLASSPATH_DIRECTORIES (
for %%a in ("%ES_ADDITIONAL_CLASSPATH_DIRECTORIES:;=","%") do (
set ES_CLASSPATH=!ES_CLASSPATH!;!ES_HOME!/%%a/*
Expand All @@ -24,5 +19,5 @@ if defined ES_ADDITIONAL_CLASSPATH_DIRECTORIES (
-Des.distribution.flavor="%ES_DISTRIBUTION_FLAVOR%" ^
-Des.distribution.type="%ES_DISTRIBUTION_TYPE%" ^
-cp "%ES_CLASSPATH%" ^
%main_class% ^
%arguments%
"%ES_MAIN_CLASS%" ^
%*
2 changes: 1 addition & 1 deletion distribution/src/bin/elasticsearch-keystore.bat
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
setlocal enabledelayedexpansion
setlocal enableextensions

set ES_MAIN_CLASS=org.elasticsearch.common.settings.KeyStoreCli
call "%~dp0elasticsearch-cli.bat" ^
org.elasticsearch.common.settings.KeyStoreCli ^
%%* ^
|| exit /b 1

Expand Down
2 changes: 1 addition & 1 deletion distribution/src/bin/elasticsearch-plugin.bat
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
setlocal enabledelayedexpansion
setlocal enableextensions

set ES_MAIN_CLASS=org.elasticsearch.plugins.PluginCli
set ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/plugin-cli
call "%~dp0elasticsearch-cli.bat" ^
org.elasticsearch.plugins.PluginCli ^
%%* ^
|| exit /b 1

Expand Down
2 changes: 1 addition & 1 deletion distribution/src/bin/elasticsearch-translog.bat
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
setlocal enabledelayedexpansion
setlocal enableextensions

set ES_MAIN_CLASS=org.elasticsearch.index.translog.TranslogToolCli
call "%~dp0elasticsearch-cli.bat" ^
org.elasticsearch.index.translog.TranslogToolCli ^
%%* ^
|| exit /b 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License.
setlocal enabledelayedexpansion
setlocal enableextensions

set ES_MAIN_CLASS=org.elasticsearch.xpack.core.ssl.CertificateGenerateTool
set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env
call "%~dp0elasticsearch-cli.bat" ^
org.elasticsearch.xpack.core.ssl.CertificateGenerateTool ^
%%* ^
|| exit /b 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License.
setlocal enabledelayedexpansion
setlocal enableextensions

set ES_MAIN_CLASS=org.elasticsearch.xpack.core.ssl.CertificateTool
set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env
call "%~dp0elasticsearch-cli.bat" ^
org.elasticsearch.xpack.core.ssl.CertificateTool ^
%%* ^
|| exit /b 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License.
setlocal enabledelayedexpansion
setlocal enableextensions

set ES_MAIN_CLASS=org.elasticsearch.xpack.security.authc.esnative.ESNativeRealmMigrateTool
set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env
call "%~dp0elasticsearch-cli.bat" ^
org.elasticsearch.xpack.security.authc.esnative.ESNativeRealmMigrateTool ^
%%* ^
|| exit /b 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License.
setlocal enabledelayedexpansion
setlocal enableextensions

set ES_MAIN_CLASS=org.elasticsearch.xpack.security.authc.saml.SamlMetadataCommand
set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env
call "%~dp0elasticsearch-cli.bat" ^
org.elasticsearch.xpack.security.authc.saml.SamlMetadataCommand ^
%%* ^
|| exit /b 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License.
setlocal enabledelayedexpansion
setlocal enableextensions

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License.
setlocal enabledelayedexpansion
setlocal enableextensions

set ES_MAIN_CLASS=org.elasticsearch.xpack.security.crypto.tool.SystemKeyTool
set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env
call "%~dp0elasticsearch-cli.bat" ^
org.elasticsearch.xpack.security.crypto.tool.SystemKeyTool ^
%%* ^
|| exit /b 1

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License.
setlocal enabledelayedexpansion
setlocal enableextensions

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License.
setlocal enabledelayedexpansion
setlocal enableextensions

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

Expand Down

0 comments on commit 01b5a46

Please sign in to comment.