Making Your zsh Return You to a Non-Production Setting

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.

Open Your Github Repo in a Web Browser from the Shell

This assumes that your first remote is a github remote. Put the following in the appropriate bash or zsh initialization script and that you're on a Mac. You might need to change "open" to whatever will launch a URL in a web browser on a different *nix system.

function gitweb() {
  git remote -v | head -n 1 | awk '{print $2}' | sed 's/.git$//' | sed 's/git@github.com:/https:\/\/github.com\//' | xargs -I {} open {}
}

.zlogin (or .zshrc) parse error near `\n'

This code was at the end of my .zlogin on my mac and was causing .zlogin:152: parse error near `\n'. The problem is rather bone-headed, but I went through trying to :set ff=unix, :set ff=mac in vim to try and remedy bad line endings.

function notify_preexec() {
  # Note the date when the command started, in unix time.
  CMD_START_DATE=$(date +%s)
  # Store the command that we're running.
  CMD_NAME="$1"
}
 
function notify_precmd() {
  # Proceed only if we've run a command in the current shell.
  # Proceed only if we've run a command in the current shell.
 
function notify_precmd() {
  # Proceed only if we've run a command in the current shell.
  if ! [[ -z $CMD_START_DATE ]]; then
    # Note current date in unix time
    CMD_END_DATE=$(date +%s)
    # Store the difference between the last command start date vs. current date.
    CMD_ELAPSED_TIME=$(($CMD_END_DATE - $CMD_START_DATE))
    # Store an arbitrary threshold, in seconds.
    CMD_NOTIFY_THRESHOLD=240
 
    if [[ $CMD_ELAPSED_TIME -gt $CMD_NOTIFY_THRESHOLD ]]; then
      # Beep or visual bell if the elapsed time (in seconds) is greater than threshold
      # Send a notification
      if [[ "$CMD_NAME" =~ 'rake' ]]; then
        say "$CMD_NAME FINISHED IN $CMD_ELAPSED_TIME SECONDS"
      fi
      unset CMD_NAME
      unset CMD_START_DATE
    fi
  fi
}

See it yet?

function notify_precmd() {
  # Proceed only if we've run a command in the current shell.
  # Proceed only if we've run a command in the current shell.
 
function notify_precmd() {

I somehow opened up the notify_precmd() function twice. I was mostly focusing on a missing fi and mismatched parens/braces.

zsh alias to make and apply patch with git diff

I keep having to look up [patching with git diff](http://tamsler.blogspot.com/2009/02/patching-with-git-diff.html) to remember the syntax whenever I want to create and apply a patch file.

Solution?

Create an alias for it. It only uses one file, /tmp/patchfile, so don't try to manage multiple patches with this (maybe I'll make more intelligent/sophisticated someday.

In my .zlogin, I now have:

alias makepatch='git diff --no-prefix > /tmp/patchfile'
alias applypatch='patch -p0 < /tmp/patchfile'

To create a patch, makepatch

To apply a patch, applypatch

Call up JIRA tasks from zsh using .oh-my-zsh

This was called to my attention via

[Current instructions are in the plugin source](https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/jira/jira.plugin.zsh)

The setup:
In .zshrc, add jira to plugins and JIRA_RAPID_BOARD=true if you use Rapid Board.

#
#
plugins=(git jira)
JIRA_RAPID_BOARD=true
#
#

Create a file .jira-url in your home directory or at the base of your project.

https://my.jira.url/

To constrain to a subproject for your project directory's .jira-url: (Your mileage may vary on how well this improves your experience)

https://my.jira.url/browse/SUBPROJ/

To create a new issue:

jira

To open an existing issue:

jira PROJ-123