Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make go-xcat pass ShellCheck #6367

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 39 additions & 36 deletions xCAT-server/share/xcat/tools/go-xcat
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
function usage()
{
local script="${0##*/}"
local version="$(version)"
local -r version="$(version)"

while read -r ; do echo "${REPLY}" ; done <<-EOF
${script} version ${version}
Expand Down Expand Up @@ -81,7 +81,7 @@ function usage()
#
# verbose_usage This function be will be called when user run
# `go-xcat --long-help'.
# Including a bunch of secert usage.
# Including a bunch of secret usage.
#
function verbose_usage()
(
Expand All @@ -94,8 +94,7 @@ function verbose_usage()
local -i i
for (( i = 0 ; i < "$1" ; ++i ))
do
read -r -u 42
[[ "$?" -ne "0" ]] && break
read -r -u 42 || break
echo "${REPLY}"
done
}
Expand All @@ -110,7 +109,7 @@ function verbose_usage()
while read -r ; do echo "${REPLY}" ; done <<-EOF
check check the version of the installed packages
of xCAT and packages in the repository
smoke test preform basic tests of the xCAT installation
smoke test perform basic tests of the xCAT installation
EOF
println 11
while read -r ; do echo "${REPLY}" ; done <<-EOF
Expand Down Expand Up @@ -201,7 +200,7 @@ function warn_if_bad()

# Broken
shift
echo "${script}: $@" >&2
echo "${script}: $*" >&2
return "${rc}"
}

Expand Down Expand Up @@ -490,17 +489,17 @@ function check_arch()

function check_linux_distro()
{
local distro="$(source /etc/os-release >/dev/null 2>&1 &&
echo "${ID}")"
local distro
distro="$(awk -F= '/^ID=/{print $2}' /etc/os-release 2>/dev/null)"
[[ -z "${distro}" && -f /etc/redhat-release ]] && distro="rhel"
[[ -z "${distro}" && -f /etc/SuSE-release ]] && distro="sles"
echo "${distro}"
}

function check_linux_version()
{
local ver="$(source /etc/os-release >/dev/null 2>&1 &&
echo "${VERSION_ID}")"
local ver
ver="$(awk -F= '/^VERSION_ID=/{print $2}' /etc/os-release 2>/dev/null)"
[[ -z "${ver}" && -f /etc/redhat-release ]] &&
# Need gawk to do this trick
ver="$(awk '{ match($0, /([.0-9]+)/, a); print substr($0, a[1, "start"], a[1, "length"]); }' \
Expand Down Expand Up @@ -581,7 +580,7 @@ function check_package_version_deb()

function check_package_version()
{
function_dispatch "${FUNCNAME}" "$@"
function_dispatch "${FUNCNAME[0]}" "$@"
}

# $@ package names
Expand Down Expand Up @@ -679,7 +678,11 @@ function check_repo_version_zypper()
function check_repo_version_apt()
{
type apt-cache >/dev/null 2>&1 || return 255
# Shellcheck doesn't handle identical names in local contexts
# well: https://github.com/koalaman/shellcheck/wiki/SC2178#exceptions
# shellcheck disable=SC2178
local name=""
# shellcheck disable=SC2178
local ver=""
while read -r name ver
do
Expand Down Expand Up @@ -708,7 +711,7 @@ function check_repo_version_apt()

function check_repo_version()
{
function_dispatch "${FUNCNAME}" "$@"
function_dispatch "${FUNCNAME[0]}" "$@"
}

# $1 repo id
Expand Down Expand Up @@ -783,13 +786,13 @@ function get_package_list_apt()
# $1 repo id
function get_package_list()
{
function_dispatch "${FUNCNAME}" "$@"
function_dispatch "${FUNCNAME[0]}" "$@"
}

function download_file_curl()
{
local script="${0##*/}"
local version="$(version)"
local -r version="$(version)"
local user_agent="${script}/${version} (${GO_XCAT_OS}; ${GO_XCAT_ARCH}; ${GO_XCAT_LINUX_DISTRO} ${GO_XCAT_LINUX_VERSION})"
type curl >/dev/null 2>&1 || return 255
local url="$1"
Expand Down Expand Up @@ -896,7 +899,7 @@ function download_file_curl()
function download_file_wget()
{
local script="${0##*/}"
local version="$(version)"
local -r version="$(version)"
local user_agent="${script}/${version} (${GO_XCAT_OS}; ${GO_XCAT_ARCH}; ${GO_XCAT_LINUX_DISTRO} ${GO_XCAT_LINUX_VERSION})"
type wget >/dev/null 2>&1 || return 255
local url="$1"
Expand Down Expand Up @@ -928,7 +931,7 @@ function download_file_wget()

function download_file()
{
function_dispatch "${FUNCNAME}" "$@"
function_dispatch "${FUNCNAME[0]}" "$@"
}

# $1 repo file
Expand Down Expand Up @@ -1026,7 +1029,7 @@ function add_repo_by_file_apt()

function add_repo_by_file()
{
function_dispatch "${FUNCNAME}" "$@"
function_dispatch "${FUNCNAME[0]}" "$@"
}

# $1 archive
Expand All @@ -1037,7 +1040,7 @@ function extract_archive()
local archive="$1"
local repo_id="$2"
local install_path="$3"
local umask="$(umask)"
local -r umask="$(umask)"
local -i ret=0

[[ -f "${archive}" ]]
Expand All @@ -1057,7 +1060,7 @@ function extract_archive()
[[ "${PIPESTATUS[0]}" -eq 0 && "${PIPESTATUS[1]}" -eq 0 &&
"${PIPESTATUS[2]}" -eq 1 ]]
warn_if_bad "$?" "${archive}: bad compressed tarball" || return 1
rm -rf "${install_path}/${repo_id}"
rm -rf "${install_path:-${GO_XCAT_DEFAULT_INSTALL_PATH}}/${repo_id}"
uncompress -c "${archive}" | ( cd "${install_path}" && tar -x -f - )
;;
*".tz"|*".tgz"|*".tar.gz")
Expand All @@ -1066,7 +1069,7 @@ function extract_archive()
[[ "${PIPESTATUS[0]}" -eq 0 && "${PIPESTATUS[1]}" -eq 0 &&
"${PIPESTATUS[2]}" -eq 1 ]]
exit_if_bad "$?" "${archive}: bad gzipped tarball" || return 1
rm -rf "${install_path}/${repo_id}"
rm -rf "${install_path:-${GO_XCAT_DEFAULT_INSTALL_PATH}}/${repo_id}"
gzip -d -c "${archive}" | ( cd "${install_path}" && tar -x -f - )
;;
*".tbz"|*".tbz2"|*".tar.bz"|*".tar.bz2")
Expand All @@ -1075,7 +1078,7 @@ function extract_archive()
[[ "${PIPESTATUS[0]}" -eq 0 && "${PIPESTATUS[1]}" -eq 0 &&
"${PIPESTATUS[2]}" -eq 1 ]]
warn_if_bad "$?" "${archive}: bad bzipped tarball" || return 1
rm -rf "${install_path}/${repo_id}"
rm -rf "${install_path:-${GO_XCAT_DEFAULT_INSTALL_PATH}}/${repo_id}"
bzip2 -d -c "${archive}" | ( cd "${install_path}" && tar -x -f - )
;;
*".txz"|*".tar.xz")
Expand All @@ -1084,15 +1087,15 @@ function extract_archive()
[[ "${PIPESTATUS[0]}" -eq 0 && "${PIPESTATUS[1]}" -eq 0 &&
"${PIPESTATUS[2]}" -eq 1 ]]
warn_if_bad "$?" "${archive}: bad xzed tarball" || return 1
rm -rf "${install_path}/${repo_id}"
rm -rf "${install_path:-${GO_XCAT_DEFAULT_INSTALL_PATH}}/${repo_id}"
xz -d -c "${archive}" | ( cd "${install_path}" && tar -x -f - )
;;
*".tar")
check_executes tar grep || return 1
tar -t -f "${archive}" | grep -v "^${repo_id}/"
[[ "${PIPESTATUS[0]}" -eq 0 && "${PIPESTATUS[1]}" -eq 1 ]]
warn_if_bad "$?" "${archive}: bad tarball" || return 1
rm -rf "${install_path}/${repo_id}"
rm -rf "${install_path:-${GO_XCAT_DEFAULT_INSTALL_PATH}}/${repo_id}"
( cd "${install_path}" && tar -x -f - ) <"${archive}"
;;
*)
Expand Down Expand Up @@ -1197,8 +1200,7 @@ function add_repo_by_url_apt()
local repo_id="$2"
local tmp=""
local install_path="${GO_XCAT_DEFAULT_INSTALL_PATH}"
local codename="$(source /etc/lsb-release >/dev/null 2>&1 &&
echo "${DISTRIB_CODENAME}")"
local -r codename="$(awk -F= '/^DISTRIB_CODENAME=/{print $2}' /etc/lsb-release 2>/dev/null)"
[[ -n "${codename}" ]]
warn_if_bad "$?" "unknown debian/ubuntu codename" || return 1
case "${url%%://*}" in
Expand Down Expand Up @@ -1248,7 +1250,7 @@ function add_repo_by_url_apt()

function add_repo_by_url()
{
function_dispatch "${FUNCNAME}" "$@"
function_dispatch "${FUNCNAME[0]}" "$@"
}

# $1 repo id
Expand All @@ -1259,7 +1261,7 @@ function remove_repo_yum()
# This deleting method is not good enough. Since there could be more
# than one repository definitions in a single repo file.
# This is a quick and dirty method.
rm -f $(grep -l "^\[${repo_id}\]$" "/etc/yum.repos.d/"*".repo" 2>/dev/null)
grep -l "^\[${repo_id}\]$" "/etc/yum.repos.d/"*".repo" 2>/dev/null | xargs -r rm -f
case "${repo_id}" in
"xcat-core")
mv /etc/yum.repos.d/xCAT-core.repo{,.nouse} 2>/dev/null
Expand Down Expand Up @@ -1299,7 +1301,7 @@ function remove_repo_apt()

function remove_repo()
{
function_dispatch "${FUNCNAME}" "$@"
function_dispatch "${FUNCNAME[0]}" "$@"
}

# $1 URL
Expand Down Expand Up @@ -1363,7 +1365,7 @@ function add_xcat_core_repo_apt()

function add_xcat_core_repo()
{
function_dispatch "${FUNCNAME}" "$@"
function_dispatch "${FUNCNAME[0]}" "$@"
}

function add_xcat_dep_repo_yum_or_zypper()
Expand Down Expand Up @@ -1449,7 +1451,7 @@ function add_xcat_dep_repo_apt()

function add_xcat_dep_repo()
{
function_dispatch "${FUNCNAME}" "$@"
function_dispatch "${FUNCNAME[0]}" "$@"
}

function update_repo_dnf()
Expand Down Expand Up @@ -1483,7 +1485,7 @@ function update_repo_apt()

function update_repo()
{
function_dispatch "${FUNCNAME}" "$@"
function_dispatch "${FUNCNAME[0]}" "$@"
}

function install_packages_dnf()
Expand Down Expand Up @@ -1530,7 +1532,7 @@ function install_packages_apt()

function install_packages()
{
function_dispatch "${FUNCNAME}" "$@"
function_dispatch "${FUNCNAME[0]}" "$@"
}

function remove_package_dnf()
Expand Down Expand Up @@ -1576,7 +1578,7 @@ function remove_package_apt()

function remove_package()
{
function_dispatch "${FUNCNAME}" "$@"
function_dispatch "${FUNCNAME[0]}" "$@"
}

# $1 -y
Expand All @@ -1596,7 +1598,7 @@ function purge_package_others()
# $1 -y
function purge_package()
{
function_dispatch "${FUNCNAME}" "$@"
function_dispatch "${FUNCNAME[0]}" "$@"
}

# $1 -y
Expand Down Expand Up @@ -1705,7 +1707,8 @@ function list_xcat_packages()
[ "${#GO_XCAT_DEP_PACKAGE_LIST[@]}" -gt "0" ]
warn_if_bad "$?" "Failed to get package list from repository \`xcat-dep'." || return 1

local -i cols="$(type tput >/dev/null 2>&1 && tput cols)"
local -i cols
cols="$(type tput >/dev/null 2>&1 && tput cols)"
[[ "${cols}" -lt 80 ]] && cols=80
[[ "${cols}" -gt 90 ]] && cols=90
[[ -t 1 ]] || cols=90
Expand Down Expand Up @@ -1848,7 +1851,7 @@ function test_case_001_xcatd()
# Check if command lsdef can be run
function test_case_002_lsdef()
{
(source /etc/profile.d/xcat.sh && lsdef) >/dev/null 2>&1
bash -c "source /etc/profile.d/xcat.sh && lsdef" >/dev/null 2>&1
warn_if_bad "$?" "Attempt of run \`lsdef' failed"
}

Expand Down