Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fnm default to return default version #1268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
8 changes: 4 additions & 4 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Commands:
completions Print shell completions to stdout
alias Alias a version to a common name
unalias Remove an alias definition
default Set a version as the default version
default Set a version as the default version or get the current default version
current Print the current Node.js version
exec Run a command within fnm context
uninstall Uninstall a Node.js version
Expand Down Expand Up @@ -607,14 +607,14 @@ Options:
# `fnm default`

```
Set a version as the default version
Set a version as the default version or get the current default version.

This is a shorthand for `fnm alias VERSION default`

Usage: fnm default [OPTIONS] <VERSION>
Usage: fnm default [OPTIONS] [VERSION]

Arguments:
<VERSION>
[VERSION]


Options:
Expand Down
5 changes: 5 additions & 0 deletions src/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ pub fn list_aliases(config: &FnmConfig) -> std::io::Result<Vec<StoredAlias>> {
Ok(vec)
}

pub fn get_alias_by_name(config: &FnmConfig, alias_name: &str) -> Option<StoredAlias> {
let alias_path = config.aliases_dir().join(alias_name);
TryInto::<StoredAlias>::try_into(alias_path.as_path()).ok()
}

#[derive(Debug)]
pub struct StoredAlias {
alias_path: PathBuf,
Expand Down
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub enum SubCommand {
#[clap(name = "unalias", bin_name = "unalias")]
Unalias(commands::unalias::Unalias),

/// Set a version as the default version
/// Set a version as the default version or get the current default version.
///
/// This is a shorthand for `fnm alias VERSION default`
#[clap(name = "default", bin_name = "default")]
Expand Down
2 changes: 2 additions & 0 deletions src/commands/alias.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ pub enum Error {
VersionNotFound { version: UserVersion },
#[error(transparent)]
CantUnderstandVersion { source: ApplicableVersionError },
#[error("A default version has not been set.")]
DefaultAliasDoesNotExist,
}
22 changes: 17 additions & 5 deletions src/commands/default.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,34 @@
use super::alias::Alias;
use super::command::Command;
use crate::alias::get_alias_by_name;
use crate::config::FnmConfig;
use crate::user_version::UserVersion;

#[derive(clap::Parser, Debug)]
pub struct Default {
version: UserVersion,
version: Option<UserVersion>,
}

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

fn apply(self, config: &FnmConfig) -> Result<(), Self::Error> {
Alias {
name: "default".into(),
to_version: self.version,
match self.version {
Some(version) => Alias {
name: "default".into(),
to_version: version,
}
.apply(config),
None => {
match get_alias_by_name(config, "default") {
Some(alias) => {
println!("{}", alias.s_ver());
return Ok(());
}
None => Err(Self::Error::DefaultAliasDoesNotExist),
}
}
}
.apply(config)
}

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