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(cargo-shuttle): update project name command #1912

Merged
merged 9 commits into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
9 changes: 9 additions & 0 deletions api-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,15 @@ impl ShuttleApiClient {
self.get_json("/projects".to_owned()).await
}

pub async fn update_project_beta(
&self,
project: &str,
req: project::ProjectUpdateRequestBeta,
) -> Result<project::ProjectResponseBeta> {
self.put_json(format!("/projects/{project}"), Some(req))
.await
}

pub async fn stop_project(&self, project: &str) -> Result<project::Response> {
let path = format!("/projects/{project}");

Expand Down
25 changes: 18 additions & 7 deletions cargo-shuttle/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub enum Command {
},
}

#[derive(Parser)]
#[derive(Subcommand)]
pub enum GenerateCommand {
/// Generate shell completions
Shell {
Expand All @@ -166,7 +166,7 @@ pub struct TableArgs {
pub raw: bool,
}

#[derive(Parser)]
#[derive(Subcommand)]
pub enum DeploymentCommand {
/// List the deployments for a service
#[command(visible_alias = "ls")]
Expand All @@ -183,6 +183,7 @@ pub enum DeploymentCommand {
table: TableArgs,
},
/// View status of a deployment
#[command(visible_alias = "stat")]
Status {
/// ID of deployment to get status for
id: Option<String>,
Expand All @@ -191,7 +192,7 @@ pub enum DeploymentCommand {
Stop,
}

#[derive(Parser)]
#[derive(Subcommand)]
pub enum ResourceCommand {
/// List the resources for a project
#[command(visible_alias = "ls")]
Expand Down Expand Up @@ -222,7 +223,7 @@ pub enum ResourceCommand {
},
}

#[derive(Parser)]
#[derive(Subcommand)]
pub enum CertificateCommand {
/// Add an SSL certificate for a custom domain
Add {
Expand All @@ -245,12 +246,16 @@ pub enum CertificateCommand {
},
}

#[derive(Parser)]
#[derive(Subcommand)]
pub enum ProjectCommand {
/// Create an environment for this project on Shuttle
/// Create a project on Shuttle
#[command(visible_alias = "create")]
Start(ProjectStartArgs),
/// Check the status of this project's environment on Shuttle
/// Update project config
#[command(subcommand, visible_alias = "upd")]
Update(ProjectUpdateCommand),
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
/// Get the status of this project on Shuttle
#[command(visible_alias = "stat")]
Status {
/// Follow status of project
// unused in beta (project has no state to follow)
Expand Down Expand Up @@ -280,6 +285,12 @@ pub enum ProjectCommand {
Link,
}

#[derive(Subcommand, Debug)]
pub enum ProjectUpdateCommand {
/// Rename the project, including its hosted subdomain
Name { name: String },
}

#[derive(Args, Debug)]
pub struct ConfirmationArgs {
/// Skip confirmations and proceed
Expand Down
32 changes: 27 additions & 5 deletions cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use std::str::FromStr;
use std::sync::Arc;

use anyhow::{anyhow, bail, Context, Result};
use args::SecretsArgs;
use chrono::Utc;
use clap::{parser::ValueSource, CommandFactory, FromArgMatches};
use crossterm::style::Stylize;
Expand Down Expand Up @@ -49,7 +48,7 @@ use shuttle_common::{
CREATE_SERVICE_BODY_LIMIT, GIT_STRINGS_MAX_LENGTH,
},
error::ApiError,
project,
project::{self, ProjectUpdateRequestBeta},
resource::{get_certificates_table_beta, get_resource_tables, get_resource_tables_beta},
},
resource::{self, ResourceInput, ShuttleResourceOutput},
Expand All @@ -76,8 +75,8 @@ use zip::write::FileOptions;

use crate::args::{
CertificateCommand, ConfirmationArgs, DeployArgs, DeploymentCommand, GenerateCommand, InitArgs,
LoginArgs, LogoutArgs, LogsArgs, ProjectCommand, ProjectStartArgs, ResourceCommand, TableArgs,
TemplateLocation,
LoginArgs, LogoutArgs, LogsArgs, ProjectCommand, ProjectStartArgs, ProjectUpdateCommand,
ResourceCommand, SecretsArgs, TableArgs, TemplateLocation,
};
pub use crate::args::{Command, ProjectArgs, RunArgs, ShuttleArgs};
use crate::config::RequestContext;
Expand Down Expand Up @@ -190,12 +189,14 @@ impl Shuttle {
Command::Deployment(DeploymentCommand::Stop)
| Command::Account
| Command::Project(ProjectCommand::Link)
| Command::Project(ProjectCommand::Update(..))
) {
bail!("This command is not supported on the OLD platform (shuttle.rs).");
}

if !matches!(
args.cmd,
// commands that don't differ in behavior in any way between .rs/.dev
Command::Feedback | Command::Generate(_) | Command::Upgrade { .. }
) {
if self.beta {
Expand Down Expand Up @@ -265,9 +266,10 @@ impl Shuttle {
| Command::Project(
// ProjectCommand::List does not need to know which project we are in
ProjectCommand::Start { .. }
| ProjectCommand::Update(..)
| ProjectCommand::Status { .. }
| ProjectCommand::Stop { .. }
| ProjectCommand::Restart { .. }
| ProjectCommand::Status { .. }
| ProjectCommand::Delete { .. }
| ProjectCommand::Link
)
Expand Down Expand Up @@ -365,6 +367,9 @@ impl Shuttle {
self.project_start(idle_minutes).await
}
}
ProjectCommand::Update(cmd) => match cmd {
ProjectUpdateCommand::Name { name } => self.project_rename_beta(name).await,
},
ProjectCommand::Restart(ProjectStartArgs { idle_minutes }) => {
self.project_restart(idle_minutes).await
}
Expand Down Expand Up @@ -2799,6 +2804,23 @@ impl Shuttle {

Ok(())
}
async fn project_rename_beta(&self, name: String) -> Result<()> {
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
let client = self.client.as_ref().unwrap();

let project = client
.update_project_beta(
self.ctx.project_id(),
ProjectUpdateRequestBeta {
name: Some(name),
..Default::default()
},
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
)
.await?;

println!("Renamed project {} to {}", project.id, project.name);
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved

Ok(())
}

async fn project_restart(&self, idle_minutes: u64) -> Result<()> {
self.project_stop()
Expand Down