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 4 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
28 changes: 26 additions & 2 deletions cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::str::FromStr;
use std::sync::Arc;

use anyhow::{anyhow, bail, Context, Result};
use args::SecretsArgs;
use args::{ProjectUpdateCommand, SecretsArgs};
use chrono::Utc;
use clap::{parser::ValueSource, CommandFactory, FromArgMatches};
use crossterm::style::Stylize;
Expand All @@ -32,6 +32,7 @@ use indicatif::ProgressBar;
use indoc::{formatdoc, printdoc};
use reqwest::header::HeaderMap;
use shuttle_api_client::ShuttleApiClient;
use shuttle_common::models::project::ProjectUpdateRequestBeta;
use shuttle_common::{
constants::{
headers::X_CARGO_SHUTTLE_VERSION, API_URL_BETA, API_URL_DEFAULT, DEFAULT_IDLE_MINUTES,
Expand Down Expand Up @@ -190,10 +191,12 @@ 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).");
}

// commands that differ in behavior in any way between .rs/.dev
if !matches!(
args.cmd,
Command::Feedback | Command::Generate(_) | Command::Upgrade { .. }
Expand Down Expand Up @@ -265,9 +268,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 +369,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 +2806,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