Tagged: zsh Toggle Comment Threads | Keyboard Shortcuts

  • ThomasPowell 9:01 pm on July 24, 2018 Permalink
    Tags: danger, hooks, precmd, preexec, production, zsh   

    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.


    function safety_preexec() {
    # Store the command that we're running.
    CMD_NAME="$1"
    }
    function safety_precmd() {
    if [[ "$CMD_NAME" =~ 'kubectl config use-context whatever-your-prod-is' ]]; then
    PROD_TIME=$(date +%s)
    CMD_NAME=""
    fi
    if ! [[ -z "$PROD_TIME" ]]; then
    CURRENT_TIME=$(date +%s)
    PROD_ELAPSED_TIME=$(($CURRENT_TIME $PROD_TIME))
    PROD_TIME_THRESHOLD=15
    if [[ $PROD_ELAPSED_TIME -gt $PROD_TIME_THRESHOLD ]]; then
    kubectl config use-context whatever-your-dev-is
    PROD_TIME=""
    fi
    fi
    }
    [[ -z $preexec_functions ]] && preexec_functions=()
    preexec_functions=($preexec_functions safety_preexec)
    [[ -z $precmd_functions ]] && precmd_functions=()
    precmd_functions=($precmd_functions safety_precmd)

    view raw

    .zlogin

    hosted with ❤ by GitHub

     
  • ThomasPowell 12:52 pm on December 30, 2014 Permalink
    Tags: github, zsh   

    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/[email protected]:/https:\/\/github.com\//' | xargs -I {} open {}
    }
    
     
  • ThomasPowell 7:35 pm on May 21, 2014 Permalink
    Tags: zsh   

    .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.

     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel
%d bloggers like this: