#confirm #preexec #zsh-hook

bin+lib preexec_confirm

A zsh middleware forcing users to confirm before executing commands with preconfigured texts/regex patterns

3 releases

0.1.2 Apr 10, 2022
0.1.1 Apr 10, 2022
0.1.0 Apr 10, 2022

#4 in #confirm

MIT license

205KB
95 lines

Preexec Confirm

A zsh `middleware` forcing user to confirm before executing commands
with preconfigured texts/regex patterns.

Demo

Users can define a YAML config file, specifying keywords or regex patterns of dangerous commands that they don't want to execute by accident. For example, serverless deploy --stage prod-environment

When the middleware detects a dangerous command, it will force the user to confirm by re-typing a random string, to avoid muscle memory.

Demo

Export env var SKIP_CONFIRM=true if you want to temporarily ignore the middleware.

Skip-confirm-demo

Installation and Usage

Requirements:

  • This project is only applied for Zsh.
  • Cargo is needed

1. Build from source

cargo install preexec_confirm

# ensure the system recognizes preexec_confirm path
which preexec_confirm
## ~/.cargo/bin/preexec_confirm

2. Create a YAML config file with the following format

- contain: string
  description (optional): string
  regex (optional): bool
  • contain: text/regex pattern to search in the command, depending on regex option
  • description: explains why the rule is needed
  • regex: if set to true, contain will be treated as a regex pattern

3. Ensure these lines exist in your .zshrc with correct order

function preexec_confirm_hook() {
    CONFIG_PATH="/home/anhtumai/.config/confirm/config.yml" #change this
    preexec_confirm $CONFIG_PATH $1
}

autoload -Uz add-zsh-hook

add-zsh-hook preexec preexec_confirm_hook

Uninstallation

  • Run cargo uninstall preexec_confirm
  • Remove these configured lines above in .zshrc

Dependencies

~4–15MB
~153K SLoC