Skip to content

Commit 9013416

Browse files
authored
Merge pull request #135 from Shopify/no-hookbook
Add --no-hookbook flag to init
2 parents 1dcf45c + c16c872 commit 9013416

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

sh/shadowenv.bash.in

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ __shadowenv_hook() {
1111
# meaning we need a subshell, so we can't just let shadowenv look up its ppid.
1212
eval "$("@SELF@" hook "${flags[@]}")"
1313
}
14-
1514
@HOOKBOOK@
16-
1715
__shadowenv_force_run=1
1816
hookbook_add_hook __shadowenv_hook

sh/shadowenv.zsh.in

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ __shadowenv_hook() {
99
fi
1010
"@SELF@" hook "${flags[@]}" | source /dev/stdin
1111
}
12-
1312
@HOOKBOOK@
14-
1513
__shadowenv_force_run=1
1614
hookbook_add_hook __shadowenv_hook

src/cli.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,15 +92,23 @@ pub struct TrustCmd {}
9292
#[clap(disable_help_subcommand = true)]
9393
pub enum InitCmd {
9494
/// Prints a script which can be eval'd by bash to set up shadowenv.
95-
Bash,
95+
Bash(InitOptions),
9696

9797
/// Prints a script which can be eval'd by zsh to set up shadowenv.
98-
Zsh,
98+
Zsh(InitOptions),
9999

100100
/// Prints a script which can be eval'd by fish to set up shadowenv.
101101
Fish,
102102
}
103103

104+
/// Options shared by all init subcommands
105+
#[derive(Args, Debug)]
106+
pub struct InitOptions {
107+
/// Don't print hookbook inline (it's still required -- only use if you've already loaded it)
108+
#[arg(long)]
109+
pub no_hookbook: bool,
110+
}
111+
104112
/// Print a little glyph you can include in a shell prompt to indicate that shadowenv is active.
105113
#[derive(clap::Args, Debug)]
106114
pub struct PromptWidgetCmd {}

src/init.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,38 @@ use std::path::PathBuf;
66
pub fn run(cmd: InitCmd) {
77
let pb = std::env::current_exe().unwrap(); // this would be... an unusual failure.
88
match cmd {
9-
Bash => print_script(pb, include_bytes!("../sh/shadowenv.bash.in")),
10-
Zsh => print_script(pb, include_bytes!("../sh/shadowenv.zsh.in")),
11-
Fish => print_script(pb, include_bytes!("../sh/shadowenv.fish.in")),
9+
Bash(opts) => print_script(
10+
pb,
11+
include_bytes!("../sh/shadowenv.bash.in"),
12+
opts.no_hookbook,
13+
),
14+
Zsh(opts) => print_script(
15+
pb,
16+
include_bytes!("../sh/shadowenv.zsh.in"),
17+
opts.no_hookbook,
18+
),
19+
Fish => print_script(
20+
pb,
21+
include_bytes!("../sh/shadowenv.fish.in"),
22+
true, // Fish doesn't use hookbook
23+
),
1224
};
1325
}
1426

15-
fn print_script(selfpath: PathBuf, bytes: &[u8]) -> i32 {
16-
let hookbook = String::from_utf8_lossy(include_bytes!("../sh/hookbook.sh"));
27+
fn print_script(selfpath: PathBuf, bytes: &[u8], no_hookbook: bool) -> i32 {
1728
let script = String::from_utf8_lossy(bytes);
1829
let script = script.replace("@SELF@", selfpath.into_os_string().to_str().unwrap());
19-
let script = script.replace("@HOOKBOOK@", &hookbook);
20-
println!("{}", script);
30+
31+
if no_hookbook {
32+
// If no_hookbook is true, replace @HOOKBOOK@ with an empty string
33+
let script = script.replace("@HOOKBOOK@", "");
34+
println!("{}", script);
35+
} else {
36+
// Otherwise, include the hookbook as before, but pad with newlines
37+
let hookbook = String::from_utf8_lossy(include_bytes!("../sh/hookbook.sh"));
38+
let padded_hookbook = format!("\n{}\n", hookbook);
39+
let script = script.replace("@HOOKBOOK@", &padded_hookbook);
40+
println!("{}", script);
41+
}
2142
0
2243
}

0 commit comments

Comments
 (0)