Skip to content

Commit

Permalink
Issue #350: Add an update-list command (#351)
Browse files Browse the repository at this point in the history
Lists the available updates either as table or list and optionally list just security releases.
  • Loading branch information
anemirovsky authored Dec 18, 2023
1 parent 5a214e0 commit ffd8ea3
Showing 1 changed file with 115 additions and 32 deletions.
147 changes: 115 additions & 32 deletions commands/update.bee.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
),
),
);
}

Expand Down Expand Up @@ -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');
Expand All @@ -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']));
}
}

0 comments on commit ffd8ea3

Please sign in to comment.