1#!/bin/sh
2
3##
4# Script to deploy a docker image. Must return exit code 0
5#
6do_exit() {
7    message="$1"
8    exit_code="$2"
9
10    echo "$message"
11    exit $exit_code
12}
13
14
15##
16# Get file, get's a file, validates the SHA
17# @param filename
18# @param expected sha value
19# @returns 0 if successful, -1 of checksum validation failed.
20#
21get_file () {
22    [ -n "$1" ] && url=$1 || do_exit "url required" 1
23    [ -n "$2" ] && dest=$2 || do_exit "destination required" 2
24    sha=$3
25    file=$(basename $dest)
26
27    curl -fL "${url}" -o "$dest"
28    if [ -n "$sha" ]; then
29        echo "$sha  $dest" | sha256sum || do_exit "Checksum validation failed for $file. Exiting" 1
30    fi
31}
32
33untar_file () {
34    [ -n "$1" ] && src=$1 || do_exit "src required" 1
35    [ -n "$2" ] && dest=$2 || dest="/usr/local"
36
37    tar -C "$dest" -xf "$src" && /bin/rm -rf "$src"
38}
39
40##
41# WIP: Just started this and not finished.
42# The intent it to download a release from a git repo,
43# compile, and install
44get_latest_release () {
45	tarsrc=$(curl -sL "https://api.github.com/repos/$1/$2/releases/latest" | jq ".tarball_url" | tr -d '"')
46	curl -fL -o /tmp/autoretrieved.tar.gz "$tarsrc"
47	origdir=$PWD
48	reponame=$(tar zxvf autoretrieved.tar.gz | tail -1 | awk -F / '{print $1}')
49	cd "/tmp/$reponame"
50	#perform compile
51	cd $origdir
52}
53