Skip to content

Commit 146f67e

Browse files
authored
fix: support registry shortcuts (#234)
1 parent 647530a commit 146f67e

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

src/cli.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,26 @@ pub enum RegistryCommands {
180180
#[clap(about = "Show current binary registry and version registry")]
181181
Show,
182182

183+
#[clap(name = "official", about = "Set registry to official registry")]
184+
Official {
185+
#[clap(
186+
long = "write-local",
187+
short = 'L',
188+
help = "Write to current directory .dvmrc file instead of global(user-wide) config"
189+
)]
190+
write_local: bool,
191+
},
192+
193+
#[clap(name = "cn", about = "Set registry to cn registry")]
194+
Cn {
195+
#[clap(
196+
long = "write-local",
197+
short = 'L',
198+
help = "Write to current directory .dvmrc file instead of global(user-wide) config"
199+
)]
200+
write_local: bool,
201+
},
202+
183203
#[clap(about = "Set registry to one of predefined registries")]
184204
Set {
185205
predefined: RegistryPredefined,
@@ -283,3 +303,27 @@ impl RegistryPredefined {
283303
.to_string()
284304
}
285305
}
306+
307+
#[cfg(test)]
308+
mod tests {
309+
use super::*;
310+
311+
#[test]
312+
fn parses_registry_predefined_shortcuts() {
313+
let cli = Cli::try_parse_from(["dvm", "registry", "cn"]).unwrap();
314+
match cli.command {
315+
Commands::Registry {
316+
command: RegistryCommands::Cn { write_local },
317+
} => assert!(!write_local),
318+
_ => panic!("expected registry cn shortcut"),
319+
}
320+
321+
let cli = Cli::try_parse_from(["dvm", "registry", "official", "--write-local"]).unwrap();
322+
match cli.command {
323+
Commands::Registry {
324+
command: RegistryCommands::Official { write_local },
325+
} => assert!(write_local),
326+
_ => panic!("expected registry official shortcut"),
327+
}
328+
}
329+
}

src/commands/registry.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ pub fn exec(meta: &mut DvmMeta, registry: RegistryCommands) -> Result<()> {
3535
println!(" binary_registry\t{}", rc_binary_registry);
3636
println!(" version_registry\t{}", rc_version_registry);
3737
}
38+
RegistryCommands::Official { write_local } => {
39+
rc_update(write_local, DVM_CONFIGRC_KEY_REGISTRY_BINARY, REGISTRY_OFFICIAL)?;
40+
rc_update(write_local, DVM_CONFIGRC_KEY_REGISTRY_VERSION, REGISTRY_LIST_OFFICIAL)?;
41+
}
42+
RegistryCommands::Cn { write_local } => {
43+
rc_update(write_local, DVM_CONFIGRC_KEY_REGISTRY_BINARY, REGISTRY_CN)?;
44+
rc_update(write_local, DVM_CONFIGRC_KEY_REGISTRY_VERSION, REGISTRY_LIST_CN)?;
45+
}
3846
RegistryCommands::Set {
3947
predefined,
4048
write_local,

0 commit comments

Comments
 (0)