Skip to content

Commit

Permalink
Improve env command
Browse files Browse the repository at this point in the history
  • Loading branch information
catuhana committed Sep 5, 2024
1 parent 8c57f5f commit 8893f4e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
5 changes: 5 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
if cfg!(windows) {
panic!("Nue is not supported on Windows.");
}
}
7 changes: 4 additions & 3 deletions resources/env.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# nue
#!/bin/sh
# nue shell setup
case ":${PATH}:" in
*:"$HOME/.nue/bin":*) ;;
*:"$HOME/.nue/node/bin":*) ;;
*)
export PATH="$HOME/.nue/node/bin:$PATH"
export PATH="$PATH:$HOME/.nue/node/bin"
;;
esac
47 changes: 46 additions & 1 deletion src/cli/env.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::{env, path::Path};

use clap::Args;
use tokio::fs;

use crate::globals::NUE_PATH;

use super::NueCommand;

Expand All @@ -7,8 +12,48 @@ pub struct CommandArguments;

impl NueCommand for CommandArguments {
async fn run(&self) -> anyhow::Result<()> {
print!("{}", include_str!("../../resources/env.sh"));
let environment_script = include_str!("../../resources/env.sh");

if !NUE_PATH.try_exists()? {
fs::create_dir_all(&*NUE_PATH).await?;
}

if !NUE_PATH.join("env").exists() {
fs::write(NUE_PATH.join("env"), environment_script).await?;
}

let mut possible_shell_profiles: Vec<&str> = vec!["~/.profile"];
match get_current_shell() {

Check warning on line 26 in src/cli/env.rs

View workflow job for this annotation

GitHub Actions / Check Code, Code Formatting and Linting (stable)

you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`

Check warning on line 26 in src/cli/env.rs

View workflow job for this annotation

GitHub Actions / Check Code, Code Formatting and Linting (nightly)

you seem to be trying to use `match` for destructuring a single pattern. Consider using `if let`
Some(shell) => match shell.as_str() {
"zsh" => {
possible_shell_profiles.append(&mut vec![
"~/.zprofile",
"~/.zshenv",
"~/.zshrc",
]);
}
"bash" => {
possible_shell_profiles.append(&mut vec!["~/.bash_profile", "~/.bashrc"]);
}
_ => {}
},
None => {}
}

println!(
"Created env script at `$HOME/.nue/env`. Source it in your shell profile ({}) to use nue.",
possible_shell_profiles.join(", ")
);

Ok(())
}
}

fn get_current_shell() -> Option<String> {
env::var("SHELL").ok().and_then(|shell_path| {
Path::new(&shell_path)
.file_name()
.and_then(|os_str| os_str.to_str())
.map(|s| s.to_string())
})
}

0 comments on commit 8893f4e

Please sign in to comment.