From d76c1768b6210bc2ae2a0aaaa8d6b77b1792cc0f Mon Sep 17 00:00:00 2001 From: Timothy Potter Date: Fri, 31 Oct 2014 16:56:22 +0000 Subject: [PATCH] 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 --- solr/bin/solr | 35 +++++++++++++++++------------------ solr/bin/solr.cmd | 45 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 60 insertions(+), 20 deletions(-) diff --git a/solr/bin/solr b/solr/bin/solr index b9531950364..0d5e54a4e03 100755 --- a/solr/bin/solr +++ b/solr/bin/solr @@ -342,7 +342,7 @@ function stop_solr() { DIR="$1" SOLR_PORT="$2" - STOP_PORT="79${SOLR_PORT: -2}" + STOP_PORT=`expr $SOLR_PORT - 1000` STOP_KEY="$3" SOLR_PID="$4" @@ -408,7 +408,7 @@ if [ "$SCRIPT_CMD" == "healthcheck" ]; then while true; do case $1 in -c|-collection) - if [ "${2:0:1}" == "-" ]; then + if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then print_usage "$SCRIPT_CMD" "Expected collection name but found $2 instead!" exit 1 fi @@ -416,7 +416,7 @@ if [ "$SCRIPT_CMD" == "healthcheck" ]; then shift 2 ;; -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!" exit 1 fi @@ -472,7 +472,7 @@ if [[ "$SCRIPT_CMD" == "create_core" || "$SCRIPT_CMD" == "create_collection" ]]; while true; do case $1 in -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!" exit 1 fi @@ -480,7 +480,7 @@ if [[ "$SCRIPT_CMD" == "create_core" || "$SCRIPT_CMD" == "create_collection" ]]; shift 2 ;; -c|-configset) - if [ "${2:0:1}" == "-" ]; then + if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then print_usage "$SCRIPT_CMD" "Expected configset name but found $2 instead!" exit 1 fi @@ -488,7 +488,7 @@ if [[ "$SCRIPT_CMD" == "create_core" || "$SCRIPT_CMD" == "create_collection" ]]; shift 2 ;; -shards) - if [ "${2:0:1}" == "-" ]; then + if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then print_usage "$SCRIPT_CMD" "Expected shard count but found $2 instead!" exit 1 fi @@ -496,7 +496,7 @@ if [[ "$SCRIPT_CMD" == "create_core" || "$SCRIPT_CMD" == "create_collection" ]]; shift 2 ;; -replicationFactor) - if [ "${2:0:1}" == "-" ]; then + if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then print_usage "$SCRIPT_CMD" "Expected replication factor but found $2 instead!" exit 1 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 ' '` IS_CLOUD=`echo $STATUS_INFO | grep $'"cloud":' | tr -d ' '` - echo $IS_CLOUD if [ "$IS_CLOUD" != "" ]; 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" @@ -593,7 +592,7 @@ if [ $# -gt 0 ]; then shift ;; -d|-dir) - if [ "${2:0:1}" == "-" ]; then + if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then print_usage "$SCRIPT_CMD" "Expected directory but found $2 instead!" exit 1 fi @@ -607,7 +606,7 @@ if [ $# -gt 0 ]; then shift 2 ;; -s|-solr.home) - if [ "${2:0:1}" == "-" ]; then + if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then print_usage "$SCRIPT_CMD" "Expected directory but found $2 instead!" exit 1 fi @@ -616,7 +615,7 @@ if [ $# -gt 0 ]; then shift 2 ;; -e|-example) - if [ "${2:0:1}" == "-" ]; then + if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then print_usage "$SCRIPT_CMD" "Expected example name but found $2 instead!" exit 1 fi @@ -628,7 +627,7 @@ if [ $# -gt 0 ]; then shift ;; -h|-host) - if [ "${2:0:1}" == "-" ]; then + if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then print_usage "$SCRIPT_CMD" "Expected hostname but found $2 instead!" exit 1 fi @@ -636,7 +635,7 @@ if [ $# -gt 0 ]; then shift 2 ;; -m|-memory) - if [ "${2:0:1}" == "-" ]; then + if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then print_usage "$SCRIPT_CMD" "Expected memory setting but found $2 instead!" exit 1 fi @@ -644,7 +643,7 @@ if [ $# -gt 0 ]; then shift 2 ;; -p|-port) - if [ "${2:0:1}" == "-" ]; then + if [[ "$2" == "" || "${2:0:1}" == "-" ]]; then print_usage "$SCRIPT_CMD" "Expected port number but found $2 instead!" exit 1 fi @@ -652,7 +651,7 @@ if [ $# -gt 0 ]; then shift 2 ;; -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!" exit 1 fi @@ -853,7 +852,7 @@ if [ "$SOLR_PORT" == "" ]; then fi if [ "$STOP_PORT" == "" ]; then - STOP_PORT="79${SOLR_PORT: -2}" + STOP_PORT=`expr $SOLR_PORT - 1000` fi if [[ "$SCRIPT_CMD" == "start" ]]; then @@ -985,7 +984,7 @@ fi function launch_solr() { run_in_foreground="$1" - stop_port="79${SOLR_PORT: -2}" + stop_port="$STOP_PORT" 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 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 else # diff --git a/solr/bin/solr.cmd b/solr/bin/solr.cmd index d6f30d4d08e..ea47cd9e9c3 100644 --- a/solr/bin/solr.cmd +++ b/solr/bin/solr.cmd @@ -278,6 +278,10 @@ goto parse_args :set_server_dir set "arg=%~2" +IF "%arg%"=="" ( + set SCRIPT_ERROR=Directory name is required! + goto invalid_cmd_line +) set firstChar=%arg:~0,1% IF "%firstChar%"=="-" ( set SCRIPT_ERROR=Expected directory but found %2 instead! @@ -297,6 +301,11 @@ goto parse_args :set_solr_home_dir set "arg=%~2" +IF "%arg%"=="" ( + set SCRIPT_ERROR=Directory name is required! + goto invalid_cmd_line +) + set firstChar=%arg:~0,1% IF "%firstChar%"=="-" ( set SCRIPT_ERROR=Expected directory but found %2 instead! @@ -310,6 +319,11 @@ goto parse_args :set_example set "arg=%~2" +IF "%arg%"=="" ( + set SCRIPT_ERROR=Example name is required! + goto invalid_cmd_line +) + set firstChar=%arg:~0,1% IF "%firstChar%"=="-" ( set SCRIPT_ERROR=Expected example name but found %2 instead! @@ -324,6 +338,11 @@ goto parse_args :set_memory set "arg=%~2" +IF "%arg%"=="" ( + set SCRIPT_ERROR=Memory setting is required! + goto invalid_cmd_line +) + set firstChar=%arg:~0,1% IF "%firstChar%"=="-" ( set SCRIPT_ERROR=Expected memory setting but found %2 instead! @@ -337,6 +356,11 @@ goto parse_args :set_host set "arg=%~2" +IF "%arg%"=="" ( + set SCRIPT_ERROR=Hostname is required! + goto invalid_cmd_line +) + set firstChar=%arg:~0,1% IF "%firstChar%"=="-" ( set SCRIPT_ERROR=Expected hostname but found %2 instead! @@ -350,6 +374,11 @@ goto parse_args :set_port set "arg=%~2" +IF "%arg%"=="" ( + set SCRIPT_ERROR=Port is required! + goto invalid_cmd_line +) + set firstChar=%arg:~0,1% IF "%firstChar%"=="-" ( set SCRIPT_ERROR=Expected port but found %2 instead! @@ -363,9 +392,14 @@ goto parse_args :set_stop_key set "arg=%~2" +IF "%arg%"=="" ( + set SCRIPT_ERROR=Stop key is required! + goto invalid_cmd_line +) + set firstChar=%arg:~0,1% 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 ) set STOP_KEY=%~2 @@ -381,6 +415,11 @@ goto parse_args :set_zookeeper set "arg=%~2" +IF "%arg%"=="" ( + set SCRIPT_ERROR=ZooKeeper connection string is required! + goto invalid_cmd_line +) + set firstChar=%arg:~0,1% IF "%firstChar%"=="-" ( set SCRIPT_ERROR=Expected ZooKeeper connection string but found %2 instead! @@ -638,7 +677,9 @@ IF "%EXAMPLE%"=="techproducts" ( ) @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.