Skip to content

Commit

Permalink
feat: add --show-secrets for resources and only show secrets when given
Browse files Browse the repository at this point in the history
  • Loading branch information
Kazy committed Oct 23, 2023
1 parent 472a26d commit c3f8742
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 15 deletions.
5 changes: 4 additions & 1 deletion cargo-shuttle/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ pub enum DeploymentCommand {
#[derive(Parser)]
pub enum ResourceCommand {
/// List all the resources for a project
List,
List {
#[arg(long, default_value_t = false)]
show_secrets: bool
},
/// Delete a resource
Delete {
/// Type of the resource to delete.
Expand Down
12 changes: 7 additions & 5 deletions cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ impl Shuttle {
self.deployments_list(page, limit).await
}
Command::Deployment(DeploymentCommand::Status { id }) => self.deployment_get(id).await,
Command::Resource(ResourceCommand::List) => self.resources_list().await,
Command::Resource(ResourceCommand::List { show_secrets }) => {
self.resources_list(show_secrets).await
}
Command::Stop => self.stop().await,
Command::Clean => self.clean().await,
Command::Secrets => self.secrets().await,
Expand Down Expand Up @@ -750,13 +752,13 @@ impl Shuttle {
Ok(CommandOutcome::Ok)
}

async fn resources_list(&self) -> Result<CommandOutcome> {
async fn resources_list(&self, show_secrets: bool) -> Result<CommandOutcome> {
let client = self.client.as_ref().unwrap();
let resources = client
.get_service_resources(self.ctx.project_name())
.await
.map_err(suggestions::resources::get_service_resources_failure)?;
let table = get_resources_table(&resources, self.ctx.project_name().as_str());
let table = get_resources_table(&resources, self.ctx.project_name().as_str(), show_secrets);

println!("{table}");

Expand Down Expand Up @@ -979,7 +981,7 @@ impl Shuttle {
.map(resource::Response::from_bytes)
.collect();

println!("{}", get_resources_table(&resources, service_name.as_str()));
println!("{}", get_resources_table(&resources, service_name.as_str(), false));

let addr = SocketAddr::new(
if run_args.external {
Expand Down Expand Up @@ -1598,7 +1600,7 @@ impl Shuttle {
let resources = client
.get_service_resources(self.ctx.project_name())
.await?;
let resources = get_resources_table(&resources, self.ctx.project_name().as_str());
let resources = get_resources_table(&resources, self.ctx.project_name().as_str(), false);

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

Expand Down
2 changes: 1 addition & 1 deletion common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl DatabaseReadyInfo {
"{}://{}:{}@{}:{}/{}",
self.engine,
self.role_name,
self.role_password.expose(),
self.role_password,
self.address_public,
self.port,
self.database_name
Expand Down
39 changes: 32 additions & 7 deletions common/src/models/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@ use crossterm::style::Stylize;
use crate::{
resource::{Response, Type},
secrets::SecretStore,
DbOutput,
};

pub fn get_resources_table(resources: &Vec<Response>, service_name: &str) -> String {
pub fn get_resources_table(
resources: &Vec<Response>,
service_name: &str,
show_secrets: bool,
) -> String {
if resources.is_empty() {
format!("{}\n", "No resources are linked to this service".bold())
} else {
Expand All @@ -35,7 +40,7 @@ pub fn get_resources_table(resources: &Vec<Response>, service_name: &str) -> Str
let mut output = Vec::new();

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

if let Some(secrets) = resource_groups.get("Secrets") {
Expand All @@ -58,12 +63,21 @@ pub fn get_resources_table(resources: &Vec<Response>, service_name: &str) -> Str
}
}

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

let header = vec![Cell::new("Type")
.add_attribute(Attribute::Bold)
.set_alignment(CellAlignment::Center)];
let header = vec![
Cell::new("Type")
.add_attribute(Attribute::Bold)
.set_alignment(CellAlignment::Center),
Cell::new("Connection string")
.add_attribute(Attribute::Bold)
.set_alignment(CellAlignment::Center),
];

table
.load_preset(UTF8_FULL)
Expand All @@ -72,7 +86,18 @@ fn get_databases_table(databases: &Vec<&Response>, service_name: &str) -> String
.set_header(header);

for database in databases {
table.add_row(vec![database.r#type.to_string()]);
let info = serde_json::from_value::<DbOutput>(database.data.clone()).expect("");
let conn_string = match info {
DbOutput::Local(info) => info.clone(),
DbOutput::Info(local_uri) => {
if show_secrets {
local_uri.connection_string_private()
} else {
local_uri.connection_string_public()
}
}
};
table.add_row(vec![database.r#type.to_string(), conn_string]);
}

format!(
Expand Down
2 changes: 1 addition & 1 deletion common/src/secrets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl<T: Zeroize> Debug for Secret<T> {

impl<T: Zeroize> Display for Secret<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
write!(f, "********")
}
}

Expand Down

0 comments on commit c3f8742

Please sign in to comment.