freebsd / pkg

Package management tool for FreeBSD. Help at #pkg on Libera Chat or pkg@FreeBSD.org
Other
739 stars 277 forks source link

How to list FreeBSD pkg(8) repositories? #2134

Open vermaden opened 1 year ago

vermaden commented 1 year ago

Hi.

I have added some additional repository to FreeBSD system.

What is the pkg(8) command to list repositories with their priorities?

Something like yum reposlist on a Linux system as an example for what I am looking in pkg(8) command.

# yum repolist
repo id         repo name
appstream       CentOS Linux 8 - AppStream
baseos          CentOS Linux 8 - BaseOS
epel            Extra Packages for Enterprise Linux 8 - x86_64
epel-modular    Extra Packages for Enterprise Linux Modular 8 - x86_64
extras          CentOS Linux 8 - Extras

Thanks in advance.

Regards, vermaden

vermaden commented 1 year ago

This is the closest I was able to reach with some simple scripting:

echo
(
  echo "REPO PRIORITY"
  for REPO in /etc/pkg/* /usr/local/etc/pkg/repos/*
  do
    if grep -q -E 'enabled:[\ \t]*yes' "${REPO}"
    then
      awk -F ':' '/\{[\ \t]*$/ {printf("%s ",$1)}' "${REPO}"
      if grep -q priority "${REPO}"
      then
        awk '/priority:/ {print $NF}' "${REPO}"
      else
        echo 'DEFAULT'
      fi
    fi
  done
) | column -t

REPO     PRIORITY
FreeBSD  DEFAULT
Custom   100

Not sure this is the way this information should be gained ...

Regards, vermaden

arrowd commented 1 year ago

The libpkg API does support this:

    pkg_repo* repo = NULL;
    while (pkg_repos(&repo) == EPKG_OK) {
        const char* id = pkg_repo_name (repo);
        const char* descr = pkg_repo_url (repo);
        bool enabled = pkg_repo_enabled (repo);

but I don't know if any of pkg subcommands reports these.

vermaden commented 1 year ago

Thank You for suggestion. My C is basic to say the least so I came up with what I know best - POSIX /bin/sh scripts - can be also used as functions if someone prefers them that way. I have made two variants.

SHOW ALL pkg(8) REPOS WITH STATUS

% ./pkg-repo-var-en.sh 
REPO     ENABLED  PRIO  URL
FreeBSD  yes      0     pkg+http://pkg.FreeBSD.org/${ABI}/latest
Custom   no       10    http://10.0.0.9/packages/Custom

pkg-repo-var-en.sh

#! /bin/sh

( # HEADER
  echo -e "\nREPO ENABLED PRIO URL"
  for REPO in /etc/pkg/* /usr/local/etc/pkg/repos/*
  do
    REPOCUR=$( grep '^[^#]' "${REPO}" )

    # REPO
    echo "${REPOCUR}" | awk -F ':' '/\{[\ \t]*$/ {printf(" %s ",$1)}'

    # ENABLED
    echo "${REPOCUR}" | awk '/enabled:/ {printf(" %s ",$NF)}' | tr -cd '[a-zA-Z ]'

    # PRIO
    if echo "${REPOCUR}" | grep -q priority
    then
      echo "${REPOCUR}" | awk '/priority:/ {printf(" %s ",$NF)}' | tr -cd '[0-9 ]'
    else
      echo -n " 0 "
    fi

    # URL
    echo "${REPOCUR}" | grep '^[^#]' | awk -F'"' '/url:/ {print $2}' | tr -d ','

  done 2> /dev/null
) | column -t 2> /dev/null

SHOW ONLY ENABLED pkg(8) REPOS

% ./pkg-repo-var.sh
REPO     PRIO  URL
FreeBSD  0     pkg+http://pkg.FreeBSD.org/${ABI}/latest

pkg-repo-var.sh

#! /bin/sh

( # HEADER
  echo -e "\nREPO PRIO URL"
  for REPO in /etc/pkg/* /usr/local/etc/pkg/repos/*
  do
    REPOCUR=$( grep '^[^#]' "${REPO}" )

    # CHECK IF ENABLED
    if echo "${REPOCUR}" | grep -q -E 'enabled:[\ \t]*yes'
    then

      # REPO
      echo "${REPOCUR}" | awk -F ':' '/\{[\ \t]*$/ {printf(" %s ",$1)}'

      # PRIO
      if echo "${REPOCUR}" | grep -q priority
      then
        echo "${REPOCUR}" | awk '/priority:/ {printf(" %s ",$NF)}'
      else
        echo -n " 0 "
      fi

      # URL
      echo "${REPOCUR}" | grep '^[^#]' | awk -F'"' '/url:/ {print $2}'
    fi

  done 2> /dev/null
) | column -t 2> /dev/null

Hope that helps someone.

Regards, vermaden

cgull commented 1 year ago

pkg -vv | sed '1, /^Repositories/d' will at least crudely dump the repos pkg knows about without having to duplicate pkg's search logic.

But yes, introspection into pkg's configured state could be improved, I've run into problems in this general area before.

grahamperrin commented 1 year ago

pkg -vv | sed '1, /^Repositories/d'

That's neat.

For support purposes I'm more lazy, I typically ask end users to run:

pkg -vv | grep -e url -e enabled -e priority

vermaden commented 1 year ago

This one seems simpler/shorted to type:

% pkg -vv | grep -A 55 '^Repo' 

Regards, vermaden