Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions cargo-shuttle/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,25 @@ impl RequestContext {
InternalProjectConfig { id: Some(id) };
}

pub fn remove_project_id(&mut self) {
*self.project_internal.as_mut().unwrap().as_mut().unwrap() =
InternalProjectConfig { id: None };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's better to modify the id field in place instead of replacing the config (in case more fields are added). Same is true for set_project_id

}

pub fn linked_project_id(&self, project_args: &ProjectArgs) -> Option<String> {
let workspace_path = project_args
.workspace_path()
.unwrap_or(project_args.working_directory.clone());

let local_manager =
LocalConfigManager::new(workspace_path, ".shuttle/config.toml".to_string());
let mut config = Config::new(local_manager);
config.open().ok()?;
let content: &InternalProjectConfig = config.as_ref().unwrap();

content.id.clone()
}

pub fn save_local_internal(&mut self) -> Result<()> {
self.project_internal.as_ref().unwrap().save()?;

Expand Down
41 changes: 32 additions & 9 deletions cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,15 @@ impl Shuttle {
} => self.delete_certificate(domain, yes).await,
},
Command::Project(cmd) => match cmd {
ProjectCommand::Create => self.project_create(args.project_args.name).await,
ProjectCommand::Create => self.project_create(&args.project_args).await,
ProjectCommand::Update(cmd) => match cmd {
ProjectUpdateCommand::Name { new_name } => self.project_rename(new_name).await,
},
ProjectCommand::Status => self.project_status().await,
ProjectCommand::List { table, .. } => self.projects_list(table).await,
ProjectCommand::Delete(ConfirmationArgs { yes }) => self.project_delete(yes).await,
ProjectCommand::Delete(ConfirmationArgs { yes }) => {
self.project_delete(yes, &args.project_args).await
}
ProjectCommand::Link => Ok(()), // logic is done in `load_project_id` in previous step
},
Command::Upgrade { preview } => update_cargo_shuttle(preview).await,
Expand Down Expand Up @@ -1929,24 +1931,34 @@ impl Shuttle {
Ok(())
}

async fn project_create(&self, name: Option<String>) -> Result<()> {
let Some(ref name) = name else {
async fn project_create(&mut self, project_args: &ProjectArgs) -> Result<()> {
let Some(ref name) = project_args.name else {
bail!("Provide a project name with '--name <name>'");
};

self.ctx.load_local_internal_config(project_args)?;

let client = self.client.as_ref().unwrap();
let r = client.create_project(name).await?;
let (proj, raw_json) = client.create_project(name).await?.into_parts();

match self.output_mode {
OutputMode::Normal => {
let project = r.into_inner();
println!("Created project '{}' with id {}", project.name, project.id);
println!("Created project '{}' with id {}", proj.name, proj.id);
}
OutputMode::Json => {
println!("{}", r.raw_json);
println!("{}", raw_json);
}
}

// self.ctx.project needs to be loaded for updating gitignore
self.ctx.load_local_config(project_args)?;

// Update the local internal config file if we are in a Rust project
if project_args.workspace_path().is_ok() {
self.ctx.set_project_id(proj.id);
self.ctx.save_local_internal()?;
}

Ok(())
}

Expand Down Expand Up @@ -2028,7 +2040,7 @@ impl Shuttle {
Ok(())
}

async fn project_delete(&self, no_confirm: bool) -> Result<()> {
async fn project_delete(&mut self, no_confirm: bool, project_args: &ProjectArgs) -> Result<()> {
let client = self.client.as_ref().unwrap();
let pid = self.ctx.project_id();

Expand Down Expand Up @@ -2064,6 +2076,17 @@ impl Shuttle {

let res = client.delete_project(pid).await?.into_inner();

if self
.ctx
.linked_project_id(project_args)
.as_ref()
.map(|id| id.as_str() == self.ctx.project_id())
.unwrap_or(false)
{
self.ctx.remove_project_id();
self.ctx.save_local_internal()?;
}

println!("{res}");

Ok(())
Expand Down