New parser: update-alternatives --query
See original GitHub issueThe Debian alternatives system has a update-alternatives
command which has a --query
option which will:
Display information about the link group like
--display
does, but in a machine parseable way (since version 1.15.0, see section QUERY FORMAT below).
For example for the editor
(omitting the Slaves output):
update-alternatives --query editor
Name: editor
Link: /usr/bin/editor
Status: manual
Best: /bin/nano
Value: /usr/bin/vim.basic
Alternative: /bin/nano
Priority: 40
Alternative: /usr/bin/nvim
Priority: 30
Alternative: /usr/bin/vim.basic
Priority: 30
Alternative: /usr/bin/vim.tiny
Priority: 15
Perhaps this might be a useful new parser for jc
, the Ansible alternatives module is only able to set alternatives, not read them, so I’ve written some Bash to use as a Ansible facts.d
script for converting the output into JSON:
#!/usr/bin/env bash
set -euo pipefail
jo $(
echo "${1}"=$(
if query=$(update-alternatives --query "${1}")
then
declare -a alternatives=()
readarray -t alternatives < <(grep -e '^Alternative' <<< "${query}" | sed 's/Alternative: //')
declare -a priorities=()
readarray -t priorities < <(grep -e '^Priority' <<< "${query}" | sed 's/Priority: //')
jo name="${1}" link=$(
grep ^Link <<< "${query}" | sed 's/^Link: //'
) value=$(
grep ^Value <<< "${query}" | sed 's/^Value: //'
) best=$(
grep ^Best <<< "${query}" | sed 's/Best: //'
) alternatives=$(
jo -a $(
i=0
while [[ "${i}" -lt "${#alternatives[@]}" ]]
do
jo path="${alternatives[${i}]}" priority="${priorities[${i}]}"
((i++))
done
)
)
else
jo state=absent
fi
)
)
Running this script with an argument, for example editor
results in:
{
"editor": {
"name": "editor",
"link": "/usr/bin/editor",
"value": "/usr/bin/vim.basic",
"best": "/bin/nano",
"alternatives": [
{
"path": "/bin/nano",
"priority": 40
},
{
"path": "/usr/bin/nvim",
"priority": 30
},
{
"path": "/usr/bin/vim.basic",
"priority": 30
},
{
"path": "/usr/bin/vim.tiny",
"priority": 15
}
]
}
}
Issue Analytics
- State:
- Created a year ago
- Comments:29 (29 by maintainers)
Top Results From Across the Web
update-alternatives(1) - Linux manual page - man7.org
update -alternatives creates, removes, maintains and displays information about the symbolic links comprising the Debian alternatives system.
Read more >update-alternatives - maintain symbolic links determining ...
update -alternatives creates, removes, maintains and displays information about the symbolic links comprising the Debian alternatives system.
Read more >Better way to add alternative using update-alternatives?
To answer your first question I'd like to hint you to --query : --query name Display information about the link group like --display...
Read more >Show Raw Contents of update-alternatives - Ask Ubuntu
I'm wondering if there's a way to show the raw contents of sudo update-alternatives --config <resource> ? I've been doing quite a lot...
Read more >How to use the command update-alternatives --config java
When using the --config option, alternatives will list all of the choices for the link group of which given name is the master...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
The Ansible role I was planning to use to test the
jc
JSONupdate-alternatives --query
output doesn’t seem to work for alternatives with lots of slaves likeaptitude
andeditor
and I won’t have time to look at this more until next week sometime – however I can’t see anyjc
bugs 😃Nice! Let me know if you run into any other issues. I’m packaging v1.18.8 now. It’s already on PIP and the binaries should be in github Releases soon.
It would be possible to output YAML in addition to JSON. I might think about that for v1.19.0. Maybe open a feature request issue so we can track it.