• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2# Copyright (c) 2024 Huawei Device Co., Ltd.
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15set -u
16set -e
17set -o pipefail
18
19ENV_VALUE=""
20SCRIPT_ARGS_VALUE=""
21RUNNER_VALUE=""
22RUNNER_ARGS_VALUE=""
23RET_CODE_VALUE=0
24
25SCRIPT_ARGUMENT="--script"
26SCRIPT_ARGS_OPTION="--script-args"
27RUNNER_OPTION="--runner"
28RUNNER_ARGS_OPTION="--runner-args"
29ENV_OPTION="--env"
30RET_CODE_OPTION="--ret-code"
31HELP_OPTION="--help"
32DEBUG_OPTION="--debug"
33
34declare -i TRUE=1
35declare -i FALSE=0
36
37declare -i HELP_VALUE=$FALSE
38declare -i DEBUG_VALUE=$FALSE
39
40print_help() {
41    echo "Usage: run_script.sh ${SCRIPT_ARGUMENT}=                                                        "
42    echo "      [${SCRIPT_ARGS_OPTION}=]+ [${RUNNER_ARGS_OPTION}=]+ [${ENV_OPTION}=]+                     "
43    echo "      [${RUNNER_OPTION}=] [${RET_CODE_OPTION}=] [${HELP_OPTION}] [${DEBUG_OPTION}]              "
44    echo "Arguments:                                                                                      "
45    echo "      ${SCRIPT_ARGUMENT}      Path for the script to be executed.                               "
46    echo "Options:                                                                                        "
47    echo "      ${SCRIPT_ARGS_OPTION}   Arguments for the script.                                         "
48    echo "      ${RUNNER_OPTION}        Specify command that shall execute the script.                    "
49    echo "      ${RUNNER_ARGS_OPTION}   Specify arguments for the script runner.                          "
50    echo "      ${ENV_OPTION}           Environment for the script execution.                             "
51    echo "      ${RET_CODE_OPTION}      Specify expected return code of the command. Set to 0 by default. "
52    echo "      ${HELP_OPTION}          Print this message.                                               "
53    echo "      ${DEBUG_OPTION}         Print debug information.                                          "
54}
55
56report_error() {
57    echo "[ERROR]/: " "$@"
58    exit 1
59}
60
61debug_message() {
62    if [ ${DEBUG_VALUE} -eq ${TRUE} ]; then
63        echo "[DEBUG]/: " "$@"
64    fi
65}
66
67parse_args() {
68    for i in "$@"; do
69        case "$i" in
70        "${SCRIPT_ARGUMENT}"=*)
71            SCRIPT_VALUE="${i#*=}"
72            shift
73            ;;
74        "${SCRIPT_ARGS_OPTION}"=*)
75            # CC-OFFNXT(bc-50008) false positive
76            SCRIPT_ARGS_VALUE="${SCRIPT_ARGS_VALUE} ${i#*=}"
77            shift
78            ;;
79        "${RUNNER_OPTION}"=*)
80            # CC-OFFNXT(bc-50008) false positive
81            RUNNER_VALUE="${i#*=}"
82            shift
83            ;;
84        "${RET_CODE_OPTION}"=*)
85            # CC-OFFNXT(bc-50008) false positive
86            RET_CODE_VALUE="${i#*=}"
87            shift
88            ;;
89        "${RUNNER_ARGS_OPTION}"=*)
90            # CC-OFFNXT(bc-50008) false positive
91            RUNNER_ARGS_VALUE="${RUNNER_ARGS_VALUE} ${i#*=}"
92            shift
93            ;;
94        "${ENV_OPTION}"=*)
95            # CC-OFFNXT(bc-50008) false positive
96            ENV_VALUE="${ENV_VALUE} ${i#*=}"
97            shift
98            ;;
99        "${HELP_OPTION}")
100            # CC-OFFNXT(bc-50008) false positive
101            HELP_VALUE=$TRUE
102            shift
103            ;;
104        "${DEBUG_OPTION}")
105            # CC-OFFNXT(bc-50008) false positive
106            DEBUG_VALUE=$TRUE
107            shift
108            ;;
109        *)
110            print_help
111            report_error "Recieved unexpected argument: ${i}"
112            ;;
113        esac
114    done
115}
116
117check_args() {
118    if [ -z "$SCRIPT_VALUE" ]; then
119        report_error "${SCRIPT_ARGUMENT} argument can not be empty."
120    fi
121
122    if [[ -z "${RUNNER_VALUE}" && -n "${RUNNER_ARGS_VALUE}" ]]; then
123        report_error "You must specify ${RUNNER_OPTION} for ${RUNNER_ARGS_OPTION} to be passed to."
124    fi
125}
126
127print_parsed_args() {
128    debug_message "Parsed arguments:"
129    debug_message "${SCRIPT_ARGUMENT}=${SCRIPT_VALUE}"
130    debug_message "${SCRIPT_ARGS_OPTION}=${SCRIPT_ARGS_VALUE}"
131    debug_message "${RUNNER_OPTION}=${RUNNER_VALUE}"
132    debug_message "${RUNNER_ARGS_OPTION}=${RUNNER_ARGS_VALUE}"
133    debug_message "${ENV_OPTION}=${ENV_VALUE}"
134    debug_message "${RET_CODE_OPTION}=${RET_CODE_VALUE}"
135    debug_message "${HELP_OPTION}=${HELP_VALUE}"
136}
137
138main() {
139    parse_args "$@"
140    print_parsed_args
141
142    if [ ${HELP_VALUE} -eq ${TRUE} ]; then
143        print_help
144        exit 0
145    fi
146
147    check_args
148
149    local -i ret_code=0
150    if [ -n "${ENV_VALUE}" ]; then
151        export ${ENV_VALUE?}
152    fi
153    ${RUNNER_VALUE} ${RUNNER_ARGS_VALUE} ${SCRIPT_VALUE} ${SCRIPT_ARGS_VALUE} || ret_code=$?
154
155    if [ ! ${ret_code} -eq ${RET_CODE_VALUE} ]; then
156        report_error "Unexpected error code returned! Expected: ${RET_CODE_VALUE} vs actual: ${ret_code}"
157    fi
158}
159
160main "$@"
161