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 1 commit
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
23 changes: 16 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 @@ -191,7 +191,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 +222,7 @@ pub enum ResourceCommand {
},
}

#[derive(Parser)]
#[derive(Subcommand)]
pub enum CertificateCommand {
/// Add an SSL certificate for a custom domain
Add {
Expand All @@ -245,12 +245,15 @@ 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
Status {
/// Follow status of project
// unused in beta (project has no state to follow)
Expand Down Expand Up @@ -280,6 +283,12 @@ pub enum ProjectCommand {
Link,
}

#[derive(Subcommand, Debug)]
pub enum ProjectUpdateCommand {
/// Rename the project, including it's hosted subdomain
Name { name: String },
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Args, Debug)]
pub struct ConfirmationArgs {
/// Skip confirmations and proceed
Expand Down
25 changes: 24 additions & 1 deletion 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 @@ -365,6 +368,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 +2805,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