SOLR-6662: better validation when parsing command-line options that expect a value

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1635812 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Timothy Potter 2014-10-31 16:56:22 +00:00
parent ea5aa39e23
commit d76c1768b6
2 changed files with 60 additions and 20 deletions

View File

@ -342,7 +342,7 @@ function stop_solr() {
DIR="$1" DIR="$1"
SOLR_PORT="$2" SOLR_PORT="$2"
STOP_PORT="79${SOLR_PORT: -2}" STOP_PORT=`expr $SOLR_PORT - 1000`
STOP_KEY="$3" STOP_KEY="$3"
SOLR_PID="$4" SOLR_PID="$4"
@ -408,7 +408,7 @@ if [ "$SCRIPT_CMD" == "healthcheck" ]; then
while true; do while true; do
case $1 in case $1 in
-c|-collection) -c|-collection)
if [ "${2:0:1}" == "-" ]; then if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then
print_usage "$SCRIPT_CMD" "Expected collection name but found $2 instead!" print_usage "$SCRIPT_CMD" "Expected collection name but found $2 instead!"
exit 1 exit 1
fi fi
@ -416,7 +416,7 @@ if [ "$SCRIPT_CMD" == "healthcheck" ]; then
shift 2 shift 2
;; ;;
-z|-zkhost) -z|-zkhost)
if [ "${2:0:1}" == "-" ]; then if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then
print_usage "$SCRIPT_CMD" "Expected a ZooKeeper connection string but found $2 instead!" print_usage "$SCRIPT_CMD" "Expected a ZooKeeper connection string but found $2 instead!"
exit 1 exit 1
fi fi
@ -472,7 +472,7 @@ if [[ "$SCRIPT_CMD" == "create_core" || "$SCRIPT_CMD" == "create_collection" ]];
while true; do while true; do
case $1 in case $1 in
-n|-name) -n|-name)
if [ "${2:0:1}" == "-" ]; then if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then
print_usage "$SCRIPT_CMD" "Expected $CREATE_TYPE name but found $2 instead!" print_usage "$SCRIPT_CMD" "Expected $CREATE_TYPE name but found $2 instead!"
exit 1 exit 1
fi fi
@ -480,7 +480,7 @@ if [[ "$SCRIPT_CMD" == "create_core" || "$SCRIPT_CMD" == "create_collection" ]];
shift 2 shift 2
;; ;;
-c|-configset) -c|-configset)
if [ "${2:0:1}" == "-" ]; then if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then
print_usage "$SCRIPT_CMD" "Expected configset name but found $2 instead!" print_usage "$SCRIPT_CMD" "Expected configset name but found $2 instead!"
exit 1 exit 1
fi fi
@ -488,7 +488,7 @@ if [[ "$SCRIPT_CMD" == "create_core" || "$SCRIPT_CMD" == "create_collection" ]];
shift 2 shift 2
;; ;;
-shards) -shards)
if [ "${2:0:1}" == "-" ]; then if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then
print_usage "$SCRIPT_CMD" "Expected shard count but found $2 instead!" print_usage "$SCRIPT_CMD" "Expected shard count but found $2 instead!"
exit 1 exit 1
fi fi
@ -496,7 +496,7 @@ if [[ "$SCRIPT_CMD" == "create_core" || "$SCRIPT_CMD" == "create_collection" ]];
shift 2 shift 2
;; ;;
-replicationFactor) -replicationFactor)
if [ "${2:0:1}" == "-" ]; then if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then
print_usage "$SCRIPT_CMD" "Expected replication factor but found $2 instead!" print_usage "$SCRIPT_CMD" "Expected replication factor but found $2 instead!"
exit 1 exit 1
fi fi
@ -549,7 +549,6 @@ if [[ "$SCRIPT_CMD" == "create_core" || "$SCRIPT_CMD" == "create_collection" ]];
STATUS_INFO=`run_tool status -solr http://localhost:$CREATE_PORT/solr | tr -d ' '` STATUS_INFO=`run_tool status -solr http://localhost:$CREATE_PORT/solr | tr -d ' '`
IS_CLOUD=`echo $STATUS_INFO | grep $'"cloud":' | tr -d ' '` IS_CLOUD=`echo $STATUS_INFO | grep $'"cloud":' | tr -d ' '`
echo $IS_CLOUD
if [ "$IS_CLOUD" != "" ]; then if [ "$IS_CLOUD" != "" ]; then
if [ "$SCRIPT_CMD" == "create_core" ]; then if [ "$SCRIPT_CMD" == "create_core" ]; then
echo -e "\nERROR: Solr running on port $CREATE_PORT is running in SolrCloud mode, please use create_collection command instead.\n" echo -e "\nERROR: Solr running on port $CREATE_PORT is running in SolrCloud mode, please use create_collection command instead.\n"
@ -593,7 +592,7 @@ if [ $# -gt 0 ]; then
shift shift
;; ;;
-d|-dir) -d|-dir)
if [ "${2:0:1}" == "-" ]; then if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then
print_usage "$SCRIPT_CMD" "Expected directory but found $2 instead!" print_usage "$SCRIPT_CMD" "Expected directory but found $2 instead!"
exit 1 exit 1
fi fi
@ -607,7 +606,7 @@ if [ $# -gt 0 ]; then
shift 2 shift 2
;; ;;
-s|-solr.home) -s|-solr.home)
if [ "${2:0:1}" == "-" ]; then if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then
print_usage "$SCRIPT_CMD" "Expected directory but found $2 instead!" print_usage "$SCRIPT_CMD" "Expected directory but found $2 instead!"
exit 1 exit 1
fi fi
@ -616,7 +615,7 @@ if [ $# -gt 0 ]; then
shift 2 shift 2
;; ;;
-e|-example) -e|-example)
if [ "${2:0:1}" == "-" ]; then if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then
print_usage "$SCRIPT_CMD" "Expected example name but found $2 instead!" print_usage "$SCRIPT_CMD" "Expected example name but found $2 instead!"
exit 1 exit 1
fi fi
@ -628,7 +627,7 @@ if [ $# -gt 0 ]; then
shift shift
;; ;;
-h|-host) -h|-host)
if [ "${2:0:1}" == "-" ]; then if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then
print_usage "$SCRIPT_CMD" "Expected hostname but found $2 instead!" print_usage "$SCRIPT_CMD" "Expected hostname but found $2 instead!"
exit 1 exit 1
fi fi
@ -636,7 +635,7 @@ if [ $# -gt 0 ]; then
shift 2 shift 2
;; ;;
-m|-memory) -m|-memory)
if [ "${2:0:1}" == "-" ]; then if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then
print_usage "$SCRIPT_CMD" "Expected memory setting but found $2 instead!" print_usage "$SCRIPT_CMD" "Expected memory setting but found $2 instead!"
exit 1 exit 1
fi fi
@ -644,7 +643,7 @@ if [ $# -gt 0 ]; then
shift 2 shift 2
;; ;;
-p|-port) -p|-port)
if [ "${2:0:1}" == "-" ]; then if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then
print_usage "$SCRIPT_CMD" "Expected port number but found $2 instead!" print_usage "$SCRIPT_CMD" "Expected port number but found $2 instead!"
exit 1 exit 1
fi fi
@ -652,7 +651,7 @@ if [ $# -gt 0 ]; then
shift 2 shift 2
;; ;;
-z|-zkhost) -z|-zkhost)
if [ "${2:0:1}" == "-" ]; then if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then
print_usage "$SCRIPT_CMD" "Expected ZooKeeper connection string but found $2 instead!" print_usage "$SCRIPT_CMD" "Expected ZooKeeper connection string but found $2 instead!"
exit 1 exit 1
fi fi
@ -853,7 +852,7 @@ if [ "$SOLR_PORT" == "" ]; then
fi fi
if [ "$STOP_PORT" == "" ]; then if [ "$STOP_PORT" == "" ]; then
STOP_PORT="79${SOLR_PORT: -2}" STOP_PORT=`expr $SOLR_PORT - 1000`
fi fi
if [[ "$SCRIPT_CMD" == "start" ]]; then if [[ "$SCRIPT_CMD" == "start" ]]; then
@ -985,7 +984,7 @@ fi
function launch_solr() { function launch_solr() {
run_in_foreground="$1" run_in_foreground="$1"
stop_port="79${SOLR_PORT: -2}" stop_port="$STOP_PORT"
SOLR_ADDL_ARGS="$2" SOLR_ADDL_ARGS="$2"
@ -1088,7 +1087,7 @@ if [ "$EXAMPLE" != "cloud" ]; then
"$JAVA" -Durl=http://localhost:$SOLR_PORT/solr/$EXAMPLE/update -jar $SOLR_TIP/example/exampledocs/post.jar $SOLR_TIP/example/exampledocs/*.xml "$JAVA" -Durl=http://localhost:$SOLR_PORT/solr/$EXAMPLE/update -jar $SOLR_TIP/example/exampledocs/post.jar $SOLR_TIP/example/exampledocs/*.xml
fi fi
echo -e "\nSolr $EXAMPLE launched successfully. Direct your Web browser to http://localhost:$SOLR_PORT/solr to visit the Solr Admin UI\n" echo -e "\nSolr $EXAMPLE example launched successfully. Direct your Web browser to http://localhost:$SOLR_PORT/solr to visit the Solr Admin UI\n"
fi fi
else else
# #

View File

@ -278,6 +278,10 @@ goto parse_args
:set_server_dir :set_server_dir
set "arg=%~2" set "arg=%~2"
IF "%arg%"=="" (
set SCRIPT_ERROR=Directory name is required!
goto invalid_cmd_line
)
set firstChar=%arg:~0,1% set firstChar=%arg:~0,1%
IF "%firstChar%"=="-" ( IF "%firstChar%"=="-" (
set SCRIPT_ERROR=Expected directory but found %2 instead! set SCRIPT_ERROR=Expected directory but found %2 instead!
@ -297,6 +301,11 @@ goto parse_args
:set_solr_home_dir :set_solr_home_dir
set "arg=%~2" set "arg=%~2"
IF "%arg%"=="" (
set SCRIPT_ERROR=Directory name is required!
goto invalid_cmd_line
)
set firstChar=%arg:~0,1% set firstChar=%arg:~0,1%
IF "%firstChar%"=="-" ( IF "%firstChar%"=="-" (
set SCRIPT_ERROR=Expected directory but found %2 instead! set SCRIPT_ERROR=Expected directory but found %2 instead!
@ -310,6 +319,11 @@ goto parse_args
:set_example :set_example
set "arg=%~2" set "arg=%~2"
IF "%arg%"=="" (
set SCRIPT_ERROR=Example name is required!
goto invalid_cmd_line
)
set firstChar=%arg:~0,1% set firstChar=%arg:~0,1%
IF "%firstChar%"=="-" ( IF "%firstChar%"=="-" (
set SCRIPT_ERROR=Expected example name but found %2 instead! set SCRIPT_ERROR=Expected example name but found %2 instead!
@ -324,6 +338,11 @@ goto parse_args
:set_memory :set_memory
set "arg=%~2" set "arg=%~2"
IF "%arg%"=="" (
set SCRIPT_ERROR=Memory setting is required!
goto invalid_cmd_line
)
set firstChar=%arg:~0,1% set firstChar=%arg:~0,1%
IF "%firstChar%"=="-" ( IF "%firstChar%"=="-" (
set SCRIPT_ERROR=Expected memory setting but found %2 instead! set SCRIPT_ERROR=Expected memory setting but found %2 instead!
@ -337,6 +356,11 @@ goto parse_args
:set_host :set_host
set "arg=%~2" set "arg=%~2"
IF "%arg%"=="" (
set SCRIPT_ERROR=Hostname is required!
goto invalid_cmd_line
)
set firstChar=%arg:~0,1% set firstChar=%arg:~0,1%
IF "%firstChar%"=="-" ( IF "%firstChar%"=="-" (
set SCRIPT_ERROR=Expected hostname but found %2 instead! set SCRIPT_ERROR=Expected hostname but found %2 instead!
@ -350,6 +374,11 @@ goto parse_args
:set_port :set_port
set "arg=%~2" set "arg=%~2"
IF "%arg%"=="" (
set SCRIPT_ERROR=Port is required!
goto invalid_cmd_line
)
set firstChar=%arg:~0,1% set firstChar=%arg:~0,1%
IF "%firstChar%"=="-" ( IF "%firstChar%"=="-" (
set SCRIPT_ERROR=Expected port but found %2 instead! set SCRIPT_ERROR=Expected port but found %2 instead!
@ -363,9 +392,14 @@ goto parse_args
:set_stop_key :set_stop_key
set "arg=%~2" set "arg=%~2"
IF "%arg%"=="" (
set SCRIPT_ERROR=Stop key is required!
goto invalid_cmd_line
)
set firstChar=%arg:~0,1% set firstChar=%arg:~0,1%
IF "%firstChar%"=="-" ( IF "%firstChar%"=="-" (
set SCRIPT_ERROR=Expected port but found %2 instead! set SCRIPT_ERROR=Expected stop key but found %2 instead!
goto invalid_cmd_line goto invalid_cmd_line
) )
set STOP_KEY=%~2 set STOP_KEY=%~2
@ -381,6 +415,11 @@ goto parse_args
:set_zookeeper :set_zookeeper
set "arg=%~2" set "arg=%~2"
IF "%arg%"=="" (
set SCRIPT_ERROR=ZooKeeper connection string is required!
goto invalid_cmd_line
)
set firstChar=%arg:~0,1% set firstChar=%arg:~0,1%
IF "%firstChar%"=="-" ( IF "%firstChar%"=="-" (
set SCRIPT_ERROR=Expected ZooKeeper connection string but found %2 instead! set SCRIPT_ERROR=Expected ZooKeeper connection string but found %2 instead!
@ -638,7 +677,9 @@ IF "%EXAMPLE%"=="techproducts" (
) )
@echo. @echo.
@echo Solr %EXAMPLE% launched successfully. IF NOT "%EXAMPLE%"=="" (
@echo Solr %EXAMPLE% example launched successfully.
)
@echo Direct your Web browser to http://localhost:%SOLR_PORT%/solr to visit the Solr Admin UI @echo Direct your Web browser to http://localhost:%SOLR_PORT%/solr to visit the Solr Admin UI
@echo. @echo.