Skip to content

Commit

Permalink
Merge pull request #6 from gliderlabs/master
Browse files Browse the repository at this point in the history
v0.0.5
  • Loading branch information
mattatcha committed Feb 11, 2015
2 parents 8f65d4e + af4e568 commit 30c2943
Show file tree
Hide file tree
Showing 15 changed files with 1,226 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
build
example/.gun
release
bindata.go
16 changes: 15 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ All notable changes to this project will be documented in this file.

### Changed

## [0.0.5] - 2015-02-11
### Fixed
- Avoid use of system `md5` by using baked-in `checksum`
- Ensure `chmod +x` on downloaded binaries in deps.bash
- `gun init` no longer breaks `.gitignore`

### Added
- `gun update` which performs a self-update to latest release
- `gun version` also displays latest released version if different

### Changed
- `gun init` makes `.gun` with its own `.gitignore`

## [0.0.4] - 2015-02-09
### Fixed
- Use bash from PATH
Expand All @@ -35,7 +48,8 @@ All notable changes to this project will be documented in this file.
### Fixed
- Fixed profiles not loading

[unreleased]: https://github.com/gliderlabs/glidergun/compare/v0.0.4...HEAD
[unreleased]: https://github.com/gliderlabs/glidergun/compare/v0.0.5...HEAD
[0.0.5]: https://github.com/gliderlabs/glidergun/compare/v0.0.4...v0.0.5
[0.0.4]: https://github.com/gliderlabs/glidergun/compare/v0.0.3...v0.0.4
[0.0.3]: https://github.com/gliderlabs/glidergun/compare/v0.0.2...v0.0.3
[0.0.2]: https://github.com/gliderlabs/glidergun/compare/v0.0.1...v0.0.2
7 changes: 4 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
NAME=glidergun
BINARYNAME=gun
ARCH=$(shell uname -m)
VERSION=0.0.4
VERSION=0.0.5

build:
go-bindata include
Expand All @@ -20,6 +20,7 @@ release: build
rm -rf release && mkdir release
tar -zcf release/$(NAME)_$(VERSION)_Linux_$(ARCH).tgz -C build/Linux $(BINARYNAME)
tar -zcf release/$(NAME)_$(VERSION)_Darwin_$(ARCH).tgz -C build/Darwin $(BINARYNAME)
gh-release create gliderlabs/$(NAME) $(VERSION) $(shell git rev-parse --abbrev-ref HEAD)
gh-release checksums sha256
gh-release create gliderlabs/$(NAME) $(VERSION) $(shell git rev-parse --abbrev-ref HEAD) $(VERSION)

.PHONY: build
.PHONY: build release
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@

## Install

$ curl dl.gliderlabs.com/glidergun/latest/$(uname -sm|tr \ _).tgz \
$ curl https://dl.gliderlabs.com/glidergun/latest/$(uname -sm|tr \ _).tgz \
| tar -zxC /usr/local/bin

## Upgrading

$ gun update
97 changes: 96 additions & 1 deletion glidergun.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,111 @@
package main

import (
"archive/tar"
"bytes"
"compress/gzip"
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"encoding/hex"
"fmt"
"hash"
"io"
"io/ioutil"
"net/http"
"os"

"github.com/inconshreveable/go-update"
"github.com/progrium/go-basher"
)

const (
LatestDownloadUrl = "https://dl.gliderlabs.com/glidergun/latest/%s.tgz"
)

var Version string

func fatal(msg string) {
println("!!", msg)
os.Exit(2)
}

func Selfupdate(args []string) {
up := update.New()
err := up.CanUpdate()
if err != nil {
fatal("Can't update because: '" + err.Error() + "'. Try as root?")
}
checksumExpected, err := hex.DecodeString(args[1])
if err != nil {
fatal(err.Error())
}
url := fmt.Sprintf(LatestDownloadUrl, args[0])
fmt.Printf("Downloading %v ...\n", url)
resp, err := http.Get(url)
if err != nil {
fatal(err.Error())
}
defer resp.Body.Close()
buf := new(bytes.Buffer)
data, err := ioutil.ReadAll(io.TeeReader(resp.Body, buf))
if err != nil {
fatal(err.Error())
}
checksum := sha256.New().Sum(data)
if bytes.Equal(checksum, checksumExpected) {
fatal("Checksum failed. Got: " + fmt.Sprintf("%x", checksum))
}
z, err := gzip.NewReader(buf)
if err != nil {
fatal(err.Error())
}
defer z.Close()
t := tar.NewReader(z)
hdr, err := t.Next()
if err != nil {
fatal(err.Error())
}
if hdr.Name != "gun" {
fatal("glidergun binary not found in downloaded tarball")
}
err, errRecover := up.FromStream(t)
if err != nil {
fmt.Printf("Update failed: %v\n", err)
if errRecover != nil {
fmt.Printf("Failed to recover bad update: %v!\n", errRecover)
fmt.Printf("Program exectuable may be missing!\n")
}
os.Exit(2)
}
fmt.Println("Updated.")
}

func Checksum(args []string) {
if len(args) < 1 {
fatal("No algorithm specified")
}
var h hash.Hash
switch args[0] {
case "md5":
h = md5.New()
case "sha1":
h = sha1.New()
case "sha256":
h = sha256.New()
default:
fatal("Algorithm '" + args[0] + "' is unsupported")
}
io.Copy(h, os.Stdin)
fmt.Printf("%x\n", h.Sum(nil))
}

func main() {
os.Setenv("GUN_VERSION", Version)
basher.Application(map[string]func([]string){}, []string{
basher.Application(map[string]func([]string){
"checksum": Checksum,
"selfupdate": Selfupdate,
}, []string{
"include/fn.bash",
"include/cmd.bash",
"include/module.bash",
Expand Down
1 change: 0 additions & 1 deletion include/cmd.bash
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,3 @@ cmd-help() {
cmd-ns ""
fi
}
cmd-export cmd-help help
3 changes: 2 additions & 1 deletion include/deps.bash
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ deps-install() {
tmpfile="${tmpdir:?}/$name"
curl -s $url > "$tmpfile"
if [[ "$checksum" ]]; then
if ! [[ "$(cat "$tmpfile" | md5)" = "$checksum" ]]; then
if ! [[ "$(cat "$tmpfile" | checksum md5)" = "$checksum" ]]; then
echo "!! Dependency checksum failed: $name $version $checksum" | red
exit 2
fi
Expand All @@ -57,6 +57,7 @@ deps-install() {
eval "$script" > /dev/null
unset PREFIX
else
chmod +x "$tmpfile"
mv "$tmpfile" "$(deps-dir)/bin"
fi
cd - > /dev/null
Expand Down
38 changes: 30 additions & 8 deletions include/gun.bash
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@

readonly latest_version_url="https://dl.gliderlabs.com/glidergun/latest/version.txt"
readonly latest_checksum_url="https://dl.gliderlabs.com/glidergun/latest/%s.tgz.sha256"

declare GUN_MODULE_DIR="${GUN_MODULE_DIR:-cmds}"

gun-init() {
declare desc="Initialize a glidergun project directory"
mkdir .gun
if [[ -f .gitignore ]]; then
if ! grep '^.gun$' .gitignore > /dev/null; then
echo ".gun" >> .gitignore
fi
fi
echo "*" > .gun/.gitignore
echo "!.gitignore" >> .gun/.gitignore
}

gun-version() {
declare desc="Display version of glidergun"
echo "glidergun $GUN_VERSION"
local latest="$(curl -s $latest_version_url)"
if [[ "$GUN_VERSION" == "$latest" ]]; then
latest=""
else
latest=" (latest: $latest)"
fi
echo "glidergun $GUN_VERSION$latest"
}

gun-update() {
declare desc="Self-update glidergun to latest version"
if [[ "$GUN_VERSION" == "$(curl --fail -s $latest_version_url)" ]]; then
echo "glidergun is already up-to-date!"
exit
fi
local platform checksum
platform="$(uname -sm | tr " " "_")"
checksum="$(curl --fail -s $(printf "$latest_checksum_url" "$platform"))"
# calls back into go executable
selfupdate "$platform" "$checksum"
}

gun-find-root() {
Expand Down Expand Up @@ -50,14 +69,17 @@ main() {
cmd-export env-show env
cmd-export fn-call fn
else
if [[ "$1" != "init" && "$1" != "version" && "$1" != "help" ]]; then
local builtins=(init version help selfupdate)
if ! [[ ${builtins[*]} =~ "$1" ]]; then
echo "* Unable to load profile $1" | yellow
fi
fi
fi

cmd-export gun-init init
cmd-export cmd-help help
cmd-export gun-version version

cmd-export gun-update update

cmd-ns "" "$@"
}
2 changes: 2 additions & 0 deletions tests/project/.gun/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions example/cmds/bar.bash → tests/project/cmds/bar.bash
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
init() {
cmd-export-ns bar
cmd-export bar-hello
deps-require jq 1.4
}

bar-hello() {
Expand Down
File renamed without changes.
Loading

0 comments on commit 30c2943

Please sign in to comment.