github.com/canonical/ubuntu-image@v0.0.0-20240430122802-2202fe98b290/tests/lib/external/snapd-testing-tools/tools/snaps.name (about)

     1  #!/bin/bash
     2  
     3  show_help() {
     4      echo "usage: snaps.name gadget, kernel, core, snap-suffix"
     5      echo ""
     6      echo "get the name of a specific snap for the current system"
     7  }
     8  
     9  gadget_name() {
    10      snap list | grep -E '(gadget$|gadget,)' | awk '{ print $1 }'
    11  }
    12  
    13  kernel_name(){
    14      # TODO this may not always be true during remodel scenarios
    15      # if the old kernel remains
    16      snap list | grep -E '(kernel$|kernel,)' | awk '{ print $1 }'
    17  }
    18  
    19  core_name(){
    20      local core_name
    21      core_name="$(snap model --verbose | grep -Po "^base:\\s+\\K.*" || true)"
    22      if [ -z "$core_name" ]; then
    23          core_name="core"
    24      fi
    25      echo "$core_name"
    26  }
    27  
    28  snap_suffix(){
    29      if os.query is-core18; then
    30          echo "-core18"
    31      elif os.query is-core20; then
    32          echo "-core20"
    33      elif os.query is-core22; then
    34          echo "-core22"
    35      elif os.query is-core24; then
    36          echo "-core24"
    37      fi
    38  }
    39  
    40  main() {
    41      if [ $# -eq 0 ]; then
    42          show_help
    43          exit 0
    44      fi
    45  
    46      while [ $# -gt 0 ]; do
    47          case "$1" in
    48              -h|--help)
    49                  show_help
    50                  exit 0
    51                  ;;
    52              gadget)
    53                  gadget_name
    54                  exit
    55                  ;;
    56              kernel)
    57                  kernel_name
    58                  exit
    59                  ;;
    60              core)
    61                  core_name
    62                  exit
    63                  ;;
    64              snap-suffix)
    65                  snap_suffix
    66                  exit
    67                  ;;
    68              *)
    69                  echo "snaps.name: unknown snap $1" >&2
    70                  exit 1
    71                  ;;
    72          esac
    73      done
    74  
    75  }
    76  
    77  main "$@"