Skip to content

Commit

Permalink
wip: remove beta db resource variant
Browse files Browse the repository at this point in the history
  • Loading branch information
oddgrd committed Jun 28, 2024
1 parent 32836d1 commit 8a151a3
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 14 deletions.
14 changes: 11 additions & 3 deletions cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,14 @@ impl Shuttle {
client.get_service_resources(self.ctx.project_name()).await
}
.map_err(suggestions::resources::get_service_resources_failure)?;
let table = get_resource_tables(&resources, self.ctx.project_name(), raw, show_secrets);

let table = get_resource_tables(
&resources,
self.ctx.project_name(),
raw,
show_secrets,
self.beta,
);

println!("{table}");

Expand Down Expand Up @@ -1261,7 +1268,7 @@ impl Shuttle {

println!(
"{}",
get_resource_tables(&mocked_responses, service_name.as_str(), false, false)
get_resource_tables(&mocked_responses, service_name.as_str(), false, false, beta)
);

//
Expand Down Expand Up @@ -2102,7 +2109,8 @@ impl Shuttle {
let resources = client
.get_service_resources(self.ctx.project_name())
.await?;
let resources = get_resource_tables(&resources, self.ctx.project_name(), false, false);
let resources =
get_resource_tables(&resources, self.ctx.project_name(), false, false, self.beta);

println!("{resources}{service}");

Expand Down
1 change: 0 additions & 1 deletion common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ pub struct DbInput {
pub enum DatabaseResource {
ConnectionString(String),
Info(DatabaseInfo),
Beta(DatabaseInfoBeta),
}

/// Holds the data for building a database connection string.
Expand Down
81 changes: 74 additions & 7 deletions common/src/models/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub fn get_resource_tables(
service_name: &str,
raw: bool,
show_secrets: bool,
beta: bool,
) -> String {
if resources.is_empty() {
if raw {
Expand All @@ -44,12 +45,21 @@ pub fn get_resource_tables(
let mut output = Vec::new();

if let Some(databases) = resource_groups.get("Databases") {
output.push(get_databases_table(
databases,
service_name,
raw,
show_secrets,
));
if beta {
output.push(get_databases_table_beta(
databases,
service_name,
raw,
show_secrets,
));
} else {
output.push(get_databases_table(
databases,
service_name,
raw,
show_secrets,
));
}
};

if let Some(secrets) = resource_groups.get("Secrets") {
Expand Down Expand Up @@ -124,7 +134,6 @@ fn get_databases_table(
info.connection_string_public(show_secrets)
}
}
DatabaseResource::Beta(info) => info.connection_string(show_secrets),
};
table.add_row(vec![database.r#type.to_string(), conn_string]);
}
Expand All @@ -138,6 +147,64 @@ fn get_databases_table(
format!("These databases are linked to {service_name}\n{table}\n{show_secret_hint}")
}

fn get_databases_table_beta(
databases: &Vec<&Response>,
service_name: &str,
raw: bool,
show_secrets: bool,
) -> String {
let mut table = Table::new();

if raw {
table
.load_preset(NOTHING)
.set_content_arrangement(ContentArrangement::Disabled)
.set_header(vec![
Cell::new("Type").set_alignment(CellAlignment::Left),
Cell::new("Connection string").set_alignment(CellAlignment::Left),
]);
} else {
table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::DynamicFullWidth)
.set_header(vec![
Cell::new("Type")
.set_alignment(CellAlignment::Center)
.add_attribute(Attribute::Bold),
Cell::new("Connection string")
.add_attribute(Attribute::Bold)
.set_alignment(CellAlignment::Center),
]);
}

// TODO: add beta version that tries to parse databaseinfobeta?
for database in databases {
let connection_string = serde_json::from_value::<String>(database.data.clone())
.expect("resource data to be a valid database");

let conn_string = if let Ok(mut url) = connection_string.parse::<url::Url>() {
if url.password().is_some() && !show_secrets {
// ignore edge cases (if any)
let _ = url.set_password(Some("********"));
}
url.to_string()
} else {
connection_string
};

table.add_row(vec![database.r#type.to_string(), conn_string]);
}

let show_secret_hint = if databases.is_empty() || show_secrets {
""
} else {
"Hint: you can show the secrets of these resources using `cargo shuttle resource list --show-secrets`\n"
};

format!("These databases are linked to {service_name}\n{table}\n{show_secret_hint}")
}

fn get_secrets_table(secrets: &[&Response], service_name: &str, raw: bool) -> String {
let mut table = Table::new();

Expand Down
1 change: 0 additions & 1 deletion resources/aws-rds/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ impl IntoResource<String> for OutputWrapper {
Ok(match self.0.output {
DatabaseResource::ConnectionString(s) => s.clone(),
DatabaseResource::Info(info) => info.connection_string_shuttle(),
DatabaseResource::Beta(info) => info.connection_string(true),
})
}
}
Expand Down
1 change: 0 additions & 1 deletion resources/shared-db/src/mongo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ impl IntoResource<String> for OutputWrapper {
Ok(match self.0.output {
DatabaseResource::ConnectionString(s) => s.clone(),
DatabaseResource::Info(info) => info.connection_string_shuttle(),
DatabaseResource::Beta(info) => info.connection_string(true),
})
}
}
Expand Down
1 change: 0 additions & 1 deletion resources/shared-db/src/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ impl IntoResource<String> for OutputWrapper {
Ok(match self.0.output {
DatabaseResource::ConnectionString(s) => s.clone(),
DatabaseResource::Info(info) => info.connection_string_shuttle(),
DatabaseResource::Beta(info) => info.connection_string(true),
})
}
}
Expand Down

0 comments on commit 8a151a3

Please sign in to comment.