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

feat: polish CLI after 0.13 updates #750

Merged
merged 3 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
6 changes: 3 additions & 3 deletions cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ impl Shuttle {
let resources = client
.get_service_resources(self.ctx.project_name())
.await?;
let table = get_resources_table(&resources);
let table = get_resources_table(&resources, self.ctx.project_name().as_str());

println!("{table}");

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

let resources = get_resources_table(&resources);
let resources = get_resources_table(&resources, self.ctx.project_name().as_str());

let mut stream = runtime_client
.subscribe_logs(tonic::Request::new(SubscribeLogsRequest {}))
Expand Down Expand Up @@ -695,7 +695,7 @@ impl Shuttle {
let resources = client
.get_service_resources(self.ctx.project_name())
.await?;
let resources = get_resources_table(&resources);
let resources = get_resources_table(&resources, self.ctx.project_name().as_str());

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

Expand Down
16 changes: 11 additions & 5 deletions common/src/models/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt::Display;

use chrono::{DateTime, Utc};
use comfy_table::{
modifiers::UTF8_ROUND_CORNERS, presets::UTF8_FULL, Cell, CellAlignment, Color,
modifiers::UTF8_ROUND_CORNERS, presets::UTF8_FULL, Attribute, Cell, CellAlignment, Color,
ContentArrangement, Table,
};
use crossterm::style::Stylize;
Expand Down Expand Up @@ -59,9 +59,15 @@ pub fn get_deployments_table(deployments: &Vec<Response>, service_name: &str) ->
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::DynamicFullWidth)
.set_header(vec![
Cell::new("ID").set_alignment(CellAlignment::Center),
Cell::new("Status").set_alignment(CellAlignment::Center),
Cell::new("Last updated").set_alignment(CellAlignment::Center),
Cell::new("Deployment ID")
.set_alignment(CellAlignment::Center)
.add_attribute(Attribute::Bold),
Cell::new("Status")
.set_alignment(CellAlignment::Center)
.add_attribute(Attribute::Bold),
Cell::new("Last updated")
.set_alignment(CellAlignment::Center)
.add_attribute(Attribute::Bold),
]);

for deploy in deployments.iter() {
Expand All @@ -77,7 +83,7 @@ pub fn get_deployments_table(deployments: &Vec<Response>, service_name: &str) ->

format!(
r#"
Most recent deploys for {}
Most recent deployments for {}
{}
"#,
service_name.bold(),
Expand Down
67 changes: 39 additions & 28 deletions common/src/models/resource.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::{collections::HashMap, path::PathBuf};

use comfy_table::{
modifiers::UTF8_ROUND_CORNERS, presets::UTF8_FULL, Cell, CellAlignment, ContentArrangement,
Table,
modifiers::UTF8_ROUND_CORNERS, presets::UTF8_FULL, Attribute, Cell, CellAlignment,
ContentArrangement, Table,
};
use crossterm::style::Stylize;

Expand All @@ -11,7 +11,7 @@ use crate::{
DbOutput, SecretStore,
};

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

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

if let Some(secrets) = resource_groups.get("Secrets") {
output.push(get_secrets_table(secrets));
output.push(get_secrets_table(secrets, service_name));
};

if let Some(static_folders) = resource_groups.get("Static Folder") {
output.push(get_static_folder_table(static_folders));
output.push(get_static_folder_table(static_folders, service_name));
};

if let Some(persist) = resource_groups.get("Persist") {
output.push(get_persist_table(persist));
output.push(get_persist_table(persist, service_name));
};

output.join("\n")
}
}

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

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),
Cell::new("Connection string").set_alignment(CellAlignment::Center),
Cell::new("Type")
.add_attribute(Attribute::Bold)
.set_alignment(CellAlignment::Center),
Cell::new("Connection string")
.add_attribute(Attribute::Bold)
.set_alignment(CellAlignment::Center),
]);

for database in databases {
Expand All @@ -74,19 +78,22 @@ fn get_databases_table(databases: &Vec<&Response>) -> String {
}

format!(
r#"These databases are linked to this service
r#"These databases are linked to {}
{table}
"#,
service_name.bold()
oddgrd marked this conversation as resolved.
Show resolved Hide resolved
)
}

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

table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_header(vec![Cell::new("Key").set_alignment(CellAlignment::Center)]);
.set_header(vec![Cell::new("Keys")
.add_attribute(Attribute::Bold)
.set_alignment(CellAlignment::Center)]);

let secrets = serde_json::from_value::<SecretStore>(secrets[0].data.clone()).unwrap();

Expand All @@ -95,57 +102,61 @@ fn get_secrets_table(secrets: &[&Response]) -> String {
}

format!(
r#"These secrets can be accessed by the service
r#"These secrets can be accessed by {}
{table}
"#,
service_name.bold()
)
}

fn get_static_folder_table(static_folders: &[&Response]) -> String {
fn get_static_folder_table(static_folders: &[&Response], service_name: &str) -> String {
let mut table = Table::new();

table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::DynamicFullWidth)
.set_header(vec![
Cell::new("Static Folders").set_alignment(CellAlignment::Center)
]);
.set_header(vec![Cell::new("Static Folders")
.set_alignment(CellAlignment::Center)
.add_attribute(Attribute::Bold)]);

for folder in static_folders {
let path = serde_json::from_value::<PathBuf>(folder.data.clone())
.unwrap()
.display()
.to_string();
.file_name()
.expect("static folder path should have a final component")
.to_str()
.expect("static folder file name should be valid unicode")
.to_owned();
Comment on lines 125 to +131
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we rather get it from folder.config somehow?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I didn't think of that! Yes, that should be possible. I likely won't have time to address these today, by the way, but I can get to them in the morning.


table.add_row(vec![path]);
}

format!(
r#"These static folders can be accessed by the service
r#"These static folders can be accessed by {}
{table}
"#,
service_name.bold()
)
}

fn get_persist_table(persist_instances: &[&Response]) -> String {
fn get_persist_table(persist_instances: &[&Response], service_name: &str) -> String {
let mut table = Table::new();

table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::DynamicFullWidth)
.set_header(vec![
Cell::new("Persist Instances").set_alignment(CellAlignment::Center)
]);
.set_header(vec![Cell::new("Persist Instances")
.set_alignment(CellAlignment::Center)
.add_attribute(Attribute::Bold)]);

for _ in persist_instances {
table.add_row(vec!["Instance"]);
}

format!(
r#"These instances are linked to this service
r#"These instances are linked to {}
{table}
"#,
service_name.bold()
)
}
12 changes: 8 additions & 4 deletions common/src/models/secret.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use chrono::{DateTime, Utc};
use comfy_table::{
modifiers::UTF8_ROUND_CORNERS, presets::UTF8_FULL, Cell, CellAlignment, ContentArrangement,
Table,
modifiers::UTF8_ROUND_CORNERS, presets::UTF8_FULL, Attribute, Cell, CellAlignment,
ContentArrangement, Table,
};
use crossterm::style::Stylize;
use serde::{Deserialize, Serialize};
Expand All @@ -22,8 +22,12 @@ pub fn get_table(secrets: &Vec<Response>) -> String {
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::DynamicFullWidth)
.set_header(vec![
Cell::new("Key").set_alignment(CellAlignment::Center),
Cell::new("Last updated").set_alignment(CellAlignment::Center),
Cell::new("Key")
.set_alignment(CellAlignment::Center)
.add_attribute(Attribute::Bold),
Cell::new("Last updated")
.set_alignment(CellAlignment::Center)
.add_attribute(Attribute::Bold),
]);

for resource in secrets.iter() {
Expand Down
2 changes: 1 addition & 1 deletion common/src/models/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Status: {}
Last Updated: {}
URI: {}
"#,
self.name,
self.name.clone().bold(),
deployment.id,
deployment
.state
Expand Down