Go utilities in ~/bin

One nice side effect of using vcsh was developing more complex scripts to help me do things. I didn’t have to worry a script or tool would get lost when a machine inevitibly died.

However before writing a script, sometimes it’s not a bad idea to check and see if someone else already has. Lately many of those that I’ve found have been in Go.

Originally I did these with update but it made update take a long time to run and sometimes with die if a rarely used Go util was broken.

Based on that I switched to shell stubs to install them if they’re missing and then run them. However most of these tools are in regular development so it’s also nice to keep them current. Again, local stamp files come into play.

The stub itself is simple. Here’s the one for scc which I first read about in an interesting article on Go optimization.

#!/bin/bash

. $HOME/.config/home-bin/.go-run
__get_and_run_go_binary scc github.com/boyter/scc "$@"

The __get_and_run_go_binary function comes from the .go-run file. It’s also rather simple, but putting it all here makes for much smaller stub files and stops me reinventing all the same mistakes I make in such stub files.

One thing to note is that it’s sometimes hard to derive the binary name from the repo name. Go has some rules for stripping those names that I don’t fully know. So rather than try and figure them out - and mess up at some future point when they change - I just put the binary name in explicitly.

#!/bin/bash

set -e

__get_and_run_go_binary() {
  bin=$1; shift
  repo=$1; shift
  stamp_file="$HOME/.config/home-bin/.go-run.stamps/$bin.stamp"

  if [[ ! -f "$GOPATH/bin/$bin" ]]; then
    touch "$stamp_file"
    go get -u $repo
  else
    if [[ ! -f $stamp_file ]]; then
      touch "$stamp_file"
    else
      if find $stamp_file -mtime +21 | grep -q stamp; then
        go get -u $repo
      fi
    fi
  fi


  "$GOPATH/bin/$bin" "$@"
}