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

     1  #!/bin/bash
     2  
     3  show_help() {
     4      echo "usage: spread-manager set-manual [--project-path <PROJECT-PATH>] <TEST-PATH...>"
     5      echo "       spread-manager unset-manual [--project-path <PROJECT-PATH>] <TEST-PATH...>"
     6      echo "       spread-manager reset-manual [--project-path <PROJECT-PATH>] <TEST-PATH...>"
     7      echo ""
     8      echo "Tool used to help with functions that are not already implemented in spread"
     9  }
    10  
    11  _check_project_path() {
    12      local project_path="$1"
    13      if [ -z "$project_path" ]; then
    14          echo "spread-manager: project path cannot be empty"
    15          exit 1
    16      fi
    17      if [ ! -d "$project_path" ]; then
    18          echo "spread-manager: project path \"$project_path\" has to be a directory"
    19          exit 1
    20      fi
    21      if [ ! -f "$project_path/spread.yaml" ]; then
    22          echo "spread-manager: project spread file \"$project_path/spread.yaml\" does not exist"
    23          exit 1
    24      fi
    25  }
    26  
    27  _check_test_paths() {
    28      # shellcheck disable=SC2124
    29      local test_paths="$@"
    30      if [ -z "$test_paths" ]; then
    31          echo "spread-manager: test path cannot be empty"
    32          exit 1
    33      fi
    34      for task_path in $test_paths; do
    35          _check_test_path "$task_path"
    36      done
    37  }
    38  
    39  _check_test_path() {
    40      local test_path="$1"
    41      if [ -z "$test_path" ]; then
    42          echo "spread-manager: test path cannot be empty"
    43          exit 1
    44      fi
    45      if [ ! -d "$test_path" ]; then
    46          echo "spread-manager: test path \"$test_path\" has to be a directory"
    47          exit 1
    48      fi
    49      if [ ! -f "$test_path/task.yaml" ]; then
    50          echo "spread-manager: test task \"$test_path/task.yaml\" does not exist"
    51          exit 1
    52      fi
    53  }
    54  
    55  _set_manual_value() {
    56      local task_path=$1
    57      local manual_value=$2
    58  
    59      # Update the manual property
    60      if grep -E "^manual:" "$task_path"; then
    61          sed -i -e "s/^manual:.*/manual: $manual_value/g" "$task_path"
    62      else
    63          echo "manual: $manual_value" >> "$task_path"
    64      fi
    65  }
    66  
    67  set_manual() {
    68      local project_path="$1"
    69      shift
    70      # shellcheck disable=SC2124
    71      local test_paths="$@"
    72  
    73      _check_project_path "$project_path"
    74      test_paths="$(echo "$test_paths" | tr ',' ' ')"
    75      _check_test_paths "$test_paths"
    76  
    77      for test_path in $test_paths; do
    78          local task_path
    79          task_path="$project_path/$test_path/task.yaml"
    80  
    81          # Backup the task
    82          cp "$task_path" "$task_path.back"
    83          _set_manual_value "$task_path" true
    84      done
    85  }
    86  
    87  unset_manual() {
    88      local project_path="$1"
    89      shift
    90      # shellcheck disable=SC2124
    91      local test_paths="$@"
    92  
    93      _check_project_path "$project_path"
    94      test_paths="$(echo "$test_paths" | tr ',' ' ')"
    95      _check_test_paths "$test_paths"
    96  
    97      for test_path in $test_paths; do
    98          local task_path
    99          task_path="$project_path/$test_path/task.yaml"
   100  
   101          # Backup the task
   102          cp "$task_path" "$task_path.back"
   103          _set_manual_value "$task_path" false
   104      done
   105  }
   106  
   107  reset_manual() {
   108      local project_path="$1"
   109      shift
   110      # shellcheck disable=SC2124
   111      local test_paths="$@"
   112  
   113      _check_project_path "$project_path"
   114      test_paths="$(echo "$test_paths" | tr ',' ' ')"
   115      _check_test_paths "$test_paths"
   116  
   117      for test_path in $test_paths; do
   118          local task_path
   119          task_path="$project_path/$test_path/task.yaml"
   120  
   121          if [ -f "$task_path.back" ]; then
   122              mv "$task_path.back" "$task_path"
   123          else
   124              echo "spread-manager: test task backup does not exist \"$task_path.back\""
   125              exit 1
   126          fi
   127      done
   128  }
   129  
   130  main() {
   131      if [ $# -eq 0 ]; then
   132          show_help
   133          exit 0
   134      fi
   135  
   136      local subcommand="$1"
   137      local action=
   138      if [ $# -eq 0 ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
   139          show_help
   140          exit 0
   141      else
   142          action=$(echo "$subcommand" | tr '-' '_')
   143          shift
   144      fi
   145  
   146      if [ -z "$(declare -f "$action")" ]; then
   147          echo "spread-manager: no such command: $subcommand" >&2
   148          show_help
   149          exit 1
   150      fi
   151  
   152      local project_path
   153      if [ $# -gt 0 ]; then
   154          if [[ "$action" =~ .*_manual ]]; then
   155              project_path="$(pwd)"
   156              if [ "$1" == "--project-path" ]; then
   157                  project_path="$2"
   158                  shift 2
   159              fi
   160              "$action" "$project_path" "$@"
   161          else
   162              "$action" "$@"
   163          fi
   164      fi
   165  }
   166  
   167  main "$@"