question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New parser: update-alternatives --query

See original GitHub issue

The 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:closed
  • Created a year ago
  • Comments:29 (29 by maintainers)

github_iconTop GitHub Comments

1reaction
chriscroomecommented, Apr 27, 2022

The Ansible role I was planning to use to test the jc JSON update-alternatives --query output doesn’t seem to work for alternatives with lots of slaves like aptitude and editor and I won’t have time to look at this more until next week sometime – however I can’t see any jc bugs 😃

1reaction
kellyjonbrazilcommented, Apr 27, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found