diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..35d619fe --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,46 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'topgrade'", + "console": "integratedTerminal", + "cargo": { + "args": [ + "build", + "--bin=topgrade", + "--package=topgrade" + ], + "filter": { + "name": "topgrade", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'topgrade'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=topgrade", + "--package=topgrade" + ], + "filter": { + "name": "topgrade", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} diff --git a/.vscode/topgrade.code-snippets b/.vscode/topgrade.code-snippets index b0722d18..002d26f2 100644 --- a/.vscode/topgrade.code-snippets +++ b/.vscode/topgrade.code-snippets @@ -21,5 +21,15 @@ "body": [ "return Err(SkipStep(format!(\"$1\").into()));" ] + }, + "Step": { + "scope": "rust", + "prefix": "step", + "body": [ + "pub fn $1(ctx: &ExecutionContext) -> Result<()> {", + " $0", + " Ok(())", + "}" + ] } -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index a61cd2be..24f95d82 100644 --- a/src/main.rs +++ b/src/main.rs @@ -392,6 +392,7 @@ fn run() -> Result<()> { } } } + runner.execute(Step::Vagrant, "Vagrant boxes", || vagrant::upgrade_vagrant_boxes(&ctx))?; if !runner.report().data().is_empty() { print_separator("Summary"); diff --git a/src/steps/remote/vagrant.rs b/src/steps/remote/vagrant.rs index ed3dfad3..1a248b26 100644 --- a/src/steps/remote/vagrant.rs +++ b/src/steps/remote/vagrant.rs @@ -4,6 +4,7 @@ use std::{fmt::Display, rc::Rc, str::FromStr}; use anyhow::Result; use log::{debug, error}; +use regex::Regex; use strum::EnumString; use crate::execution_context::ExecutionContext; @@ -200,3 +201,35 @@ pub fn topgrade_vagrant_box(ctx: &ExecutionContext, vagrant_box: &VagrantBox) -> .args(&["ssh", "-c", &command]) .check_run() } + +pub fn upgrade_vagrant_boxes(ctx: &ExecutionContext) -> Result<()> { + let vagrant = utils::require("vagrant")?; + print_separator("Vagrant boxes"); + + let outdated = Command::new(&vagrant) + .args(&["box", "outdated", "--global"]) + .check_output()?; + + let re = Regex::new(r"\* '(.*?)' for '(.*?)' is outdated").unwrap(); + + let mut found = false; + for ele in re.captures_iter(&outdated) { + found = true; + let _ = ctx + .run_type() + .execute(&vagrant) + .args(&["box", "update", "--box"]) + .arg(&ele.get(1).unwrap().as_str()) + .arg("--provider") + .arg(ele.get(2).unwrap().as_str()) + .check_run(); + } + + if !found { + println!("No outdated boxes") + } else { + ctx.run_type().execute(&vagrant).args(&["box", "prune"]).check_run()?; + } + + Ok(()) +}