Skip to content

Commit

Permalink
feat: add spinner wait for project new and project status --follow (
Browse files Browse the repository at this point in the history
#503)

Signed-off-by: Federico Guerinoni <guerinoni.federico@gmail.com>
Co-authored-by: oddgrd <29732646+oddgrd@users.noreply.github.com>
  • Loading branch information
guerinoni and oddgrd authored Dec 7, 2022
1 parent d9d6d3e commit b597eef
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 8 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.

1 change: 1 addition & 0 deletions cargo-shuttle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ flate2 = "1.0.25"
futures = "0.3.25"
git2 = "0.14.2"
headers = "0.3.8"
indicatif = "0.17.2"
ignore = "0.4.18"
indoc = "1.0.7"
log = "0.4.17"
Expand Down
6 changes: 5 additions & 1 deletion cargo-shuttle/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ pub enum ProjectCommand {
/// remove this project environment from shuttle
Rm,
/// show the status of this project's environment on shuttle
Status,
Status {
#[clap(short, long)]
/// Follow status of project command
follow: bool,
},
}

#[derive(Parser, Clone, Debug)]
Expand Down
81 changes: 74 additions & 7 deletions cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod config;
mod factory;
mod init;

use shuttle_common::project::ProjectName;
use std::collections::BTreeMap;
use std::ffi::OsString;
use std::fs::{read_to_string, File};
Expand All @@ -27,7 +28,7 @@ use futures::StreamExt;
use git2::{Repository, StatusOptions};
use ignore::overrides::OverrideBuilder;
use ignore::WalkBuilder;
use shuttle_common::models::secret;
use shuttle_common::models::{project, secret};
use shuttle_service::loader::{build_crate, Loader};
use shuttle_service::Logger;
use std::fmt::Write;
Expand Down Expand Up @@ -93,7 +94,9 @@ impl Shuttle {
Command::Secrets => self.secrets(&client).await,
Command::Auth(auth_args) => self.auth(auth_args, &client).await,
Command::Project(ProjectCommand::New) => self.project_create(&client).await,
Command::Project(ProjectCommand::Status) => self.project_status(&client).await,
Command::Project(ProjectCommand::Status { follow }) => {
self.project_status(&client, follow).await
}
Command::Project(ProjectCommand::Rm) => self.project_delete(&client).await,
_ => {
unreachable!("commands that don't need a client have already been matched")
Expand Down Expand Up @@ -485,18 +488,82 @@ impl Shuttle {
}

async fn project_create(&self, client: &Client) -> Result<()> {
let project = client.create_project(self.ctx.project_name()).await?;
self.wait_with_spinner(
&[project::State::Ready, project::state::Errored],
Client::create_project,
self.ctx.project_name(),
client,
)
.await?;

println!("{project}");
Ok(())
}

async fn project_status(&self, client: &Client, follow: bool) -> Result<()> {
match follow {
true => {
self.wait_with_spinner(
&[
project::State::Ready,
project::State::Destroyed,
project::state::Errored,
],
Client::get_project,
self.ctx.project_name(),
client,
)
.await?;
}
false => {
let project = client.get_project(self.ctx.project_name()).await?;
println!("{project}");
}
}

Ok(())
}

async fn project_status(&self, client: &Client) -> Result<()> {
let project = client.get_project(self.ctx.project_name()).await?;
async fn wait_with_spinner<'a, F, Fut>(
&self,
states_to_check: &[project::State],
f: F,
project_name: &'a ProjectName,
client: &'a Client,
) -> Result<(), anyhow::Error>
where
F: Fn(&'a Client, &'a ProjectName) -> Fut,
Fut: std::future::Future<Output = Result<project::Response>> + 'a,
{
let mut project = f(client, project_name).await?;
let pb = indicatif::ProgressBar::new_spinner();
pb.enable_steady_tick(std::time::Duration::from_millis(350));
pb.set_style(
indicatif::ProgressStyle::with_template("{spinner:.orange} {msg}")
.unwrap()
.tick_strings(&[
"( ● )",
"( ● )",
"( ● )",
"( ● )",
"( ●)",
"( ● )",
"( ● )",
"( ● )",
"( ● )",
"(● )",
"(●●●●●●)",
]),
);
loop {
if states_to_check.contains(&project.state) {
break;
}

pb.set_message(format!("{project}"));
project = client.get_project(project_name).await?;
}
pb.finish_with_message("Done");
println!("{project}");

Ok(())
}

Expand Down

0 comments on commit b597eef

Please sign in to comment.