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

Added json option to env command #800

Merged
merged 21 commits into from
Nov 17, 2022
Merged
Changes from 1 commit
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
84 changes: 47 additions & 37 deletions src/commands/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ pub struct Env {
#[clap(long)]
#[clap(possible_values = AVAILABLE_SHELLS)]
shell: Option<Box<dyn Shell>>,
/// Print JSON instead of shell commands.
#[clap(long)]
json: bool,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably make it broader, so --output=json is preferable. --output=json conflicts with --shell=ANY which can be declared with #[clap(conflicts_with = "shell")]

That being said, --shell=... is practically --output=..., right? so maybe we can leverage --shell= to be used as --shell=json? Kinda ugly naming but does not break and does not introduce another API complexity?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I didn't know about conflicts_with. Thanks!

Haha I almost did --shell=json. Maybe deprecating --shell and replace it with --output? That reads well in my head.

fnm env --output=bash

Then for compatbility sake just have --shell map to --output? Is there an option for that in clap?

/// Deprecated. This is the default now.
#[clap(long, hide = true)]
multi: bool,
Expand Down Expand Up @@ -45,6 +48,17 @@ fn make_symlink(config: &FnmConfig) -> Result<std::path::PathBuf, Error> {
}
}

#[derive(serde::Serialize, serde::Deserialize)]
#[allow(non_snake_case)]
struct EnvVars {
FNM_MULTISHELL_PATH: String,
FNM_VERSION_FILE_STRATEGY: String,
FNM_DIR: String,
FNM_LOGLEVEL: String,
FNM_NODE_DIST_MIRROR: String,
FNM_ARCH: String,
}
zaucy marked this conversation as resolved.
Show resolved Hide resolved

impl Command for Env {
type Error = Error;

Expand All @@ -59,50 +73,46 @@ impl Command for Env {
);
}

let shell: Box<dyn Shell> = self
.shell
.or_else(&infer_shell)
.ok_or(Error::CantInferShell)?;
let multishell_path = make_symlink(config)?;
let binary_path = if cfg!(windows) {
multishell_path.clone()
} else {
multishell_path.join("bin")
};
println!("{}", shell.path(&binary_path)?);
println!(
"{}",
shell.set_env_var("FNM_MULTISHELL_PATH", multishell_path.to_str().unwrap())
);
println!(
"{}",
shell.set_env_var(
"FNM_VERSION_FILE_STRATEGY",
config.version_file_strategy().as_str()
)
);
println!(
"{}",
shell.set_env_var("FNM_DIR", config.base_dir_with_default().to_str().unwrap())
);
println!(
"{}",
shell.set_env_var("FNM_LOGLEVEL", config.log_level().clone().into())
);
println!(
"{}",
shell.set_env_var("FNM_NODE_DIST_MIRROR", config.node_dist_mirror.as_str())
);
println!(
"{}",
shell.set_env_var("FNM_ARCH", &config.arch.to_string())
);
if self.use_on_cd {
println!("{}", shell.use_on_cd(config)?);
}
if let Some(v) = shell.rehash() {
println!("{}", v);

let env_vars = EnvVars{
FNM_MULTISHELL_PATH: multishell_path.to_str().unwrap().to_owned(),
FNM_VERSION_FILE_STRATEGY: config.version_file_strategy().as_str().to_owned(),
FNM_DIR: config.base_dir_with_default().to_str().unwrap().to_owned(),
FNM_LOGLEVEL: <&'static str>::from(config.log_level().clone()).to_owned(),
FNM_NODE_DIST_MIRROR: config.node_dist_mirror.as_str().to_owned(),
FNM_ARCH: config.arch.to_string(),
};
zaucy marked this conversation as resolved.
Show resolved Hide resolved

if self.json {
println!("{}", serde_json::to_string(&env_vars).unwrap());
} else {
let shell: Box<dyn Shell> = self
.shell
.or_else(&infer_shell)
.ok_or(Error::CantInferShell)?;
println!("{}", shell.path(&binary_path)?);

println!("{}", shell.set_env_var("FNM_MULTISHELL_PATH", &env_vars.FNM_MULTISHELL_PATH));
println!("{}", shell.set_env_var("FNM_VERSION_FILE_STRATEGY", &env_vars.FNM_VERSION_FILE_STRATEGY));
println!("{}", shell.set_env_var("FNM_DIR", &env_vars.FNM_DIR));
println!("{}", shell.set_env_var("FNM_LOGLEVEL", &env_vars.FNM_LOGLEVEL));
println!("{}", shell.set_env_var("FNM_NODE_DIST_MIRROR", &env_vars.FNM_NODE_DIST_MIRROR));
println!("{}", shell.set_env_var("FNM_ARCH", &env_vars.FNM_ARCH));
zaucy marked this conversation as resolved.
Show resolved Hide resolved

if self.use_on_cd {
println!("{}", shell.use_on_cd(config)?);
}
if let Some(v) = shell.rehash() {
println!("{}", v);
}
}

Ok(())
}
}
Expand Down