Skip to content

Commit c9d93ea

Browse files
authored
feat(cargo-shuttle): separate --id and --name. support 'init --id' with immediate linking (#2069)
* feat: separate --id and --name. support init --id with immediate linking * . * refa * fix test
1 parent 8c3c1ea commit c9d93ea

File tree

6 files changed

+144
-132
lines changed

6 files changed

+144
-132
lines changed

cargo-shuttle/src/args.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,12 @@ pub struct ProjectArgs {
7373
/// Specify the working directory
7474
#[arg(global = true, long, visible_alias = "wd", default_value = ".", value_parser = OsStringValueParser::new().try_map(parse_path))]
7575
pub working_directory: PathBuf,
76-
/// Specify the name or id of the project
77-
#[arg(global = true, long = "name", visible_alias = "id")]
78-
pub name_or_id: Option<String>,
76+
/// Specify the name of the project to target or create
77+
#[arg(global = true, long)]
78+
pub name: Option<String>,
79+
/// Specify the id of the project to target
80+
#[arg(global = true, long)]
81+
pub id: Option<String>,
7982
}
8083

8184
impl ProjectArgs {
@@ -411,9 +414,9 @@ pub struct InitArgs {
411414
/// Don't check the project name's validity or availability and use it anyways
412415
#[arg(long)]
413416
pub force_name: bool,
414-
/// Whether to start the container for this project on Shuttle, and claim the project name
415-
#[arg(long)]
416-
pub create_env: bool,
417+
/// Whether to create a project on Shuttle
418+
#[arg(long, visible_alias = "create_env")]
419+
pub create_project: bool,
417420
/// Don't initialize a new git repository
418421
#[arg(long)]
419422
pub no_git: bool,
@@ -640,7 +643,8 @@ mod tests {
640643
fn workspace_path() {
641644
let project_args = ProjectArgs {
642645
working_directory: path_from_workspace_root("examples/axum/hello-world/src"),
643-
name_or_id: None,
646+
name: None,
647+
id: None,
644648
};
645649

646650
assert_eq!(
@@ -653,7 +657,8 @@ mod tests {
653657
fn project_name() {
654658
let project_args = ProjectArgs {
655659
working_directory: path_from_workspace_root("examples/axum/hello-world/src"),
656-
name_or_id: None,
660+
name: None,
661+
id: None,
657662
};
658663

659664
assert_eq!(
@@ -668,7 +673,8 @@ mod tests {
668673
working_directory: path_from_workspace_root(
669674
"examples/rocket/workspace/hello-world/src",
670675
),
671-
name_or_id: None,
676+
name: None,
677+
id: None,
672678
};
673679

674680
assert_eq!(

cargo-shuttle/src/config.rs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ impl RequestContext {
9696
})
9797
}
9898

99-
pub fn load_local_internal(&mut self, project_args: &ProjectArgs) -> Result<()> {
99+
pub fn load_local_internal_config(&mut self, project_args: &ProjectArgs) -> Result<()> {
100100
let workspace_path = project_args
101101
.workspace_path()
102102
.unwrap_or(project_args.working_directory.clone());
@@ -119,10 +119,10 @@ impl RequestContext {
119119
let config = project_internal.as_mut().unwrap();
120120

121121
// Project id is preferred in this order:
122-
// 1. Name given on command line
123-
// 2. Name from .shuttle/config.toml file
124-
match (&project_args.name_or_id, &config.id) {
125-
// Command-line name parameter trumps everything
122+
// 1. Id given on command line
123+
// 2. Id from .shuttle/config.toml file
124+
match (&project_args.id, &config.id) {
125+
// Command-line id parameter trumps everything
126126
(Some(id_from_args), _) => {
127127
trace!("using command-line project id");
128128
config.id = Some(id_from_args.clone());
@@ -167,21 +167,16 @@ impl RequestContext {
167167
Ok(())
168168
}
169169

170-
/// Load the project configuration at the given `working_directory`
171-
///
172-
/// Ensures that if `--name` is not specified on the command-line, and either the project
173-
/// file does not exist, or it has not set the `name` key then the `ProjectConfig` instance
174-
/// has `ProjectConfig.name = Some("crate-name")`.
175-
pub fn load_local(&mut self, project_args: &ProjectArgs) -> Result<()> {
176-
// Shuttle.toml
170+
/// Load the Shuttle.toml project configuration at the given `working_directory`
171+
pub fn load_local_config(&mut self, project_args: &ProjectArgs) -> Result<()> {
177172
let project = Self::get_local_config(project_args)?;
178173

179174
self.project = Some(project);
180175

181176
Ok(())
182177
}
183178

184-
pub fn get_local_config(
179+
fn get_local_config(
185180
project_args: &ProjectArgs,
186181
) -> Result<Config<LocalConfigManager, ProjectConfig>> {
187182
let workspace_path = project_args
@@ -209,7 +204,7 @@ impl RequestContext {
209204
// 2. Name from Shuttle.toml file
210205
// 3. Name from Cargo.toml package if it's a crate
211206
// 4. Name from the workspace directory if it's a workspace
212-
match (&project_args.name_or_id, &config.name) {
207+
match (&project_args.name, &config.name) {
213208
// Command-line name parameter trumps everything
214209
(Some(name_from_args), _) => {
215210
trace!("using command-line project name");
@@ -379,7 +374,8 @@ mod tests {
379374
fn get_local_config_finds_name_in_cargo_toml() {
380375
let project_args = ProjectArgs {
381376
working_directory: path_from_workspace_root("examples/axum/hello-world/"),
382-
name_or_id: None,
377+
name: None,
378+
id: None,
383379
};
384380

385381
let local_config = RequestContext::get_local_config(&project_args).unwrap();
@@ -391,7 +387,8 @@ mod tests {
391387
fn get_local_config_finds_name_from_workspace_dir() {
392388
let project_args = ProjectArgs {
393389
working_directory: path_from_workspace_root("examples/rocket/workspace/hello-world/"),
394-
name_or_id: None,
390+
name: None,
391+
id: None,
395392
};
396393

397394
let local_config = RequestContext::get_local_config(&project_args).unwrap();
@@ -403,7 +400,8 @@ mod tests {
403400
fn setting_name_overrides_name_in_config() {
404401
let project_args = ProjectArgs {
405402
working_directory: path_from_workspace_root("examples/axum/hello-world/"),
406-
name_or_id: Some("my-fancy-project-name".to_owned()),
403+
name: Some("my-fancy-project-name".to_owned()),
404+
id: None,
407405
};
408406

409407
let local_config = RequestContext::get_local_config(&project_args).unwrap();

0 commit comments

Comments
 (0)