Skip to content

Commit

Permalink
feat(mantle): add json and yaml formats to diff command
Browse files Browse the repository at this point in the history
  • Loading branch information
blake-mealey committed Dec 16, 2023
1 parent 14c0246 commit e2ecf2b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
17 changes: 17 additions & 0 deletions mantle/mantle/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,21 @@ fn get_app() -> App<'static, 'static> {
.help("The label of the environment to deploy to. If not specified, attempts to match the current git branch to each environment's `branches` property.")
.value_name("ENVIRONMENT")
.takes_value(true))
.arg(
Arg::with_name("output")
.long("output")
.short("o")
.help("A file path to print the diff to, if a format is provided")
.value_name("FILE")
.takes_value(true))
.arg(
Arg::with_name("format")
.long("format")
.short("f")
.help("The format to print the diff in")
.value_name("FORMAT")
.takes_value(true)
.possible_values(&["json","yaml"]))
)
.subcommand(
SubCommand::with_name("destroy")
Expand Down Expand Up @@ -167,6 +182,8 @@ pub async fn run_with(args: Vec<String>) -> i32 {
commands::diff::run(
diff_matches.value_of("PROJECT"),
diff_matches.value_of("environment"),
diff_matches.value_of("output"),
diff_matches.value_of("format"),
)
.await
}
Expand Down
36 changes: 34 additions & 2 deletions mantle/mantle/src/commands/diff.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::str;
use std::{fs, str};

use difference::Changeset;
use yansi::Paint;
Expand Down Expand Up @@ -57,7 +57,12 @@ fn print_diff(diff: ResourceGraphDiff) {
}
}

pub async fn run(project: Option<&str>, environment: Option<&str>) -> i32 {
pub async fn run(
project: Option<&str>,
environment: Option<&str>,
output: Option<&str>,
format: Option<&str>,
) -> i32 {
logger::start_action("Loading project:");
let (project_path, config) = match load_project_config(project) {
Ok(v) => v,
Expand Down Expand Up @@ -98,8 +103,35 @@ pub async fn run(project: Option<&str>, environment: Option<&str>) -> i32 {

match diff {
Ok(diff) => {
let outputs_string = format.map(|format| match format {
"json" => serde_json::to_string_pretty(&diff)
.map(|x| x + "\n")
.map_err(|e| e.to_string()),
"yaml" => serde_yaml::to_string(&diff).map_err(|e| e.to_string()),
_ => Err(format!("Unknown format: {}", format)),
});

print_diff(diff);
logger::end_action("Succeeded");

if let Some(outputs_string) = outputs_string {
if let Ok(outputs_string) = outputs_string {
if let Some(output) = output {
if let Err(e) = fs::write(output, outputs_string).map_err(|e| {
format!("Unable to write outputs file: {}\n\t{}", output, e)
}) {
logger::log(Paint::red(e));
return 1;
}
} else {
print!("{}", outputs_string);
}
} else {
logger::log(Paint::red("Failed to serialize outputs"));
return 1;
}
}

return 0;
}
Err(e) => {
Expand Down
5 changes: 5 additions & 0 deletions mantle/rbx_mantle/src/resource_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,28 +646,33 @@ where
}
}

#[derive(Serialize)]
pub struct ResourceGraphDiff {
pub removals: BTreeMap<ResourceId, ResourceRemoval>,
pub additions: BTreeMap<ResourceId, ResourceAddition>,
pub changes: BTreeMap<ResourceId, ResourceChange>,
pub dependency_changes: BTreeMap<ResourceId, ResourceDependencyChange>,
}

#[derive(Serialize)]
pub struct ResourceRemoval {
pub previous_inputs_hash: String,
pub previous_outputs_hash: String,
}

#[derive(Serialize)]
pub struct ResourceAddition {
pub current_inputs_hash: String,
}

#[derive(Serialize)]
pub struct ResourceChange {
pub previous_inputs_hash: String,
pub previous_outputs_hash: String,
pub current_inputs_hash: String,
}

#[derive(Serialize)]
pub struct ResourceDependencyChange {
pub previous_inputs_hash: String,
pub previous_outputs_hash: String,
Expand Down

0 comments on commit e2ecf2b

Please sign in to comment.