add a dist_get script for getting bins from dist.ipfs.io
License: MIT Signed-off-by: Jeromy <why@ipfs.io>
Jeromy committed
Apr 16, 2016 at 12:12 UTC
284bb1866fe494c0edb038793e4f7b34ffd18171
2 files changed
+161
-10
Makefile
+18
-10
@@ -9,6 +9,12 @@ else
9
go_test=go test
10
endif
11
12
+
13
+gx_bin=bin/gx-v0.6.0
14
+gx-go_bin=bin/gx-go-v1.1.0
15
+
16
+# use things in our bin before any other system binaries
17
+export PATH := bin:$(PATH)
18
export IPFS_API ?= v04x.ipfs.io
19
20
all: help
@@ -21,21 +27,23 @@ toolkit_upgrade: gx_upgrade gxgo_upgrade
27
go_check:
28
@bin/check_go_version $(IPFS_MIN_GO_VERSION)
29
24
-gx_upgrade:
25
- go get -u github.com/whyrusleeping/gx
30
+bin/gx-%:
31
+ @echo "installing gx $(@:bin/gx-%=%)"
32
+ @bin/dist_get gx $@ $(@:bin/gx-%=%)
33
27
-gxgo_upgrade:
28
- go get -u github.com/whyrusleeping/gx-go
34
+bin/gx-go-%:
35
+ @echo "installing gx-go $(@:bin/gx-go-%=%)"
36
+ @bin/dist_get gx-go $@ $(@:bin/gx-go-%=%)
37
+ rm -f bin/gx-go
38
+ ln -s $(@:bin/%=%) bin/gx-go
39
+
40
+gx_check: ${gx_bin} ${gx-go_bin}
41
42
path_check:
43
@bin/check_go_path $(realpath $(shell pwd)) $(realpath $(GOPATH)/src/github.com/ipfs/go-ipfs)
44
33
-gx_check:
34
- @bin/check_gx_program "gx" $(IPFS_MIN_GX_VERSION) 'Upgrade or install gx using your package manager or run `make gx_upgrade`'
35
- @bin/check_gx_program "gx-go" $(IPFS_MIN_GX_GO_VERSION) 'Upgrade or install gx-go using your package manager or run `make gxgo_upgrade`'
36
-
45
deps: go_check gx_check path_check
38
- gx --verbose install --global
46
+ ${gx_bin} --verbose install --global
47
48
# saves/vendors third-party dependencies to Godeps/_workspace
49
# -r flag rewrites import paths to use the vendored path
@@ -58,7 +66,7 @@ clean:
66
uninstall:
67
make -C cmd/ipfs uninstall
68
61
-PHONY += all help godep toolkit_upgrade gx_upgrade gxgo_upgrade gx_check
69
+PHONY += all help godep toolkit_upgrade gx_check
70
PHONY += go_check deps vendor install build nofuse clean uninstall
71
72
##############################################################
bin/dist_get
new
+143
@@ -0,0 +1,143 @@
1
+#!/bin/bash
2
+
3
+die() {
4
+ echo "$@" >&2
5
+ exit 1
6
+}
7
+
8
+have_binary() {
9
+ type "$1" > /dev/null
10
+}
11
+
12
+check_writeable() {
13
+ printf "" > "$1" && rm "$1"
14
+}
15
+
16
+download() {
17
+ local url="$1"
18
+ local output="$2"
19
+
20
+ if [ -z "$url" ] || [ -z "$output" ]; then
21
+ die "download takes exactly two arguments. was given '$@'"
22
+ fi
23
+
24
+ if ! check_writeable "$output"; then
25
+ die "download error: cannot write to $output"
26
+ fi
27
+
28
+ printf 'Downloading "%s" to "%s"\n' "$url" "$output"
29
+ if have_binary wget; then
30
+ wget "$url" -O "$output"
31
+ elif have_binary curl; then
32
+ curl --silent "$url" > "$output"
33
+ elif have_binary fetch; then
34
+ fetch "$url" -o "$output"
35
+ else
36
+ die "no binary found to download $url. exiting."
37
+ fi
38
+}
39
+
40
+unarchive() {
41
+ local archivetype="$1"
42
+ local infile="$2"
43
+ local outfile="$3"
44
+ local distname="$4"
45
+
46
+ if ! check_writeable "$outfile"; then
47
+ die "unarchive error: cannot write to $outfile"
48
+ fi
49
+
50
+ case $archivetype in
51
+ tar.gz)
52
+ if have_binary tar; then
53
+ echo "==> using 'tar' to extract binary from archive"
54
+ tar -x "$distname/$distname" -f "$infile" -O > "$outfile"
55
+ else
56
+ die "no binary on system for extracting tar files"
57
+ fi
58
+ ;;
59
+ zip)
60
+ if have_binary unzip; then
61
+ echo "==> using 'unzip' to extract binary from archive"
62
+ unzip -p "$infile" "$distname/$distname" > "$outfile"
63
+ else
64
+ die "no installed method for extracting .zip archives"
65
+ fi
66
+ ;;
67
+ *)
68
+ die "unrecognized archive type '$archivetype'"
69
+ esac
70
+
71
+ chmod +x "$outfile"
72
+}
73
+
74
+get_go_vars() {
75
+ if [ ! -z "$GOOS" ] && [ ! -z "$GOARCH" ]; then
76
+ printf "%s-%s" "$GOOS" "$GOARCH"
77
+ fi
78
+
79
+ if have_binary go; then
80
+ printf "%s-%s" "$(go env GOOS)" "$(go env GOARCH)"
81
+ else
82
+ die "no way of determining system GOOS and GOARCH\nPlease manually set GOOS and GOARCH then retry."
83
+ fi
84
+}
85
+
86
+mkurl() {
87
+ local name="$1"
88
+ local vers="$2"
89
+ local archive="$3"
90
+
91
+ local govars=$(get_go_vars)
92
+
93
+ echo "http://dist.ipfs.io/$name/$vers/${name}_${vers}_$govars.$archive"
94
+}
95
+
96
+distname="$1"
97
+outpath="$2"
98
+version="$3"
99
+
100
+if [ -z "$distname" ] || [ -z "$outpath" ] || [ -z "$version" ]; then
101
+ die "usage: dist_get <distname> <outpath> <version>"
102
+fi
103
+
104
+if [ ${version:0:1} != "v" ]; then
105
+ die "versions must begin with 'v', for example: v0.4.0"
106
+fi
107
+
108
+# TODO: don't depend on the go tool being installed to detect this
109
+goenv=$(get_go_vars)
110
+
111
+case $goenv in
112
+ linux-*)
113
+ archive="tar.gz"
114
+ ;;
115
+ darwin-*)
116
+ archive="tar.gz"
117
+ ;;
118
+ windows-*)
119
+ archive="zip"
120
+ ;;
121
+ freebsd-*)
122
+ archive="tar.gz"
123
+ ;;
124
+ *)
125
+ echo "unrecognized system environment: $goenv" >&2
126
+ die "currently only linux, darwin, windows and freebsd are supported by this script"
127
+esac
128
+
129
+
130
+mkdir -p bin/tmp
131
+
132
+url=$(mkurl "$distname" "$version" "$archive")
133
+tmpfi="bin/tmp/$distname.$archive"
134
+
135
+download "$url" "$tmpfi"
136
+if [ $? -ne 0 ]; then
137
+ die "failed to download $url to $tmpfi"
138
+fi
139
+
140
+unarchive "$archive" "$tmpfi" "$outpath" "$distname"
141
+if [ $? -ne 0 ]; then
142
+ die "failed to exract archive $tmpfi"
143
+fi