refactor: use _comp_compgen -C for service name generation#1553
refactor: use _comp_compgen -C for service name generation#1553akinomyoga wants to merge 4 commits intoscop:mainfrom
_comp_compgen -C for service name generation#1553Conversation
| _comp_compgen -a split -l -- "$(initctl list 2>/dev/null | cut -d' ' -f1)" | ||
| fi | ||
|
|
||
| ((${#services[@]})) || return 1 |
There was a problem hiding this comment.
On a quick skim it seems removing this changes the behavior of _comp_compgen_services so that it no longer returns 1 when there are no services found. We don't seem to rely on that behavior in any in-tree uses, but should we not preserve it as the function is part of the API?
There was a problem hiding this comment.
OK, I checked the uses of this function inside bash-completion, but I didn't care about the possibility of external dependencies.
I updated it: c4c79ad..a02d487.
bash_completion
Outdated
| } 2>/dev/null | | ||
| _comp_awk '$1 ~ /\.service$/ { sub("\\.service$", "", $1); print $1 }') | ||
| _comp_split -la services "$_generated" | ||
| _comp_awk 'sub(/\.service$/, "", $1) { print $1 }') |
There was a problem hiding this comment.
I think simple edits like this are usually done using sed instead of awk, maybe do that here?
There was a problem hiding this comment.
The non-trivial part is the extraction of $1. We may still implement it in sed as follows, but I feel it's worse.
| _comp_awk 'sub(/\.service$/, "", $1) { print $1 }') | |
| command sed -n 's/^[[:blank:]]*\([^[:blank:]]\{1,\}\)\.service\([[:blank:]].*\)\{0,1\}$/\1/p' |
Another option is to use both, but I'd rather do it in a single invocation of awk.
| _comp_awk 'sub(/\.service$/, "", $1) { print $1 }') | |
| _comp_awk '{print $1}' | command sed -n 's/\.service$//p' |
However, I noticed in the output of systemctl list-units --full --all in my system that the service name isn't necessarily included as $1. Running services are prefixed by ●. For example,
$ systemctl list-units --full --all | grep -F iptables.service
● iptables.service not-found inactive dead iptables.serviceThe current implementation fails to extract those lines.
Alternative implementations are ...
| _comp_awk 'sub(/\.service$/, "", $1) { print $1 }') | |
| _comp_awk '{ for (i = 1; i <= 2; i++) if (sub(/\.service$/, "", $i)) { print $i; next } }') |
| _comp_awk 'sub(/\.service$/, "", $1) { print $1 }') | |
| _comp_awk '{ if (sub(/\.service$/, "", $1)) print $1; else if (sub(/\.service$/, "", $2)) print $2 }') |
| _comp_awk 'sub(/\.service$/, "", $1) { print $1 }') | |
| _comp_awk 'sub(/\.service$/, "", $1) { print $1; next } sub(/\.service$/, "", $2) { print $2 }') |
| _comp_awk 'sub(/\.service$/, "", $1) { print $1 }') | |
| command sed -n 's/^\([^[:blank:]]\{1,\}[[:blank:]]\)\{0,1\}[[:blank:]]*\([^[:blank:]]\{1,\}\)\.service\([[:blank:]].*\)\{0,1\}$/\2/p') |
| _comp_awk 'sub(/\.service$/, "", $1) { print $1 }') | |
| _comp_awk 'sub(/\.service([[:blank:]].*)?$/, "") && NF <= 2 { print $NF }') |
There was a problem hiding this comment.
I decided to use this: a02d487..8082cf8
8082cf8 to
809b7db
Compare
Recent versions of systemctl seems to prefix a character representing the status before the unit names. Thus, in addition to $1, $2 may also contain a service name.
5e6dc86 and 655371b use
_comp_compgen -C "$sysvdir" -- -finstead of first generating filenames with"$svsvdir"/*and stripping the parent directory name by"{tmp[@]#$sysvdir/}".e21dbb8 and 354f9c5 are additional refactoring.