Skip to content

Commit 584df01

Browse files
authored
breaking: rename alias default to active (#89)
1 parent 39bfdab commit 584df01

File tree

7 files changed

+31
-20
lines changed

7 files changed

+31
-20
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
## [0.8.0]
44

5+
### Breaking
6+
7+
- Rename active version alias from `default` to `active` [#89](https://github.com/numToStr/snm/pull/89)
8+
9+
> If you are upgrading from an older version, `snm` will not recognize the running version. To fix this just run the `use` command with a version of your choice.
10+
511
### Changes
612

713
- Rename `prune` command to `purge` but retain `prune` as an alias [#82](https://github.com/numToStr/snm/pull/82)

src/cli/config.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ impl Config {
7272
}
7373

7474
pub fn alias_default(&self) -> AliasDir {
75-
let p = self.alias_dir().as_ref().join(UserAlias::DEFAULT);
76-
77-
AliasDir::new(p)
75+
self.alias_dir().join(UserAlias::ACTIVE)
7876
}
7977

8078
pub fn bin_path(&self, path: &Path) -> PathBuf {

src/commands/ls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl super::Command for Ls {
1717
for version in versions.into_iter() {
1818
match aliases.get(&version) {
1919
Some(a) => {
20-
if a.contains(&UserAlias::DEFAULT.to_string()) {
20+
if a.iter().any(|x| *x == UserAlias::ACTIVE) {
2121
println!("> {} \t{}", style(version).bold(), a.join(", "));
2222
} else {
2323
println!("- {} \t{}", version, a.join(", "));

src/commands/purge.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,13 @@ impl super::Command for Purge {
3131
if !default_alias.as_ref().exists() {
3232
anyhow::bail!(
3333
"Unable to prune. No {} alias found",
34-
style(UserAlias::DEFAULT).bold()
34+
style(UserAlias::ACTIVE).bold()
3535
);
3636
}
3737

3838
let release_dir = config.release_dir();
3939

40-
let used_ver = Linker::read_convert_to_dist(&default_alias, &release_dir)?;
40+
let active_ver = Linker::read_convert_to_dist(&default_alias, &release_dir)?;
4141

4242
// Nuke the alias directory after reading the default alias
4343
remove_dir_all(config.alias_dir().as_ref())?;
@@ -49,7 +49,7 @@ impl super::Command for Purge {
4949
let dist_versions = DistVersion::list_versions(&release_dir)?;
5050
for version in dist_versions {
5151
// If the version is currently active then don't delete
52-
if version.eq(&used_ver) {
52+
if version.eq(&active_ver) {
5353
continue;
5454
}
5555

@@ -63,7 +63,7 @@ impl super::Command for Purge {
6363
// Restoring the default alias
6464
// NOTE: don't use the `default_alias` variable
6565
Linker::create_link(
66-
&release_dir.join(used_ver.to_string()),
66+
&release_dir.join(active_ver.to_string()),
6767
&config.alias_default(),
6868
)?;
6969

src/commands/uninstall.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ use snm_core::{
1111
#[derive(Debug, Clap)]
1212
pub struct UnInstall {
1313
/// Semver, Alias or Lts codename that needs to be removed
14-
version_or_alias: UserVersion,
14+
version: UserVersion,
1515

16-
/// Don't remove if the version is currently used.
16+
/// Don't remove if the version is currently active.
1717
#[clap(short = 'N', long)]
18-
no_used: bool,
18+
no_active: bool,
1919
}
2020

2121
impl super::Command for UnInstall {
@@ -25,15 +25,15 @@ impl super::Command for UnInstall {
2525

2626
// first we need to find out the whether the provided version is an alias, lts codename or partial semver
2727
// If the version is alias or codename, then we need to find the linked/installed version
28-
let version = match &self.version_or_alias {
28+
let version = match &self.version {
2929
UserVersion::Lts(lts_code) => {
3030
let alias_ver = alias_dir.join(&lts_code.to_string());
3131

3232
if !alias_ver.as_ref().exists() {
3333
anyhow::bail!("Codename {} not found", style(lts_code).bold());
3434
}
3535

36-
Linker::read_convert_to_dist(&alias_dir, &release_dir)?
36+
Linker::read_convert_to_dist(&alias_ver, &release_dir)?
3737
}
3838
UserVersion::Alias(alias) => {
3939
let alias_ver = alias_dir.join(alias.as_ref());
@@ -42,7 +42,7 @@ impl super::Command for UnInstall {
4242
anyhow::bail!("Alias {} not found", style(alias).bold());
4343
}
4444

45-
Linker::read_convert_to_dist(&alias_dir, &release_dir)?
45+
Linker::read_convert_to_dist(&alias_ver, &release_dir)?
4646
}
4747
x => DistVersion::match_version(&release_dir, x)?,
4848
};
@@ -51,12 +51,12 @@ impl super::Command for UnInstall {
5151
// then remove them all the aliases before removing the actuall installed version
5252
let aliases = Linker::list_for_version(&version, &alias_dir, &release_dir)?;
5353

54-
// Checking whether the version is currently used or not
55-
let is_default = aliases.iter().any(|x| x.as_str() == UserAlias::DEFAULT);
54+
// Checking whether the version is currently active or not
55+
let is_default = aliases.iter().any(|x| *x == UserAlias::ACTIVE);
5656

57-
if is_default && self.no_used {
57+
if is_default && self.no_active {
5858
anyhow::bail!(
59-
"Unable to uninstall. Version {} is currently used!",
59+
"Unable to uninstall. Version {} is currently active!",
6060
style(version).bold()
6161
);
6262
}

src/lib/types.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use std::{
44
str::FromStr,
55
};
66

7+
use console::style;
8+
79
macro_rules! as_ref {
810
($impl: ident, $ty: ty) => {
911
impl AsRef<$ty> for $impl {
@@ -54,7 +56,7 @@ as_ref!(UserLts, str);
5456
pub struct UserAlias(String);
5557

5658
impl UserAlias {
57-
pub const DEFAULT: &'static str = "default";
59+
pub const ACTIVE: &'static str = "active";
5860

5961
pub fn new(s: &str) -> Self {
6062
Self(s.replace('/', "-").replace('\\', "-"))
@@ -72,6 +74,10 @@ impl Display for UserAlias {
7274
impl FromStr for UserAlias {
7375
type Err = anyhow::Error;
7476
fn from_str(s: &str) -> Result<Self, Self::Err> {
77+
if s == Self::ACTIVE {
78+
anyhow::bail!("{} is not allowed", style(Self::ACTIVE).bold())
79+
}
80+
7581
Ok(Self::new(s))
7682
}
7783
}

src/lib/version/user_version.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ impl ParseVersion for UserVersion {
5454
} else if UserLts::is_lts(v) {
5555
Self::Lts(UserLts::new(v))
5656
} else {
57-
Self::Alias(UserAlias::new(v))
57+
// from_str method is used bcz it returns err if the given alias is same as active alias
58+
Self::Alias(UserAlias::from_str(v)?)
5859
}
5960
};
6061

0 commit comments

Comments
 (0)