Skip to content

Commit

Permalink
refactor(cargo-shuttle): cleanup of beta branching & behaviour (#1760)
Browse files Browse the repository at this point in the history
* refa(cargo-shuttle): no if beta in client

* feat: split models, more beta chores

* clip it
  • Loading branch information
jonaro00 committed May 2, 2024
1 parent 28f4011 commit 43d7f28
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 126 deletions.
2 changes: 0 additions & 2 deletions backends/src/client/gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ mod tests {
id: "00000000000000000000000001".to_string(),
name: "user-1-project-1".to_string(),
state: State::Stopped,
deployment_state: None,
idle_minutes: Some(30),
owner: shuttle_common::models::project::Owner::User("user-1".to_string()),
is_admin: true,
Expand All @@ -116,7 +115,6 @@ mod tests {
id: "00000000000000000000000002".to_string(),
name: "user-1-project-2".to_string(),
state: State::Ready,
deployment_state: None,
idle_minutes: Some(30),
owner: shuttle_common::models::project::Owner::User("user-1".to_string()),
is_admin: true,
Expand Down
3 changes: 2 additions & 1 deletion cargo-shuttle/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ pub enum ResourceCommand {
#[derive(Parser)]
pub enum ProjectCommand {
/// Create an environment for this project on Shuttle
#[command(visible_alias = "create")]
Start(ProjectStartArgs),
/// Check the status of this project's environment on Shuttle
Status {
Expand All @@ -211,7 +212,7 @@ pub enum ProjectCommand {
Stop,
/// Destroy and create an environment for this project on Shuttle
Restart(ProjectStartArgs),
/// List all projects belonging to the calling account
/// List all projects you have access to
List {
#[arg(long, default_value = "1")]
/// (deprecated) Which page to display
Expand Down
81 changes: 44 additions & 37 deletions cargo-shuttle/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@ pub struct ShuttleApiClient {
client: reqwest::Client,
api_url: String,
api_key: Option<ApiKey>,
/// alter behaviour to interact with the new platform
beta: bool,
}

impl ShuttleApiClient {
pub fn new(api_url: String, api_key: Option<ApiKey>, beta: bool) -> Self {
pub fn new(api_url: String, api_key: Option<ApiKey>) -> Self {
Self {
client: reqwest::Client::builder()
.default_headers(
Expand All @@ -44,7 +42,6 @@ impl ShuttleApiClient {
.unwrap(),
api_url,
api_key,
beta,
}
}

Expand Down Expand Up @@ -144,13 +141,14 @@ impl ShuttleApiClient {
}

pub async fn get_service_resources(&self, project: &str) -> Result<Vec<resource::Response>> {
let path = if self.beta {
format!("/projects/{project}/resources")
} else {
format!("/projects/{project}/services/{project}/resources")
};

self.get(path).await
self.get(format!("/projects/{project}/services/{project}/resources"))
.await
}
pub async fn get_service_resources_beta(
&self,
project: &str,
) -> Result<Vec<resource::Response>> {
self.get(format!("/projects/{project}/resources")).await
}

pub async fn delete_service_resource(
Expand All @@ -159,29 +157,39 @@ impl ShuttleApiClient {
resource_type: &resource::Type,
) -> Result<()> {
let r#type = resource_type.to_string();

let r#type = utf8_percent_encode(&r#type, percent_encoding::NON_ALPHANUMERIC).to_owned();

let path = if self.beta {
format!("/projects/{project}/resources/{}", r#type)
} else {
format!(
"/projects/{project}/services/{project}/resources/{}",
r#type
)
};
self.delete(format!(
"/projects/{project}/services/{project}/resources/{}",
r#type
))
.await
}
pub async fn delete_service_resource_beta(
&self,
project: &str,
resource_type: &resource::Type,
) -> Result<()> {
let r#type = resource_type.to_string();
let r#type = utf8_percent_encode(&r#type, percent_encoding::NON_ALPHANUMERIC).to_owned();

self.delete(path).await
self.delete(format!("/projects/{project}/resources/{}", r#type))
.await
}

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

self.post(path, Some(config))
self.post(format!("/projects/{project}"), Some(config))
.await
.context("failed to make create project request")?
.to_json()
.await
}
pub async fn create_project_beta(&self, name: &str) -> Result<project::ResponseBeta> {
self.post(format!("/projects/{name}"), None::<()>)
.await
.context("failed to make create project request")?
.to_json()
Expand All @@ -199,14 +207,18 @@ impl ShuttleApiClient {
}

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

self.get(path).await
self.get(format!("/projects/{project}")).await
}
pub async fn get_project_beta(&self, project: &str) -> Result<project::ResponseBeta> {
self.get(format!("/projects/{project}")).await
}

pub async fn get_projects_list(&self) -> Result<Vec<project::Response>> {
self.get("/projects".to_owned()).await
}
pub async fn get_projects_list_beta(&self) -> Result<Vec<project::ResponseBeta>> {
self.get("/projects".to_owned()).await
}

pub async fn stop_project(&self, project: &str) -> Result<project::Response> {
let path = format!("/projects/{project}");
Expand All @@ -215,23 +227,18 @@ impl ShuttleApiClient {
}

pub async fn delete_project(&self, project: &str) -> Result<String> {
let path = if self.beta {
format!("/projects/{project}")
} else {
format!("/projects/{project}/delete")
};

self.delete(path).await
self.delete(format!("/projects/{project}/delete")).await
}
pub async fn delete_project_beta(&self, project: &str) -> Result<String> {
self.delete(format!("/projects/{project}")).await
}

pub async fn get_teams_list(&self) -> Result<Vec<team::Response>> {
self.get("/teams".to_string()).await
}

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

self.get(path).await
self.get(format!("/teams/{team_id}/projects")).await
}

pub async fn get_logs(
Expand Down
6 changes: 3 additions & 3 deletions cargo-shuttle/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ use crate::args::TemplateLocation;
pub fn generate_project(
dest: PathBuf,
name: &str,
temp_loc: TemplateLocation,
temp_loc: &TemplateLocation,
no_git: bool,
) -> Result<()> {
println!(r#"Creating project "{name}" in "{}""#, dest.display());

let temp_dir: TempDir = setup_template(&temp_loc.auto_path)
.context("Failed to setup template generation directory")?;

let path = match temp_loc.subfolder {
let path = match temp_loc.subfolder.as_ref() {
Some(subfolder) => {
let path = temp_dir.path().join(&subfolder);
let path = temp_dir.path().join(subfolder);
if path.exists() {
path
} else {
Expand Down
Loading

0 comments on commit 43d7f28

Please sign in to comment.