-
-
Save SidIcarus/8e7d8d0499b3a411eab5c7cfb4d87a85 to your computer and use it in GitHub Desktop.
Bitbucket Snippet Command Line Tool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# Copyright 2017-2018 Martin Goellnitz | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |
# | |
# defaults | |
PRIVATE=true | |
SCM=git | |
# shellcheck disable=SC2153 | |
BITBUCKETUSER="${BITBUCKET_USER}" | |
if [ ! -z "${BITBUCKETUSER}" ] ; then | |
TEAM=$(echo "${BITBUCKETUSER}" | cut -d ':' -f 1) | |
fi | |
if [ "$(jq 2>&1 | wc -l)" = 0 ] ; then | |
echo "To use this tool, jq must be installed." | |
exit | |
fi | |
if [ "$#" == "0" ]; then | |
echo "Bitbucket User Snippet Command Line Tool 'bucketsnip.sh'" | |
echo "" | |
echo "${0} [-u user] [-t team] [-s snippet title] [-l] [-p] [-h] [filenames...]" | |
echo "" | |
echo "If 'filename' points to an existing file, it is uploaded to Bitbucket with an optional title." | |
echo "If 'filename' describes a filename present in the user's snippets, it is downloaded." | |
echo "If 'filename' is not present, the contents of the current directory are used as file-set." | |
echo "" | |
echo " -l list snippets" | |
echo "" | |
echo " -u user name - optionally with app password appended after ':' - default \$BITBUCKET_USER" | |
echo "" | |
echo " -t team to use instead of users snippets" | |
echo "" | |
echo " -s snippet title - defaults to filename for single file snippets or to "$(basename `pwd`) | |
echo "" | |
echo " -p make snippet public" | |
echo "" | |
echo " -h create snippet with mercurial (only available on creation)" | |
echo "" | |
exit 1 | |
fi | |
PSTART=$(echo "${1}" | sed -e 's/^\(.\).*/\1/g') | |
while [ "${PSTART}" = "-" ] ; do | |
if [ "$1" = "-u" ] ; then | |
shift | |
BITBUCKETUSER="$1" | |
TEAM=$(echo "${BITBUCKETUSER}" | cut -d ':' -f 1) | |
fi | |
if [ "$1" = "-t" ] ; then | |
shift | |
TEAM="$1" | |
fi | |
if [ "$1" = "-s" ] ; then | |
shift | |
SNIPPET="$1" | |
fi | |
if [ "$1" = "-p" ] ; then | |
PRIVATE=false | |
fi | |
if [ "$1" = "-h" ] ; then | |
SCM=hg | |
fi | |
if [ "$1" = "-l" ] ; then | |
LISTING=true | |
fi | |
shift | |
PSTART=$(echo "${1}" | sed -e 's/^\(.\).*/\1/g') | |
done | |
if [ -z "${BITBUCKETUSER}" ] ; then | |
echo "Cannot work without the context of a bitbucket user name. Sorry..." | |
exit 1 | |
fi | |
AUTH=-u"${BITBUCKETUSER}" | |
API="https://api.bitbucket.org/2.0/snippets/${TEAM}" | |
if [ ! -z "${LISTING}" ] ; then | |
echo "Listing snippets of $TEAM" | |
SNIPPETS=$(curl -k --basic \ | |
$AUTH \ | |
"${API}" 2> /dev/null \ | |
| jq -r '.values[] | .id' | |
) | |
for SNID in $SNIPPETS ; do | |
# echo $SNID | |
LISTING=$(curl -k --basic \ | |
$AUTH \ | |
"${API}/${SNID}" 2> /dev/null \ | |
| jq -r '.title, .updated_on, .is_private, (.files | keys)' | |
) | |
echo "${LISTING}" | |
done | |
else | |
FILES=$* | |
if [ -z "$FILES" ] ; then | |
FILES=$(ls) | |
fi | |
if [ -z "$SNIPPET" ] ; then | |
if [ "$#" == "1" ] ; then | |
SNIPPET=$(basename $1) | |
echo "Checking for present single file snippet" | |
SNIPPETS=$(curl -k --basic \ | |
$AUTH \ | |
"${API}" 2> /dev/null \ | |
| jq -r '.values[] | .id' \ | |
) | |
for SNID in $SNIPPETS ; do | |
JSON=$(curl -k --basic \ | |
$AUTH \ | |
"${API}/${SNID}" 2> /dev/null) | |
if [ $(echo $JSON | jq ".files[].links.self.href" | wc -l) == 1 ] ; then | |
LISTING=$(echo $JSON \ | |
| jq -r ".files[\"$SNIPPET\"].links.self.href" | |
) | |
if [ ! -z "$LISTING" ] ; then | |
if [ "$LISTING" != "null" ] ; then | |
SNIPPET=$(curl -k --basic \ | |
$AUTH \ | |
"${API}" 2> /dev/null \ | |
| jq -r ".values[] | select(.id==\"${SNID}\") | .title" | |
) | |
echo "Discovered $SNIPPET for snippet containing solely $1" | |
fi | |
fi | |
fi | |
done | |
else | |
SNIPPET=$(basename `pwd`) | |
fi | |
fi | |
SNID=$(curl -k --basic \ | |
$AUTH \ | |
"${API}" 2> /dev/null \ | |
| jq -r ".values[] | select(.title==\"${SNIPPET}\") | .id" | |
) | |
if [ -z "$SNID" ] ; then | |
echo -n "Creating" | |
test "true" == "$PRIVATE" && echo -n " private" | |
echo " snippet $SNIPPET in $TEAM" | |
DATA="" | |
for FILEPATH in $FILES ; do | |
if [ -f $FILEPATH ] ; then | |
FILE=$(basename $FILEPATH) | |
# echo $FILE $FILEPATH | |
DATA="$DATA -F file=@$FILEPATH" | |
fi | |
done | |
curl -k --basic \ | |
$AUTH \ | |
-X POST \ | |
-F title="$SNIPPET" \ | |
-F is_private="$PRIVATE" \ | |
-F scm=$SCM \ | |
$DATA \ | |
"${API}" 2> /dev/null \ | |
| jq .title | |
else | |
echo "Updating snippet $SNIPPET in $TEAM" | |
DATA="" | |
DOWNLOADS="" | |
for FILEPATH in $FILES ; do | |
if [ -f $FILEPATH ] ; then | |
DATA="$DATA -F file=@$FILEPATH" | |
else | |
DOWNLOADS="$DOWNLOADS $FILEPATH" | |
fi | |
done | |
if [ ! -z "$DATA" ] ; then | |
curl -k --basic \ | |
$AUTH \ | |
-X PUT \ | |
-F title="$SNIPPET" \ | |
-F is_private="$PRIVATE" \ | |
-F scm=$SCM \ | |
$DATA \ | |
"${API}/${SNID}" 2> /dev/null \ | |
| jq .title | |
else | |
echo "Nothing to upload" | |
fi | |
echo "Downloading missing files... $DOWNLOADS" | |
JSON=$(curl -k --basic \ | |
$AUTH \ | |
"${API}/${SNID}" 2> /dev/null \ | |
| jq '.') | |
LISTING=$(echo $JSON | jq '(.files | keys)') | |
LISTING=$(echo $LISTING \ | |
| sed -e 's/\",.\"/ /g' \ | |
-e 's/.\ \"//g' \ | |
-e 's/\"\ .//g' | |
) | |
# echo $LISTING | |
if [ $# == 0 ] ; then | |
echo "No additional arguments" | |
for ELEMENT in $LISTING ; do | |
if [ ! -f "$ELEMENT" ] ; then | |
echo "Adding ${ELEMENT} to download list" | |
DOWNLOADS+=" ${ELEMENT}" | |
fi | |
done | |
fi | |
for ELEMENT in $LISTING ; do | |
for FILE in $DOWNLOADS ; do | |
if [ "$ELEMENT" == "$FILE" ] ; then | |
echo "Downloading $FILE" | |
dlURL=$(echo $JSON \ | |
| jq -r '. | .files["'$FILE'"].links.self.href' | |
) | |
curl -k --basic \ | |
$AUTH \ | |
"${dlURL}" 2> /dev/null > $FILE | |
fi | |
done | |
done | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment