Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for --check flag in update command #62

Merged
merged 3 commits into from
Aug 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 60 additions & 34 deletions src/cli/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub struct UpdateSubcommand {
/// Update tools globally instead of using the nearest manifest file.
#[clap(long)]
pub global: bool,

#[clap(long)]
pub check: bool,
}

impl UpdateSubcommand {
Expand Down Expand Up @@ -150,8 +153,7 @@ impl UpdateSubcommand {
.try_collect::<Vec<_>>()
.await?;

// 4. Modify the manifest with the desired new tools, save
pt.update_message("Modifying");
// 4. Check if the --check flag was used, and is so, check for updates
let tools_changed = tool_releases
.iter()
.filter_map(|(alias, _, artifact)| {
Expand All @@ -164,44 +166,68 @@ impl UpdateSubcommand {
}
})
.collect::<Vec<_>>();
for (alias, _, spec_new) in &tools_changed {
manifest.update_tool(alias, spec_new);
let bullet = style("•").dim();
let arrow = style("→").dim();

let updated_tool_lines = tools_changed
.iter()
.map(|(alias, spec_old, spec_new)| {
format!(
"{bullet} {} {} {arrow} {}",
style(alias.to_string()).bold().cyan(),
style(spec_old.version()).yellow(),
style(spec_new.version()).bold().yellow()
)
})
.collect::<Vec<_>>()
.join("\n");
if self.check {
pt.update_message("Checking for updates");

if tools_changed.is_empty() {
pt.finish_with_message(format!(
"All tools are already up-to-date! {}",
pt.formatted_elapsed(),
));
} else {
pt.finish_with_message(format!(
"New versions are available for {} tool{} {}\
\n\n{updated_tool_lines}\n\n\
Run `{}` to update the tools.",
style(tools_changed.len()).bold().magenta(),
if tools_changed.len() == 1 { "" } else { "s" },
pt.formatted_elapsed(),
style("rokit update").bold().green(),
));
}
pt.subtask_completed();
}
manifest.save(&manifest_path).await?;

// 5. Finally, display a nice message to the user
if tools_changed.is_empty() {
pt.finish_with_message(format!(
"All tools are already up-to-date! {}",
pt.formatted_elapsed(),
));
} else {
let bullet = style("•").dim();
let arrow = style("→").dim();
// 5. Modify the manifest with the desired new tools, save
pt.update_message("Modifying");

let updated_tool_lines = tools_changed
.iter()
.map(|(alias, spec_old, spec_new)| {
format!(
"{bullet} {} {} {arrow} {}",
style(alias.to_string()).bold().cyan(),
style(spec_old.version()).yellow(),
style(spec_new.version()).bold().yellow()
)
})
.collect::<Vec<_>>()
.join("\n");
for (alias, _, spec_new) in &tools_changed {
manifest.update_tool(alias, spec_new);
pt.subtask_completed();
}
manifest.save(&manifest_path).await?;

pt.finish_with_message(format!(
"Updated versions for {} tool{} {}\
// 6. Finally, display a nice message to the user
if tools_changed.is_empty() {
pt.finish_with_message(format!(
"All tools are already up-to-date! {}",
pt.formatted_elapsed(),
));
} else {
pt.finish_with_message(format!(
"Updated versions for {} tool{} {}\
\n\n{updated_tool_lines}\n\n\
Run `{}` to install the updated tools.",
style(tools_changed.len()).bold().magenta(),
if tools_changed.len() == 1 { "" } else { "s" },
pt.formatted_elapsed(),
style("rokit install").bold().green(),
));
style(tools_changed.len()).bold().magenta(),
if tools_changed.len() == 1 { "" } else { "s" },
pt.formatted_elapsed(),
style("rokit install").bold().green(),
));
}
}

// FUTURE: Install the newly updated tools automatically
Expand Down