Skip to content

Commit

Permalink
Allow init --skip-env and --only-env
Browse files Browse the repository at this point in the history
In (some?) shells, it's sometimes preferable to set up env vars in a
different place than other configuration like most of what `atuin init`
sets up. For example, ZSH reads ~/.zshenv for all shells while ~/.zshrc
is only read for interactive shells.

This allows splitting the output of init such that in these two
different config files we could load environment variables and load all
other config seperately:

~/.zshenv:

    eval "$(atuin init zsh --env-only)"

~/.zshrc:

    eval "$(atuin init zsh --skip-env --disable-up-arrow)"

Which means that when launching noninteractive shells (I do this
frequently for running tests from Vim, for example, but it's more
generally useful) the specified env values will be sourced and we won't
redefine them in login shells.

\## Checks
- [x] I am happy for maintainers to push small adjustments to this PR,
      to speed up the review cycle
- [x] I have checked that there are no existing pull requests for the same thing

Note: I know I didn't create an issue or post anywhere else first. If
this is totally not a desired feature my feelings won't be hurt if you
say so and close this.
  • Loading branch information
calebhearth committed Aug 26, 2024
1 parent 72a562a commit a10c47b
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 20 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ All notable changes to this project will be documented in this file.
- *(gui)* Background terminals and more ([#2303](https://github.com/atuinsh/atuin/issues/2303))
- *(gui)* Clean up home page, fix a few bugs ([#2304](https://github.com/atuinsh/atuin/issues/2304))
- *(history)* Filter out various environment variables containing potential secrets ([#2174](https://github.com/atuinsh/atuin/issues/2174))
- *(init)* Allow splitting environment variable loading from remainder of setup
- *(tui)* Configurable prefix character ([#2157](https://github.com/atuinsh/atuin/issues/2157))
- *(tui)* Customizable Themes ([#2236](https://github.com/atuinsh/atuin/issues/2236))
- *(tui)* Fixed preview height option ([#2286](https://github.com/atuinsh/atuin/issues/2286))
Expand Down
16 changes: 16 additions & 0 deletions crates/atuin/src/command/client/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ pub struct Cmd {
/// Disable the binding of the Up Arrow key to atuin
#[clap(long)]
disable_up_arrow: bool,

/// Only import environment variables (useful for ~/.zsh_env or ~/.bash_env)
#[clap(long)]
only_env: bool,

/// Skip importing environment variables (useful if already imported)
#[clap(long)]
skip_env: bool,
}

#[derive(Clone, Copy, ValueEnum, Debug)]
Expand Down Expand Up @@ -122,6 +130,8 @@ $env.config = (
var_store,
self.disable_up_arrow,
self.disable_ctrl_r,
self.only_env,
self.skip_env,
)
.await?;
}
Expand All @@ -131,6 +141,8 @@ $env.config = (
var_store,
self.disable_up_arrow,
self.disable_ctrl_r,
self.only_env,
self.skip_env,
)
.await?;
}
Expand All @@ -140,6 +152,8 @@ $env.config = (
var_store,
self.disable_up_arrow,
self.disable_ctrl_r,
self.only_env,
self.skip_env,
)
.await?;
}
Expand All @@ -150,6 +164,8 @@ $env.config = (
var_store,
self.disable_up_arrow,
self.disable_ctrl_r,
self.only_env,
self.skip_env,
)
.await?;
}
Expand Down
16 changes: 11 additions & 5 deletions crates/atuin/src/command/client/init/bash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ pub async fn init(
vars: VarStore,
disable_up_arrow: bool,
disable_ctrl_r: bool,
only_env: bool,
skip_env: bool,
) -> Result<()> {
init_static(disable_up_arrow, disable_ctrl_r);
if !only_env {
init_static(disable_up_arrow, disable_ctrl_r);

let aliases = atuin_dotfiles::shell::bash::alias_config(&aliases).await;
let vars = atuin_dotfiles::shell::bash::var_config(&vars).await;
let aliases = atuin_dotfiles::shell::bash::alias_config(&aliases).await;
println!("{aliases}");
}

println!("{aliases}");
println!("{vars}");
if !skip_env {
let vars = atuin_dotfiles::shell::bash::var_config(&vars).await;
println!("{vars}");
}

Ok(())
}
16 changes: 11 additions & 5 deletions crates/atuin/src/command/client/init/fish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,20 @@ pub async fn init(
vars: VarStore,
disable_up_arrow: bool,
disable_ctrl_r: bool,
only_env: bool,
skip_env: bool,
) -> Result<()> {
init_static(disable_up_arrow, disable_ctrl_r);
if !only_env {
init_static(disable_up_arrow, disable_ctrl_r);

let aliases = atuin_dotfiles::shell::fish::alias_config(&aliases).await;
let vars = atuin_dotfiles::shell::fish::var_config(&vars).await;
let aliases = atuin_dotfiles::shell::fish::alias_config(&aliases).await;
println!("{aliases}");
}

println!("{aliases}");
println!("{vars}");
if !skip_env {
let vars = atuin_dotfiles::shell::fish::var_config(&vars).await;
println!("{vars}");
}

Ok(())
}
16 changes: 11 additions & 5 deletions crates/atuin/src/command/client/init/xonsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,20 @@ pub async fn init(
vars: VarStore,
disable_up_arrow: bool,
disable_ctrl_r: bool,
only_env: bool,
skip_env: bool,
) -> Result<()> {
init_static(disable_up_arrow, disable_ctrl_r);
if !only_env {
init_static(disable_up_arrow, disable_ctrl_r);

let aliases = atuin_dotfiles::shell::xonsh::alias_config(&aliases).await;
let vars = atuin_dotfiles::shell::xonsh::var_config(&vars).await;
let aliases = atuin_dotfiles::shell::xonsh::alias_config(&aliases).await;
println!("{aliases}");
}

println!("{aliases}");
println!("{vars}");
if !skip_env {
let vars = atuin_dotfiles::shell::xonsh::var_config(&vars).await;
println!("{vars}");
}

Ok(())
}
16 changes: 11 additions & 5 deletions crates/atuin/src/command/client/init/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,20 @@ pub async fn init(
vars: VarStore,
disable_up_arrow: bool,
disable_ctrl_r: bool,
only_env: bool,
skip_env: bool,
) -> Result<()> {
init_static(disable_up_arrow, disable_ctrl_r);
if !only_env {
init_static(disable_up_arrow, disable_ctrl_r);

let aliases = atuin_dotfiles::shell::zsh::alias_config(&aliases).await;
let vars = atuin_dotfiles::shell::zsh::var_config(&vars).await;
let aliases = atuin_dotfiles::shell::zsh::alias_config(&aliases).await;
println!("{aliases}");
}

println!("{aliases}");
println!("{vars}");
if !skip_env {
let vars = atuin_dotfiles::shell::zsh::var_config(&vars).await;
println!("{vars}");
}

Ok(())
}

0 comments on commit a10c47b

Please sign in to comment.