mirror of
https://git.proxmox.com/git/mirror_lxc
synced 2025-07-28 09:32:49 +00:00
Fix lxc-cgroup
smart completion.
Also make bash function more readable for itself. Signed-off-by: Edênis Freindorfer Azevedo <edenisfa@gmail.com>
This commit is contained in:
parent
b3dcb19407
commit
d9be2feb09
@ -166,9 +166,11 @@ __lxc_check_word_in_array() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
__lxc_piped_args() {
|
__lxc_piped_args() {
|
||||||
|
local -r currentWord="${1}"
|
||||||
local -r sep="${2}"
|
local -r sep="${2}"
|
||||||
|
declare -a completionWords=("${@:3}")
|
||||||
# Remove double/single quote and backslash from current completion parameter.
|
# Remove double/single quote and backslash from current completion parameter.
|
||||||
IFS=$"${sep}" read -r -e -a current <<< "${1//[\\\"\']}"
|
IFS=$"${sep}" read -r -e -a current <<< "${currentWord//[\\\"\']}"
|
||||||
|
|
||||||
# Add separator back to current in case it is part of completion list.
|
# Add separator back to current in case it is part of completion list.
|
||||||
for i in "${!current[@]}"; do
|
for i in "${!current[@]}"; do
|
||||||
@ -179,7 +181,7 @@ __lxc_piped_args() {
|
|||||||
|
|
||||||
# Remove words from completion already added to argument.
|
# Remove words from completion already added to argument.
|
||||||
declare -a minuslast=("${current[@]::${#current[@]}-1}")
|
declare -a minuslast=("${current[@]::${#current[@]}-1}")
|
||||||
declare -a completion=("${@:3}")
|
declare -a completion=("${completionWords[@]}")
|
||||||
for i in "${!completion[@]}"; do
|
for i in "${!completion[@]}"; do
|
||||||
if __lxc_check_word_in_array "${completion[${i}]}" "${minuslast[@]}"; then
|
if __lxc_check_word_in_array "${completion[${i}]}" "${minuslast[@]}"; then
|
||||||
command unset -v 'completion[${i}]'
|
command unset -v 'completion[${i}]'
|
||||||
@ -191,7 +193,7 @@ __lxc_piped_args() {
|
|||||||
if __lxc_array_has_duplicates "${minuslast[@]}"; then
|
if __lxc_array_has_duplicates "${minuslast[@]}"; then
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
declare -a allcomps=("${@:3}")
|
declare -a allcomps=("${completionWords[@]}")
|
||||||
for i in "${!minuslast[@]}"; do
|
for i in "${!minuslast[@]}"; do
|
||||||
if ! __lxc_check_word_in_array "${minuslast[${i}]}" "${allcomps[@]}"; then
|
if ! __lxc_check_word_in_array "${minuslast[${i}]}" "${allcomps[@]}"; then
|
||||||
return
|
return
|
||||||
@ -212,7 +214,7 @@ __lxc_piped_args() {
|
|||||||
fi
|
fi
|
||||||
# TAB after quotes to complete for next value.
|
# TAB after quotes to complete for next value.
|
||||||
if ! __lxc_array_has_duplicates "${current[@]}" && __lxc_check_word_in_array "${lastword}" "${allcomps[@]}"; then
|
if ! __lxc_array_has_duplicates "${current[@]}" && __lxc_check_word_in_array "${lastword}" "${allcomps[@]}"; then
|
||||||
if ! __lxc_check_completion_avail "${lastword}" "${completion[@]}" || [[ "${#1}" -lt "${#sep}" ]] || [[ "${1: -${#sep}}" == "${sep}" ]]; then
|
if ! __lxc_check_completion_avail "${lastword}" "${completion[@]}" || [[ "${#currentWord}" -lt "${#sep}" ]] || [[ "${currentWord: -${#sep}}" == "${sep}" ]]; then
|
||||||
prefix=$(__lxc_concat_array_sep "${sep}" "${current[@]}")
|
prefix=$(__lxc_concat_array_sep "${sep}" "${current[@]}")
|
||||||
for comp in "${completion[@]}"; do
|
for comp in "${completion[@]}"; do
|
||||||
[[ "${comp}" == "${lastword}" ]] && continue
|
[[ "${comp}" == "${lastword}" ]] && continue
|
||||||
@ -340,7 +342,7 @@ __lxc_get_groups() {
|
|||||||
line=$(command echo -e "${line}" | command sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
|
line=$(command echo -e "${line}" | command sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')
|
||||||
IFS=$',' read -r -e -a linegroups <<< "${line}"
|
IFS=$',' read -r -e -a linegroups <<< "${line}"
|
||||||
for entry in "${linegroups[@]}"; do
|
for entry in "${linegroups[@]}"; do
|
||||||
key=$(printf "%q" "${entry}")
|
key=$(command printf "%q" "${entry}")
|
||||||
groups+=(["${key}"]=1)
|
groups+=(["${key}"]=1)
|
||||||
done
|
done
|
||||||
done
|
done
|
||||||
@ -385,7 +387,7 @@ __lxc_cgroup_v2() {
|
|||||||
local -r path="${1}"
|
local -r path="${1}"
|
||||||
[[ ! -f "${path}/cgroup.controllers" ]] && return
|
[[ ! -f "${path}/cgroup.controllers" ]] && return
|
||||||
for controller in $(<"${path}/cgroup.controllers"); do
|
for controller in $(<"${path}/cgroup.controllers"); do
|
||||||
for so in ${path}/${controller}.*; do
|
for so in "${path}/${controller}".*; do
|
||||||
[[ ! -f "${so}" ]] && continue
|
[[ ! -f "${so}" ]] && continue
|
||||||
stateObjects+=("${so##*/}")
|
stateObjects+=("${so##*/}")
|
||||||
done
|
done
|
||||||
@ -411,20 +413,30 @@ __lxc_cgroup_v1() {
|
|||||||
__lxc_cgroup_state_object() {
|
__lxc_cgroup_state_object() {
|
||||||
local -r name="${1}"
|
local -r name="${1}"
|
||||||
local -r cgroupPath="/sys/fs/cgroup"
|
local -r cgroupPath="/sys/fs/cgroup"
|
||||||
# cgroup_v2 + user.slice
|
local output
|
||||||
local -r userSlicePath="${cgroupPath}/user.slice"
|
local -r userSlicePath="${cgroupPath}/user.slice"
|
||||||
|
local -r lxcPayloadPath="${cgroupPath}/lxc.payload.${name}"
|
||||||
if [[ -d "${userSlicePath}" ]]; then
|
if [[ -d "${userSlicePath}" ]]; then
|
||||||
COMPREPLY=( $( compgen -W "$(__lxc_cgroup_v2 "${userSlicePath}")" -- "${cur}" ) )
|
# cgroup_v2 + user.slice
|
||||||
return
|
read -r -e -a output <<< $(__lxc_cgroup_v2 "${userSlicePath}")
|
||||||
fi
|
elif [[ -d "${lxcPayloadPath}" ]]; then
|
||||||
# cgroup_v2 + lxc.payload
|
# cgroup_v2 + lxc.payload
|
||||||
local -r lxcCgroup="${cgroupPath}/lxc.payload.${name}"
|
read -r -e -a output <<< $(__lxc_cgroup_v2 "${lxcPayloadPath}")
|
||||||
if [[ -d "${lxcCgroup}" ]]; then
|
else
|
||||||
COMPREPLY=( $( compgen -W "$(__lxc_cgroup_v2 "${lxcCgroup}")" -- "${cur}" ) )
|
# cgroup_v1
|
||||||
|
read -r -e -a output <<< $(__lxc_cgroup_v1 "${cgroupPath}")
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Check if state-object is present already.
|
||||||
|
for w in "${words[@]}"; do
|
||||||
|
if [[ "${cur}" != "${w}" ]] && __lxc_check_word_in_array "${w}" "${output[@]}"; then
|
||||||
|
return
|
||||||
|
elif [[ "${cur}" == "${w}" ]] && ! __lxc_check_completion_avail "${w}" "${output[@]}"; then
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
# cgroup_v1
|
done
|
||||||
COMPREPLY=( $( compgen -W "$(__lxc_cgroup_v1 "${cgroupPath}")" -- "${cur}" ) )
|
|
||||||
|
COMPREPLY=( $( compgen -W "${output[*]}" -- "${cur}" ) )
|
||||||
}
|
}
|
||||||
|
|
||||||
_lxc_cgroup() {
|
_lxc_cgroup() {
|
||||||
@ -453,8 +465,9 @@ _lxc_cgroup() {
|
|||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if __lxc_check_name_present; then
|
local -r custom=$(__lxc_check_name_present)
|
||||||
__lxc_cgroup_state_object
|
if [[ -n "${custom}" ]]; then
|
||||||
|
__lxc_cgroup_state_object "${custom}"
|
||||||
else
|
else
|
||||||
__lxc_append_name
|
__lxc_append_name
|
||||||
fi
|
fi
|
||||||
|
Loading…
Reference in New Issue
Block a user