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

refactor: only get existing svc if noconfirm is set #20

Merged
merged 2 commits into from
Mar 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
23 changes: 17 additions & 6 deletions src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,19 @@ impl APIClient {
token: &str,
name: &str,
billing_email: &str,
) -> Result<Organization, reqwest::Error> {
) -> anyhow::Result<Organization> {
let url = format!("{}/orgs", self.base_url);
let mut body = HashMap::new();
body.insert("name", name);
body.insert("billing_email", billing_email);
let response = self.post(&url, token, &body)?;
response.json()
match response.status() {
StatusCode::CREATED => Ok(serde_json::from_str(&response.text()?).with_context(|| "Failed to deserialize org")?),
StatusCode::UNAUTHORIZED => Err(anyhow!("Unauthorized, please login first")),
StatusCode::NOT_FOUND => Err(anyhow!("Org not found")),
StatusCode::BAD_REQUEST => Err(anyhow!("Bad request: {}", response.text()?)),
_ => Err(anyhow!("Failed to deploy service. API returned {} - {}", response.status(), response.text()?))
}
}

pub fn get_environments(
Expand All @@ -87,12 +93,18 @@ impl APIClient {
token: &str,
name: &str,
org_name: &str
) -> Result<CreateEnvironmentResponse, reqwest::Error> {
) -> anyhow::Result<CreateEnvironmentResponse> {
let url = format!("{}/orgs/{}/envs", self.base_url, org_name);
let mut body = HashMap::new();
body.insert("name", name);
let response = self.post(&url, token, &body)?;
response.json()
match response.status() {
StatusCode::CREATED => Ok(serde_json::from_str(&response.text()?).with_context(|| "Failed to deserialize env")?),
StatusCode::UNAUTHORIZED => Err(anyhow!("Unauthorized, please login first")),
StatusCode::NOT_FOUND => Err(anyhow!("Org not found")),
StatusCode::BAD_REQUEST => Err(anyhow!("Bad request: {}", response.text()?)),
_ => Err(anyhow!("Failed to deploy service. API returned {} - {}", response.status(), response.text()?))
}
}

pub fn deploy_service(
Expand Down Expand Up @@ -208,8 +220,7 @@ impl APIClient {
.header("Authorization", format!("Bearer {}", token))
.header("Content-Type", "application/json")
.json(&body)
.send()?
.error_for_status();
.send()
}

fn post_str(&self, url: &str, token: &str, body: String) -> Result<Response, reqwest::Error> {
Expand Down
57 changes: 17 additions & 40 deletions src/commands/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,24 +92,24 @@ impl Deploy {
));
}

let response = base.api_client().get_service(
token,
&org_name,
&manifest.environment,
&manifest.service.name,
);

let existing_svc = match response? {
Some(svc) => svc,
None => return self.create_new_service(base, token, &org_name),
};

if let Some(false) = self.no_confirm {
if existing_svc == manifest.service {
println!("no changes detected");
return Ok(());
}
let existing_svc_yaml = serde_yaml::to_string(&existing_svc)?;
let response = base.api_client().get_service(
token,
&org_name,
&manifest.environment,
&manifest.service.name,
);

let existing_svc_yaml = match response? {
Some(svc) => {
if svc == manifest.service {
println!("no changes detected");
return Ok(());
}
serde_yaml::to_string(&svc)?
}
None => "".to_string(),
};
let new_svc_yaml = serde_yaml::to_string(&manifest.service)?;
self.render_diff(existing_svc_yaml, new_svc_yaml)?;
let selection = self.user_confirmation();
Expand All @@ -129,29 +129,6 @@ impl Deploy {
Ok(())
}

fn create_new_service(&self, base: &CommandBase, token: &str, org_name: &str) -> Result<()> {
let manifest = self.read_manifest()?;
if let Some(false) = self.no_confirm {
let new_svc_yaml = serde_yaml::to_string(&manifest.service)?;
self.render_diff("".to_string(), new_svc_yaml)?;

let selection = self.user_confirmation();
if selection == 0 {
println!("Cancelling...");
return Ok(());
}
}

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

fn read_manifest(&self) -> Result<Manifest> {
let file_path = self.manifest.clone();
let mut file_content = String::new();
Expand Down
Loading