From ffd8ea30065d37461e800c1e1817d817fb35aa93 Mon Sep 17 00:00:00 2001 From: Anthony Nemirovsky Date: Mon, 18 Dec 2023 12:17:59 -0800 Subject: [PATCH] Issue #350: Add an update-list command (#351) Lists the available updates either as table or list and optionally list just security releases. --- commands/update.bee.inc | 147 +++++++++++++++++++++++++++++++--------- 1 file changed, 115 insertions(+), 32 deletions(-) diff --git a/commands/update.bee.inc b/commands/update.bee.inc index 285e320c..f6725fc9 100644 --- a/commands/update.bee.inc +++ b/commands/update.bee.inc @@ -41,6 +41,32 @@ function update_bee_command() { 'bee update webform tatsu' => bt('Updates the Webform module and Tatsu theme only'), ), ), + 'update-list' => array( + 'description' => bt('Lists available updates for backdrop, modules, themes and layouts with new releases'), + 'callback' => 'update_list_bee_callback', + 'aliases' => array('ups', 'pm-updatestatus', 'update-status', 'upl'), + 'bootstrap' => BEE_BOOTSTRAP_FULL, + 'arguments' => array( + 'projects' => bt('One or more projects to list updates for.'), + ), + 'optional_arguments' => array('projects'), + 'multiple_argument' => 'projects', + 'options' => array( + 'security-only' => array( + 'description' => bt('Only list updates for modules that have security updates available.'), + 'short' => 's', + ), + 'format' => array( + 'description' => bt('Format to output update information in. Options are "table" (default), "list".'), + 'value' => bt('table'), + ), + ), + 'examples' => array( + 'bee update-list' => bt('List updates for everything with a new release.'), + 'bee update-list --security-only' => bt('List only security updates for everything with a new release.'), + 'bee update-list webform tatsu' => bt('List updates for the Webform module and Tatsu theme only'), + ), + ), ); } @@ -133,6 +159,57 @@ function update_db_bee_callback() { function update_bee_callback($arguments, $options) { global $_bee_backdrop_root; require_once $_bee_backdrop_root . '/core/includes/file.inc'; + $data = update_bee_get_available_updates($arguments, $options); + if ($data != NULL) { + update_bee_render_table_output($data); + // Prompt to continue. + if (!bee_confirm(bt('Would you like to continue?'), FALSE)) { + return; + } + foreach ($data as $item) { + $folder = backdrop_get_path($item['project_type'], $item['name']); + if (is_dir($folder)) { + bee_delete($folder); + } + download_bee_callback(array('projects' => array($item['name']),), []); + echo ''; + } + } else { + bee_message(bt('No Modules or Themes to Update')); + } +} + +/** + * Command callback: Show module and theme updates. + */ +function update_list_bee_callback(array $arguments, array $options) { + $data = update_bee_get_available_updates($arguments, $options); + $is_list_format = isset($options['format']) && $options['format'] === 'list'; + if ($data == NULL) { + if (!$is_list_format) { + bee_message(bt('No Modules or Themes to Update')); + } + return; + } + if ($is_list_format) { + update_bee_render_list_output($data); + } else { + update_bee_render_table_output($data); + } +} + +/** + * Get an array of available project updates. + * + * @param array $arguments + * A list of arguments passed to the command. + * @param array $options + * A list of options passed to the command. + * + * @return array|NULL + * An array of projects with updates available, or NULL if there are none. + */ +function update_bee_get_available_updates(array $arguments, array $options) { $data = NULL; if ($available = update_get_available(TRUE)) { module_load_include('inc', 'update', 'update.compare'); @@ -156,41 +233,47 @@ function update_bee_callback($arguments, $options) { unset($data[$item['name']]); continue; } - if ((isset($options['security-only']) || isset($options['s'])) && $item['status'] !== UPDATE_NOT_SECURE) { + if (isset($options['security-only']) && $item['status'] !== UPDATE_NOT_SECURE) { unset($data[$item['name']]); } } } - if ($data != NULL) { - foreach ($data as $item) { - $rows[] = array( - array('value' => $item['name']), - array('value' => $item['existing_version']), - array('value' => $item['latest_version']), - ); - } - bee_render_text(array('value' => bt("These are the items being updated:\n"))); - bee_render_table(array( - 'rows' => $rows, - 'header' => array( - array('value' => bt('Module')), - array('value' => bt('Existing')), - array('value' => bt('Latest')), - ), - )); - // Prompt to continue. - if (!bee_confirm(bt('Would you like to continue?'), FALSE)) { - return; - } - foreach ($data as $item) { - $folder = backdrop_get_path($item['project_type'], $item['name']); - if (is_dir($folder)) { - bee_delete($folder); - } - download_bee_callback(array('projects' => array($item['name']),), []); - echo ''; - } - } else { - bee_message(bt('No Modules or Themes to Update')); + return $data; +} + +/** + * Render available updates as a table. + * + * @param array $data + * An array of projects with updates available. + */ +function update_bee_render_table_output(array $data) { + foreach ($data as $item) { + $rows[] = array( + array('value' => $item['name']), + array('value' => $item['existing_version']), + array('value' => $item['latest_version']), + ); + } + bee_render_text(array('value' => bt("These are the items being updated:\n"))); + bee_render_table(array( + 'rows' => $rows, + 'header' => array( + array('value' => bt('Module')), + array('value' => bt('Existing')), + array('value' => bt('Latest')), + ), + )); +} + +/** + * Render available updates as a list. + * + * @param array $data + * An array of projects with updates available. + */ +function update_bee_render_list_output(array $data) { + foreach ($data as $item) { + bee_render_text(array('value' => $item['name'])); } }