Bitbucket Snippets

Here's a small hack for command line access to bitbuckets snippets

#!/bin/bash
# 
# Filename: snippet
# Created: Sun Jul 12 13:26:38 2015 (+1000)
# Version: 0.1
# Author: Ade
# Maintainer: 
# Description: 
#   list, get, put, update and delete snippets to bitbucket

# Change Log:
# 

USER=your_username
PRIVATE="true"

if [ "$1" = "-P" ]
then
  PRIVATE=false
  shift
fi

case "$1" in
  "-L")
    PASS="$(pass show ade/bitbucket | head -1)"
    curl -s --user $USER:$PASS \
         https://api.bitbucket.org/2.0/snippets/$USER | jq '.values[]'
    ;;
  "-l")
    PASS="$(pass show ade/bitbucket | head -1)"
    curl -s --user $USER:$PASS \
         https://api.bitbucket.org/2.0/snippets/$USER | \
      jq -r '.values[]|([.title,.id,(select(.is_private==false)|"public")]|
        join(", ")),.updated_on,(.links.clone[]|select(.name=="ssh")|.href),""'
    ;;
  "-g")
    PASS="$(pass show ade/bitbucket | head -1)"
    id=$(curl -s --user $USER:$PASS \
              https://api.bitbucket.org/2.0/snippets/$USER | \
              jq -r '.values[]|select(.title=="'$2'")|.id')
    curl -s --user $USER:$PASS \
         https://api.bitbucket.org/2.0/snippets/$USER/${id} \
         -H "Accept: multipart/related" | \
      tail +13 | grep -v '^--===============.*$'
    ;;
  "-p")
    PASS="$(pass show ade/bitbucket | head -1)"
    curl -s --user $USER:$PASS \
         -X POST https://api.bitbucket.org/2.0/snippets \
         -F is_private=$PRIVATE -F title="$2" -F file=@$3 | jq -r '.id'
    ;;
  "-u")
    echo "update not done yet"
    ;;
  "-d")
    PASS="$(pass show ade/bitbucket | head -1)"
    id=$(curl -s --user $USER:$PASS \
              https://api.bitbucket.org/2.0/snippets/$USER | \
              jq -r '.values[]|select(.title=="'$2'")|.id')
    curl -s --user $USER:$PASS \
         -X DELETE https://api.bitbucket.org/2.0/snippets/$USER/${id}
    ;;
  *)
    echo "usage: $(basename $0) -[l|L]|-g|-[p|P]|-u|-d [name] [file]"
    echo "  list, get, put, update and delete bitbucket snippets"
    echo "  -L gives full json listing, -l a synopsis listing"
    echo "  -P creates a public snippet, -p creates a private one"
    echo "  note: update isn't done yet, put will create multiple snippets"
    ;;
esac


Date: 2015-07-13 Mon

Emacs 24.5.1 (Org mode 8.2.10)