Skip to content

Commit ddab191

Browse files
isaaclSchniz
andauthored
Add fnm default to return default version (#1268)
* Add `fnm default` to return default version Fixes #1196. * changeset * cargo fmt && cargo clippy --fix --------- Co-authored-by: Gal Schlezinger <[email protected]>
1 parent 4377824 commit ddab191

File tree

6 files changed

+32
-10
lines changed

6 files changed

+32
-10
lines changed

.changeset/odd-mayflies-poke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"fnm": patch
3+
---
4+
5+
Make `fnm default` with no trailing positional argument to return the default version

docs/commands.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Commands:
1414
completions Print shell completions to stdout
1515
alias Alias a version to a common name
1616
unalias Remove an alias definition
17-
default Set a version as the default version
17+
default Set a version as the default version or get the current default version
1818
current Print the current Node.js version
1919
exec Run a command within fnm context
2020
uninstall Uninstall a Node.js version [aliases: uni]
@@ -652,14 +652,14 @@ Options:
652652
# `fnm default`
653653

654654
```
655-
Set a version as the default version
655+
Set a version as the default version or get the current default version.
656656
657657
This is a shorthand for `fnm alias VERSION default`
658658
659-
Usage: fnm default [OPTIONS] <VERSION>
659+
Usage: fnm default [OPTIONS] [VERSION]
660660
661661
Arguments:
662-
<VERSION>
662+
[VERSION]
663663
664664
665665
Options:

src/alias.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ pub fn list_aliases(config: &FnmConfig) -> std::io::Result<Vec<StoredAlias>> {
3030
Ok(vec)
3131
}
3232

33+
pub fn get_alias_by_name(config: &FnmConfig, alias_name: &str) -> Option<StoredAlias> {
34+
let alias_path = config.aliases_dir().join(alias_name);
35+
TryInto::<StoredAlias>::try_into(alias_path.as_path()).ok()
36+
}
37+
3338
#[derive(Debug)]
3439
pub struct StoredAlias {
3540
alias_path: PathBuf,

src/cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub enum SubCommand {
4444
#[clap(name = "unalias", bin_name = "unalias")]
4545
Unalias(commands::unalias::Unalias),
4646

47-
/// Set a version as the default version
47+
/// Set a version as the default version or get the current default version.
4848
///
4949
/// This is a shorthand for `fnm alias VERSION default`
5050
#[clap(name = "default", bin_name = "default")]

src/commands/alias.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,6 @@ pub enum Error {
3838
VersionNotFound { version: UserVersion },
3939
#[error(transparent)]
4040
CantUnderstandVersion { source: ApplicableVersionError },
41+
#[error("A default version has not been set.")]
42+
DefaultAliasDoesNotExist,
4143
}

src/commands/default.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
use super::alias::Alias;
22
use super::command::Command;
3+
use crate::alias::get_alias_by_name;
34
use crate::config::FnmConfig;
45
use crate::user_version::UserVersion;
56

67
#[derive(clap::Parser, Debug)]
78
pub struct Default {
8-
version: UserVersion,
9+
version: Option<UserVersion>,
910
}
1011

1112
impl Command for Default {
1213
type Error = super::alias::Error;
1314

1415
fn apply(self, config: &FnmConfig) -> Result<(), Self::Error> {
15-
Alias {
16-
name: "default".into(),
17-
to_version: self.version,
16+
match self.version {
17+
Some(version) => Alias {
18+
name: "default".into(),
19+
to_version: version,
20+
}
21+
.apply(config),
22+
None => match get_alias_by_name(config, "default") {
23+
Some(alias) => {
24+
println!("{}", alias.s_ver());
25+
Ok(())
26+
}
27+
None => Err(Self::Error::DefaultAliasDoesNotExist),
28+
},
1829
}
19-
.apply(config)
2030
}
2131

2232
fn handle_error(err: Self::Error, config: &FnmConfig) {

0 commit comments

Comments
 (0)