Skip to content

Instantly share code, notes, and snippets.

@k2wanko
Created December 4, 2015 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k2wanko/a4ad5c3b903f68f194b1 to your computer and use it in GitHub Desktop.
Save k2wanko/a4ad5c3b903f68f194b1 to your computer and use it in GitHub Desktop.
CircleCI functions
#!/bin/sh
CIRCLE_TOKEN="..."
CIRCLE_HOST="https://circleci.com/api/v1"
_circle_token="&circle-token=$CIRCLE_TOKEN"
open=$(which open)
function circle_projects() {
url="$CIRCLE_HOST/projects?${_circle_token}"
curl -s -H "Accept: application/json" $url | jq '.[] | "¥(.username)/¥(.reponame)"' | perl -nle '/^"(.+)"$/ and print $1'
}
function circle_open_project() {
path=$(circle_projects | peco | awk '{print $1}')
url="https://circleci.com/gh/$path"
echo "open: $url"
$open $url
}
function circle_env() {
project=$1
name=$2
value=$3
if [ ! -n "$project" ]; then
echo "not project" > /dev/stderr
return 1
fi
url="$CIRCLE_HOST/project/$project/envvar?${_circle_token}"
if [ -n "$value" ]; then
body="{¥"name¥":¥"$name¥",¥"value¥":¥"$value¥"}"
curl -s -H "Accept: application/json" -H "Content-Type: application/json" -d $body $url > /dev/null
echo ok
return 0
fi
if [ -n "$name" ]; then
method="GET"
if [ "${name:0:1}" = ":" ]; then
method="DELETE"
name=${name:1:${#name}}
fi
url="$CIRCLE_HOST/project/$project/envvar/$name?${_circle_token}"
res=$(curl -X $method -s -H "Accept: application/json" $url)
if [ $method = "DELETE" ]; then
echo ok
else
echo $res | jq .value | perl -nle '/^"(.+)"$/ and print $1'
fi
return 0
fi
curl -s -H "Accept: application/json" $url | jq '.[] | "¥(.name) ¥(.value)"' | perl -nle '/^"(.+)"$/ and print $1'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment