diff --git a/Makefile b/Makefile index e7044b9..138da2b 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ endif GOFLAGS := -ldflags "$(LDFLAGS)" ## Download dependencies and the run unit test and build the binary -all: install clean build +all: install clean test build ## Clean the dist directory clean: @@ -34,6 +34,9 @@ install: @which dep > /dev/null || go get github.com/golang/dep/cmd/dep dep ensure -vendor-only +## Run unit test +test: + go test . ## Run for local development start: DATA_DIRECTORY="$$PWD/data" \ diff --git a/README.rst b/README.rst index ab13525..c719e3e 100644 --- a/README.rst +++ b/README.rst @@ -8,9 +8,15 @@ pubsub command which is suite for the shell script pipelining. Install ============== -Download from here. Please choose your artitecture. (and chmod ugo+x if needed) +Download from `here`_. Please choose your artitecture. (and chmod ugo+x if needed) + +.. _here: https://github.com/sks/mqttcli/releases/latest + + +:: + + curl https://raw.githubusercontent.com/sks/mqttcli/master/install.sh | sh -https://drone.io/github.com/shirou/mqttcli/files Or if you have golang environment, type this to build on your host. diff --git a/config_test.go b/config_test.go index d1103ad..5dc976b 100644 --- a/config_test.go +++ b/config_test.go @@ -42,6 +42,6 @@ func Test_ConfigCert(t *testing.T) { t.Error(err) } if c.CaCert != "ca" || c.ClientCert != "client" || c.PrivateKey != "key" { - t.Error("parse failed, %#v", c) + t.Errorf("parse failed, %+v", c) } } diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..91f9032 --- /dev/null +++ b/install.sh @@ -0,0 +1,127 @@ +#!/bin/sh -e + +# Based on https://raw.githubusercontent.com/golang/dep/master/install.sh + +RELEASES_URL="https://github.com/sks/mqttcli/releases" +INSTALL_NAME="mqttcli" +if [ -z ${INSTALL_DIRECTORY} ] +then + INSTALL_DIRECTORY="${HOME}/bin" +fi + +downloadJSON() { + url="$2" + + echo "Fetching $url.." + if test -x "$(command -v curl)"; then + response=$(curl -s -L -w 'HTTPSTATUS:%{http_code}' -H 'Accept: application/json' "$url") + body=$(echo "$response" | sed -e 's/HTTPSTATUS\:.*//g') + code=$(echo "$response" | tr -d '\n' | sed -e 's/.*HTTPSTATUS://') + elif test -x "$(command -v wget)"; then + temp=$(mktemp) + body=$(wget -q --header='Accept: application/json' -O - --server-response "$url" 2> "$temp") + code=$(awk '/^ HTTP/{print $2}' < "$temp" | tail -1) + rm "$temp" + else + echo "Neither curl nor wget was available to perform http requests." + exit 1 + fi + if [ "$code" != 200 ]; then + echo "Request failed with code $code" + exit 1 + fi + + eval "$1='$body'" +} + +downloadFile() { + url="$1" + destination="$2" + + echo "Fetching $url.." + if test -x "$(command -v curl)"; then + code=$(curl -s -w '%{http_code}' -L "$url" -o "$destination") + elif test -x "$(command -v wget)"; then + code=$(wget -q -O "$destination" --server-response "$url" 2>&1 | awk '/^ HTTP/{print $2}' | tail -1) + else + echo "Neither curl nor wget was available to perform http requests." + exit 1 + fi + + if [ "$code" != 200 ]; then + echo "Request failed with code $code" + exit 1 + fi +} + +initArch() { + ARCH=$(uname -m) + if [ -n "$DEP_ARCH" ]; then + echo "Using DEP_ARCH" + ARCH="$DEP_ARCH" + fi + case $ARCH in + amd64) ARCH="amd64";; + x86_64) ARCH="amd64";; + i386) ARCH="386";; + *) echo "Architecture ${ARCH} is not supported by this installation script"; exit 1;; + esac + echo "ARCH = $ARCH" +} + +initOS() { + OS=$(uname | tr '[:upper:]' '[:lower:]') + if [ -n "$DEP_OS" ]; then + echo "Using DEP_OS" + OS="$DEP_OS" + fi + case "$OS" in + darwin) OS='darwin';; + linux) OS='linux';; + freebsd) OS='freebsd';; + mingw*) OS='windows';; + msys*) OS='windows';; + *) echo "OS ${OS} is not supported by this installation script"; exit 1;; + esac + echo "OS = $OS" +} + +# identify platform based on uname output +initArch +initOS + +# determine install directory if required +echo "Will install into $INSTALL_DIRECTORY" + +# assemble expected release artifact name +BINARY="mqttcli_${OS}_${ARCH}" + +# add .exe if on windows +if [ "$OS" = "windows" ]; then + BINARY="$BINARY.exe" +fi + +# if DEP_RELEASE_TAG was not provided, assume latest +if [ -z "$DEP_RELEASE_TAG" ]; then + downloadJSON LATEST_RELEASE "$RELEASES_URL/latest" + DEP_RELEASE_TAG=$(echo "${LATEST_RELEASE}" | tr -s '\n' ' ' | sed 's/.*"tag_name":"//' | sed 's/".*//' ) +fi +echo "Release Tag = $DEP_RELEASE_TAG" + +# fetch the real release data to make sure it exists before we attempt a download +downloadJSON RELEASE_DATA "$RELEASES_URL/tag/$DEP_RELEASE_TAG" + +BINARY_URL="$RELEASES_URL/download/$DEP_RELEASE_TAG/$BINARY" +DOWNLOAD_FILE=$(mktemp) + +downloadFile "$BINARY_URL" "$DOWNLOAD_FILE" + +echo "Setting executable permissions." +chmod +x "$DOWNLOAD_FILE" + +if [ "$OS" = "windows" ]; then + INSTALL_NAME="$INSTALL_NAME.exe" +fi + +echo "Moving executable to $INSTALL_DIRECTORY/$INSTALL_NAME" +mv "$DOWNLOAD_FILE" "$INSTALL_DIRECTORY/$INSTALL_NAME" \ No newline at end of file