From 126fe5ca4cade8eacda0942cc72559215722bcf2 Mon Sep 17 00:00:00 2001 From: Kieren Davies Date: Mon, 3 Apr 2023 15:31:25 +0200 Subject: [PATCH] refactor: Enable exhaustiveness check of command matching (#768) * refactor: Enable exhaustiveness check of command matching * refactor: Group project methods together --- cargo-shuttle/src/lib.rs | 102 ++++++++++++++++++--------------------- 1 file changed, 48 insertions(+), 54 deletions(-) diff --git a/cargo-shuttle/src/lib.rs b/cargo-shuttle/src/lib.rs index 2cf21ca2b..a684ee6f5 100644 --- a/cargo-shuttle/src/lib.rs +++ b/cargo-shuttle/src/lib.rs @@ -89,45 +89,41 @@ impl Shuttle { Command::Logout => self.logout().await, Command::Feedback => self.feedback().await, Command::Run(run_args) => self.local_run(run_args).await, - need_client => { - let mut client = Client::new(self.ctx.api_url()); - client.set_api_key(self.ctx.api_key()?); - - match need_client { - Command::Deploy(deploy_args) => { - return self.deploy(deploy_args, &client).await; - } - Command::Status => self.status(&client).await, - Command::Logs { id, follow } => self.logs(&client, id, follow).await, - Command::Deployment(DeploymentCommand::List) => { - self.deployments_list(&client).await - } - Command::Deployment(DeploymentCommand::Status { id }) => { - self.deployment_get(&client, id).await - } - Command::Resource(ResourceCommand::List) => self.resources_list(&client).await, - Command::Stop => self.stop(&client).await, - Command::Clean => self.clean(&client).await, - Command::Secrets => self.secrets(&client).await, - Command::Project(ProjectCommand::New { idle_minutes }) => { - self.project_create(&client, idle_minutes).await - } - Command::Project(ProjectCommand::Status { follow }) => { - self.project_status(&client, follow).await - } - Command::Project(ProjectCommand::List { filter }) => { - self.projects_list(&client, filter).await - } - Command::Project(ProjectCommand::Rm) => self.project_delete(&client).await, - _ => { - unreachable!("commands that don't need a client have already been matched") - } - } + Command::Deploy(deploy_args) => { + return self.deploy(deploy_args, &self.client()?).await; + } + Command::Status => self.status(&self.client()?).await, + Command::Logs { id, follow } => self.logs(&self.client()?, id, follow).await, + Command::Deployment(DeploymentCommand::List) => { + self.deployments_list(&self.client()?).await + } + Command::Deployment(DeploymentCommand::Status { id }) => { + self.deployment_get(&self.client()?, id).await + } + Command::Resource(ResourceCommand::List) => self.resources_list(&self.client()?).await, + Command::Stop => self.stop(&self.client()?).await, + Command::Clean => self.clean(&self.client()?).await, + Command::Secrets => self.secrets(&self.client()?).await, + Command::Project(ProjectCommand::New { idle_minutes }) => { + self.project_create(&self.client()?, idle_minutes).await + } + Command::Project(ProjectCommand::Status { follow }) => { + self.project_status(&self.client()?, follow).await } + Command::Project(ProjectCommand::List { filter }) => { + self.projects_list(&self.client()?, filter).await + } + Command::Project(ProjectCommand::Rm) => self.project_delete(&self.client()?).await, } .map(|_| CommandOutcome::Ok) } + fn client(&self) -> Result { + let mut client = Client::new(self.ctx.api_url()); + client.set_api_key(self.ctx.api_key()?); + Ok(client) + } + /// Log in, initialize a project and potentially create the Shuttle environment for it. /// /// If both a project name and framework are passed as arguments, it will run without any extra @@ -224,9 +220,7 @@ impl Shuttle { project_args.working_directory = path; self.load_project(&mut project_args)?; - let mut client = Client::new(self.ctx.api_url()); - client.set_api_key(self.ctx.api_key()?); - self.project_create(&client, IDLE_MINUTES).await?; + self.project_create(&self.client()?, IDLE_MINUTES).await?; } Ok(()) @@ -781,6 +775,23 @@ impl Shuttle { Ok(()) } + async fn project_delete(&self, client: &Client) -> Result<()> { + self.wait_with_spinner( + &[ + project::State::Destroyed, + project::State::Errored { + message: Default::default(), + }, + ], + client.delete_project(self.ctx.project_name()), + self.ctx.project_name(), + client, + ) + .await?; + + Ok(()) + } + async fn wait_with_spinner<'a, Fut>( &self, states_to_check: &[project::State], @@ -807,23 +818,6 @@ impl Shuttle { Ok(()) } - async fn project_delete(&self, client: &Client) -> Result<()> { - self.wait_with_spinner( - &[ - project::State::Destroyed, - project::State::Errored { - message: Default::default(), - }, - ], - client.delete_project(self.ctx.project_name()), - self.ctx.project_name(), - client, - ) - .await?; - - Ok(()) - } - fn make_archive(&self) -> Result> { let encoder = GzEncoder::new(Vec::new(), Compression::fast()); let mut tar = Builder::new(encoder);