mirror of
https://git.proxmox.com/git/mirror_lxc
synced 2025-04-29 15:35:15 +00:00
Refactor __lxc_check_name_present()
.
Print name of container found, if any. Signed-off-by: Edênis Freindorfer Azevedo <edenisfa@gmail.com>
This commit is contained in:
parent
c227466673
commit
b3dcb19407
@ -38,20 +38,31 @@ __lxc_names() {
|
|||||||
__lxc_check_name_present() {
|
__lxc_check_name_present() {
|
||||||
mapfile -t names < <(command lxc-ls -1)
|
mapfile -t names < <(command lxc-ls -1)
|
||||||
local -r shortoptnamexp="^-[0-9A-Za-mo-z]*n[0-9A-Za-mo-z]*$"
|
local -r shortoptnamexp="^-[0-9A-Za-mo-z]*n[0-9A-Za-mo-z]*$"
|
||||||
|
local container
|
||||||
|
local param
|
||||||
local parsed
|
local parsed
|
||||||
|
# Current word on command line, minus double/single quote and backslash.
|
||||||
local -r current="${cur//[\\\"\']}"
|
local -r current="${cur//[\\\"\']}"
|
||||||
mapfile -t names < <(command lxc-ls -1)
|
# If `--name` or `-n` are present, then a container name should be available.
|
||||||
# If `--name` or `-n` are present, do not complete with container names.
|
for i in "${!words[@]}"; do
|
||||||
for param in "${words[@]}"; do
|
param="${words[${i}]}"
|
||||||
# Parse names from command line when space is escaped by backslash.
|
# Parse names from command line when space is escaped by backslash.
|
||||||
parsed="${param//[\\\"\']}"
|
parsed="${param//[\\\"\']}"
|
||||||
if [[ ${parsed} =~ ^--name(=(.*))?$ ]]; then
|
if [[ ${parsed} =~ ^--name(=(.*))?$ ]]; then
|
||||||
|
if [[ -n "${BASH_REMATCH[2]}" ]]; then
|
||||||
|
container="${BASH_REMATCH[2]}"
|
||||||
|
else
|
||||||
|
container="${words[${i}+1]}"
|
||||||
|
fi
|
||||||
|
command printf "%q" "${container}"
|
||||||
return 0
|
return 0
|
||||||
elif [[ ${parsed} =~ ${shortoptnamexp} ]]; then
|
elif [[ ${parsed} =~ ${shortoptnamexp} ]]; then
|
||||||
|
command printf "%q" "${words[${i}+1]}"
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
for name in "${names[@]}"; do
|
for name in "${names[@]}"; do
|
||||||
if [[ "${parsed}" == "${name}" ]] && [[ "${current}" != "${parsed}" ]]; then
|
if [[ "${parsed}" == "${name}" ]] && [[ "${current}" != "${parsed}" ]]; then
|
||||||
|
command printf "%q" "${name}"
|
||||||
return 0
|
return 0
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
@ -60,39 +71,15 @@ __lxc_check_name_present() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
__lxc_append_name() {
|
__lxc_append_name() {
|
||||||
if ! __lxc_check_name_present; then
|
local -r name=$(__lxc_check_name_present)
|
||||||
|
if [[ -z "${name}" ]]; then
|
||||||
__lxc_names
|
__lxc_names
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
__lxc_get_snapshots() {
|
__lxc_get_snapshots() {
|
||||||
mapfile -t names < <(command lxc-ls -1)
|
local -r container=$(__lxc_check_name_present)
|
||||||
local -r shortoptnamexp="^-[0-9A-Za-mo-z]*n[0-9A-Za-mo-z]*$"
|
[[ -z "${container}" ]] && return
|
||||||
local container
|
|
||||||
local param
|
|
||||||
for i in "${!words[@]}"; do
|
|
||||||
param="${words[${i}]}"
|
|
||||||
parsed="${param//[\\\"\']}"
|
|
||||||
if [[ ${param} =~ ^--name(=(.*))?$ ]]; then
|
|
||||||
if [[ -n "${BASH_REMATCH[2]}" ]]; then
|
|
||||||
container="${BASH_REMATCH[2]}"
|
|
||||||
else
|
|
||||||
container="${words[${i}+1]}"
|
|
||||||
fi
|
|
||||||
break
|
|
||||||
elif [[ ${param} =~ ${shortoptnamexp} ]]; then
|
|
||||||
container="${words[${i}+1]}"
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
for name in "${names[@]}"; do
|
|
||||||
if [[ "${parsed}" == "${name}" ]]; then
|
|
||||||
container="${name}"
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
[[ -n "${container}" ]] && break
|
|
||||||
done
|
|
||||||
container="${container//[\\\"\']}"
|
|
||||||
mapfile -t snaps < <(command lxc-snapshot --name="${container}" --list)
|
mapfile -t snaps < <(command lxc-snapshot --name="${container}" --list)
|
||||||
local -r nosnapxp="^No snapshots$"
|
local -r nosnapxp="^No snapshots$"
|
||||||
if [[ ! "${snaps[*]}" =~ ${nosnapxp} ]]; then
|
if [[ ! "${snaps[*]}" =~ ${nosnapxp} ]]; then
|
||||||
|
Loading…
Reference in New Issue
Block a user