Skip to content

Add flag to skip running the init file #51

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

Merged
merged 1 commit into from
Mar 30, 2025
Merged
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
49 changes: 46 additions & 3 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ pub enum Action {
Shell,
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq)]
#[allow(clippy::struct_excessive_bools)]
pub struct Search {
pub dry_run: bool,
pub load_file: bool,
pub insecure: bool,
pub use_color: bool,
pub empty_exit_code: i32,
Expand All @@ -28,6 +30,21 @@ pub struct Search {
pub query: Option<String>,
}

impl Default for Search {
fn default() -> Self {
Self {
dry_run: false,
load_file: true,
insecure: false,
use_color: false,
empty_exit_code: 0,
projects_config: None,
scope: Scope::default(),
query: None,
}
}
}

impl Search {
#[must_use]
pub fn into_query(self) -> Option<String> {
Expand Down Expand Up @@ -160,7 +177,7 @@ impl Action {
]
}

fn search_args() -> [Arg; 7] {
fn search_args() -> [Arg; 8] {
[
Arg::new("dry_run")
.long("dry-run")
Expand All @@ -169,6 +186,19 @@ impl Action {
.value_parser(value_parser!(bool))
.action(ArgAction::SetTrue)
.required(false),
Arg::new("load_file")
.long("skip-init")
.short('S')
.help("Skip running any initialization file")
.long_help(concat!(
"This is usefile when there isan issue with the ",
"configuration file, and you still want to attach ",
"to a session, pretending the innitialization file ",
"does not exist.",
))
.value_parser(value_parser!(bool))
.action(ArgAction::SetFalse)
.required(false),
Arg::new("insecure")
.long("insecure")
.short('i')
Expand Down Expand Up @@ -338,6 +368,7 @@ impl Action {
return Self::Shell;
}

let load_file = matches.remove_one::<bool>("load_file").expect("flag");
let insecure = matches.remove_one::<bool>("insecure").expect("flag");
let dry_run = matches.remove_one::<bool>("dry_run").expect("flag");
let empty_exit_code = matches
Expand Down Expand Up @@ -380,6 +411,7 @@ impl Action {

Self::Search(Search {
dry_run,
load_file,
insecure,
use_color,
empty_exit_code,
Expand Down Expand Up @@ -844,6 +876,16 @@ mod tests {
assert_search!(["--insecure"], Search { insecure: true });
}

#[test]
fn skip_init_flag_for_search() {
assert_search!(["--skip-init"], Search { load_file: false });
}

#[test]
fn skip_init_short_flag_for_search() {
assert_search!(["-S"], Search { load_file: false });
}

#[test]
fn tmux_only_scope_long() {
assert_search!(
Expand Down Expand Up @@ -921,9 +963,10 @@ mod tests {
#[test]
fn all_search_flags() {
assert_search!(
["-n", "--insecure", "--tmux", "foo", "bar"],
["-n", "-S", "--insecure", "--tmux", "foo", "bar"],
Search {
dry_run: true,
load_file: false,
insecure: true,
scope: Scope::TmuxOnly,
query: Some("foo bar".into())
Expand Down
11 changes: 7 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ fn run_shell() -> Result<()> {
fn run_search(
Search {
dry_run,
load_file,
insecure,
use_color,
empty_exit_code,
Expand Down Expand Up @@ -99,8 +100,8 @@ fn run_search(

let command = prompt_user(selection).and_then(|e| {
e.map(|e| {
debug!(eentry =? e, "selected");
apply_entry(e, !insecure)
debug!(entry =? e, "selected");
apply_entry(e, load_file, !insecure)
})
.transpose()
})?;
Expand Down Expand Up @@ -142,10 +143,12 @@ fn find_tmux_sessions(tx: SyncSender<Entry>) -> Thread<()> {
Thread::new("tmux ls", thread)
}

fn apply_entry(entry: Entry, secure: bool) -> Result<Command> {
fn apply_entry(entry: Entry, load_file: bool, secure: bool) -> Result<Command> {
let action = match entry {
Entry::Project(project) => {
let on_init = init::find_action(&project.root, secure)
let on_init = load_file
.then(|| init::find_action(&project.root, secure))
.flatten()
.transpose()?
.unwrap_or_default();
Action::Create {
Expand Down