I have my prompt in zsh setup to display what Kubernetes context I am pointing to, so that I will notice if I’m somehow pointed at production environments, but I’m still wary of being asked to check things in production and then forgetting to point back to a “safe” environment before doing something else.
So I set up a preexec and precmd hook to store state for the session that I’m in. In the preexec, I save off whatever command was run.
In the precmd, I check to see that command matches “switching over to production”, mark the time, and clear the command out (otherwise, pressing <return> on the shell will keep the same command).
If I have marked the time I switched to production (PROD_TIME is not an empty string), then I check the current time and if it’s past a threshold (15 seconds in the current case), then I run the command to switch back.
The last few lines clear the prexec and precmd function hooks and set them to the “safety” functions.