From 4fc835ce4395cdaf97c55e4738792fc991c0bd8b Mon Sep 17 00:00:00 2001 From: sabith Date: Sun, 8 Jul 2018 09:10:35 -0700 Subject: [PATCH 1/3] Using travis for CI CD Signed-off-by: sabith --- .gitignore | 2 ++ .travis.yml | 15 +++++++++++ Gopkg.lock | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++ Gopkg.toml | 50 ++++++++++++++++++++++++++++++++++ Makefile | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 217 insertions(+) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 Gopkg.lock create mode 100644 Gopkg.toml create mode 100644 Makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..fd8ceee --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vendor/ +build/ diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..c029d14 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,15 @@ +language: go + +go: + - master + +script: make + +deploy: + provider: releases + api_key: $GITHUB_TOKEN + file_glob: true + file: "build/*" + skip_cleanup: true + on: + tags: true diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..d374445 --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,73 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + name = "github.com/Sirupsen/logrus" + packages = ["."] + revision = "c155da19408a8799da419ed3eeb0cb5db0ad5dbc" + version = "v1.0.5" + +[[projects]] + name = "github.com/bitly/go-simplejson" + packages = ["."] + revision = "aabad6e819789e569bd6aabf444c935aa9ba1e44" + version = "v0.5.0" + +[[projects]] + name = "github.com/eclipse/paho.mqtt.golang" + packages = [ + ".", + "packets" + ] + revision = "36d01c2b4cbeb3d2a12063e4880ce30800af9560" + version = "v1.1.1" + +[[projects]] + name = "github.com/mattn/go-colorable" + packages = ["."] + revision = "167de6bfdfba052fa6b2d3664c8f5272e23c9072" + version = "v0.0.9" + +[[projects]] + name = "github.com/mattn/go-isatty" + packages = ["."] + revision = "0360b2af4f38e8d38c7fce2a9f4e702702d73a39" + version = "v0.0.3" + +[[projects]] + name = "github.com/urfave/cli" + packages = ["."] + revision = "cfb38830724cc34fedffe9a2a29fb54fa9169cd1" + version = "v1.20.0" + +[[projects]] + branch = "master" + name = "golang.org/x/crypto" + packages = ["ssh/terminal"] + revision = "a49355c7e3f8fe157a85be2f77e6e269a0f89602" + +[[projects]] + branch = "master" + name = "golang.org/x/net" + packages = [ + "internal/socks", + "proxy", + "websocket" + ] + revision = "32a936f46389aa10549d60bd7833e54b01685d09" + +[[projects]] + branch = "master" + name = "golang.org/x/sys" + packages = [ + "unix", + "windows" + ] + revision = "3c6ecd8f22c6f40fbeec94c000a069d7d87c7624" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "93f25dec6686dd7cc9f3c37e4ba3b599e150e4d41817bc4f6d8a5ef5a450001e" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..6340282 --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,50 @@ +# Gopkg.toml example +# +# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md +# for detailed Gopkg.toml documentation. +# +# required = ["github.com/user/thing/cmd/thing"] +# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] +# +# [[constraint]] +# name = "github.com/user/project" +# version = "1.0.0" +# +# [[constraint]] +# name = "github.com/user/project2" +# branch = "dev" +# source = "github.com/myfork/project2" +# +# [[override]] +# name = "github.com/x/y" +# version = "2.4.0" +# +# [prune] +# non-go = false +# go-tests = true +# unused-packages = true + + +[[constraint]] + name = "github.com/Sirupsen/logrus" + version = "1.0.5" + +[[constraint]] + name = "github.com/bitly/go-simplejson" + version = "0.5.0" + +[[constraint]] + name = "github.com/eclipse/paho.mqtt.golang" + version = "1.1.1" + +[[constraint]] + name = "github.com/mattn/go-colorable" + version = "0.0.9" + +[[constraint]] + name = "github.com/urfave/cli" + version = "1.20.0" + +[prune] + go-tests = true + unused-packages = true diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e7044b9 --- /dev/null +++ b/Makefile @@ -0,0 +1,77 @@ +GREEN := $(shell tput -Txterm setaf 2) +YELLOW := $(shell tput -Txterm setaf 3) +WHITE := $(shell tput -Txterm setaf 7) +RESET := $(shell tput -Txterm sgr0) + + +.PHONY: all + +GO_PROJECT = github.com/shirou/mqttcli +BUILD_DEST = build +COMMIT_HASH=`git rev-parse --short HEAD` +GIT_COMMIT = $(shell git rev-parse HEAD) +GIT_SHA = $(shell git rev-parse --short HEAD) +GIT_DIRTY = $(shell test -n "`git status --porcelain`" && echo "dirty" || echo "clean") + +LDFLAGS += -w -s -extldflags -static + +ifndef VERSION + VERSION = DEV +endif + +GOFLAGS := -ldflags "$(LDFLAGS)" + +## Download dependencies and the run unit test and build the binary +all: install clean build + +## Clean the dist directory +clean: + @rm -rf $(BUILD_DEST) + +## download dependencies to run this project +install: + @which gox > /dev/null || go get github.com/mitchellh/gox + @which dep > /dev/null || go get github.com/golang/dep/cmd/dep + dep ensure -vendor-only + +## Run for local development +start: + DATA_DIRECTORY="$$PWD/data" \ + go run *.go + +## Build the linux binary +build: + @rm -rf $(BUILD_DEST) + @mkdir -p $(BUILD_DEST) > /dev/null + @CGO_ENABLED=0 \ + gox \ + -output "$(BUILD_DEST)/{{.Dir}}_{{.OS}}_{{.Arch}}" \ + $(GOFLAGS) \ + . + +## Prints the version info about the project +info: + @echo "Version: ${VERSION}" + @echo "Git Commit: ${GIT_COMMIT}" + @echo "Git Tree State: ${GIT_DIRTY}" + +## Print the dependency graph and open in MAC +dependencygraph: + dep status -dot | dot -T png | open -f -a /Applications/Preview.app + +## Prints this help command +help: + @echo '' + @echo 'Usage:' + @echo ' ${YELLOW}make${RESET} ${GREEN}${RESET}' + @echo '' + @echo 'Targets:' + @awk '/^[a-zA-Z\-\_0-9]+:/ { \ + helpMessage = match(lastLine, /^## (.*)/); \ + if (helpMessage) { \ + helpCommand = substr($$1, 0, index($$1, ":")-1); \ + helpMessage = substr(lastLine, RSTART + 3, RLENGTH); \ + printf " ${YELLOW}%-$(TARGET_MAX_CHAR_NUM)s${RESET}: ${GREEN}%s${RESET}\n", helpCommand, helpMessage; \ + } \ + } \ + { lastLine = $$0 }' $(MAKEFILE_LIST) \ No newline at end of file From c7210e3587ba7f9b466f2fe6606db4b0a954a887 Mon Sep 17 00:00:00 2001 From: sabith Date: Sun, 8 Jul 2018 09:27:44 -0700 Subject: [PATCH 2/3] Try using a install script like dep Signed-off-by: sabith --- Makefile | 5 +- README.rst | 10 +++- config_test.go | 2 +- install.sh | 127 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 140 insertions(+), 4 deletions(-) create mode 100755 install.sh 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 From 67a1558ad535fd6b05ac2daa9fe803ceda4a3b0b Mon Sep 17 00:00:00 2001 From: Sabith K Soopy Date: Tue, 17 Jul 2018 22:58:14 -0700 Subject: [PATCH 3/3] Using releases from @shirou repo --- README.rst | 4 ++-- install.sh | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index c719e3e..36d7c39 100644 --- a/README.rst +++ b/README.rst @@ -10,12 +10,12 @@ Install Download from `here`_. Please choose your artitecture. (and chmod ugo+x if needed) -.. _here: https://github.com/sks/mqttcli/releases/latest +.. _here: https://github.com/shirou/mqttcli/releases/latest :: - curl https://raw.githubusercontent.com/sks/mqttcli/master/install.sh | sh + curl https://raw.githubusercontent.com/shirou/mqttcli/master/install.sh | sh Or if you have golang environment, type this to build on your host. diff --git a/install.sh b/install.sh index 91f9032..65c4f7f 100755 --- a/install.sh +++ b/install.sh @@ -2,7 +2,7 @@ # Based on https://raw.githubusercontent.com/golang/dep/master/install.sh -RELEASES_URL="https://github.com/sks/mqttcli/releases" +RELEASES_URL="https://github.com/shirou/mqttcli/releases" INSTALL_NAME="mqttcli" if [ -z ${INSTALL_DIRECTORY} ] then