Skip to content

Commit

Permalink
Merge pull request #9 from molnett/show-diff
Browse files Browse the repository at this point in the history
feat: show diff to user
  • Loading branch information
tmlye authored Feb 20, 2024
2 parents 8ee012a + 1237ddb commit d0f2bb6
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 6 deletions.
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ chrono = { version = "0.4.30", features = ["serde"] }
clap = { version = "4.4.2", features = ["derive", "env"] }
config = "0.13.3"
dialoguer = { version = "0.10.4", features = ["fuzzy-select"] }
difference = "2.0"
dirs-next = "2.0.0"
home = "0.5.5"
oauth2 = "4.4.2"
Expand All @@ -21,6 +22,7 @@ serde = { version = "1.0.188", features = ["derive"] }
serde_json = "1.0.105"
serde_yaml = "0.9.25"
tabled = "0.14.0"
term = "0.7.0"
thiserror = "1.0.48"
tiny_http = "0.12.0"
tracing = "0.1.37"
2 changes: 1 addition & 1 deletion src/api/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct ListServicesResponse {
pub services: Vec<Service>
}

#[derive(Serialize, Deserialize, Debug, Tabled, Clone)]
#[derive(Serialize, Deserialize, Debug, Tabled, Clone, PartialEq)]
pub struct Service {
pub name: String,
pub image: String,
Expand Down
54 changes: 49 additions & 5 deletions src/commands/services.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use anyhow::{anyhow, Result};
use clap::{Parser, Subcommand};
use dialoguer::{FuzzySelect, Input};
use difference::{Difference, Changeset};
use std::io::Write;
use std::path::Path;
use super::CommandBase;
use tabled::Table;
Expand Down Expand Up @@ -53,11 +55,11 @@ pub enum Commands {
pub struct Deploy {
#[arg(help = "Name of the app to deploy")]
name: String,
#[arg(long, help = "Environment to deploy to")]
#[arg(short, long, help = "Environment to deploy to")]
env: String,
#[arg(short, long, help = "The image to deploy, e.g. yourimage:v1")]
image: Option<String>,
#[arg(long, help = "(not implemented) Skip confirmation")]
#[arg(long, help = "Skip confirmation", default_missing_value("true"), default_value("false"), num_args(0..=1), require_equals(true))]
no_confirm: Option<bool>,
#[arg(short, long, help = "Port the application listens on")]
port: Option<u16>,
Expand Down Expand Up @@ -119,15 +121,57 @@ impl Deploy {
};

if let Some(false) = self.no_confirm {
// TODO: show user what changed
let existing_svc_yaml = serde_yaml::to_string(&existing_svc)?;
println!("{}", existing_svc_yaml);
if existing_svc.is_some() && existing_svc.clone().unwrap() == new_svc {
println!("no changes detected");
return Ok(())
}
let existing_svc_yaml = if existing_svc.is_some() {
serde_yaml::to_string(&existing_svc)?
} else {
"".to_string()
};
let new_svc_yaml = serde_yaml::to_string(&new_svc)?;
self.render_diff(existing_svc_yaml, new_svc_yaml)?;
let selection = FuzzySelect::with_theme(&dialoguer::theme::ColorfulTheme::default())
.with_prompt("Do you want to apply the above changes?")
.items(&["no", "yes"])
.default(0)
.interact()
.unwrap();
if selection == 0 {
println!("Cancelling...");
return Ok(())
}
}

let result = base.api_client().deploy_service(token, &org_name, &self.env, new_svc)?;
println!("Service {} deployed", result.name);
Ok(())
}

fn render_diff(&self, a: String, b: String) -> Result<()> {
let Changeset { diffs, .. } = Changeset::new(&a, &b, "\n");
let mut t = term::stdout().unwrap();
for i in 0..diffs.len() {
match diffs[i] {
Difference::Same(ref x) => {
t.reset().unwrap();
writeln!(t, " {}", x)?;
}
Difference::Add(ref x) => {
t.fg(term::color::GREEN).unwrap();
writeln!(t, "+{}", x)?;
}
Difference::Rem(ref x) => {
t.fg(term::color::RED).unwrap();
writeln!(t, "-{}", x)?;
}
}
}
t.reset().unwrap();
t.flush().unwrap();
Ok(())
}
}

#[derive(Parser, Debug)]
Expand Down

0 comments on commit d0f2bb6

Please sign in to comment.