OSDN Git Service

add package
[bytom/vapor.git] / vendor / github.com / multiformats / go-multiaddr-net / bin / dist_get
1 #!/bin/bash
2
3 die() {
4         echo "$@" >&2
5         exit 1
6 }
7
8 have_binary() {
9         type "$1" > /dev/null 2> /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         if have_binary wget; then
29                 printf 'Using wget to download "%s" to "%s"\n' "$url" "$output"
30                 wget "$url" -O "$output"
31         elif have_binary curl; then
32                 printf 'Using curl to download "%s" to "%s"\n' "$url" "$output"
33                 curl --silent "$url" > "$output"
34         elif have_binary fetch; then
35                 printf 'Using fetch to download "%s" to "%s"\n' "$url" "$output"
36                 fetch "$url" -o "$output"
37         else
38                 die "no binary found to download $url. exiting."
39         fi
40 }
41
42 unarchive() {
43         local archivetype="$1"
44         local infile="$2"
45         local outfile="$3"
46         local distname="$4"
47
48         if ! check_writeable "$outfile"; then
49                 die "unarchive error: cannot write to $outfile"
50         fi
51
52         case $archivetype in
53                 tar.gz)
54                         if have_binary tar; then
55                                 echo "==> using 'tar' to extract binary from archive"
56                                 cat "$infile" | tar -O -z -x "$distname/$distname" > "$outfile"
57                         else
58                                 die "no binary on system for extracting tar files"
59                         fi
60                         ;;
61                 zip)
62                         if have_binary unzip; then
63                                 echo "==> using 'unzip' to extract binary from archive"
64                                 unzip -p "$infile" "$distname/$distname" > "$outfile"
65                         else
66                                 die "no installed method for extracting .zip archives"
67                         fi
68                         ;;
69                 *)
70                         die "unrecognized archive type '$archivetype'"
71         esac
72
73         chmod +x "$outfile"
74 }
75
76 get_go_vars() {
77         if [ ! -z "$GOOS" ] && [ ! -z "$GOARCH" ]; then
78                 printf "%s-%s" "$GOOS" "$GOARCH"
79         fi
80
81         if have_binary go; then
82                 printf "%s-%s" "$(go env GOOS)" "$(go env GOARCH)"
83         else
84                 die "no way of determining system GOOS and GOARCH\nPlease manually set GOOS and GOARCH then retry."
85         fi
86 }
87
88 mkurl() {
89         local name="$1"
90         local vers="$2"
91         local archive="$3"
92
93         local govars=$(get_go_vars)
94
95         echo "http://dist.ipfs.io/$name/$vers/${name}_${vers}_$govars.$archive"
96 }
97
98 distname="$1"
99 outpath="$2"
100 version="$3"
101
102 if [ -z "$distname" ] || [ -z "$outpath" ] || [ -z "$version" ]; then
103         die "usage: dist_get <distname> <outpath> <version>"
104 fi
105
106 if [ ${version:0:1} != "v" ]; then
107         echo "invalid version '$version'" >&2
108         die "versions must begin with 'v', for example: v0.4.0"
109 fi
110
111 # TODO: don't depend on the go tool being installed to detect this
112 goenv=$(get_go_vars)
113
114 case $goenv in
115         linux-*)
116                 archive="tar.gz"
117                 ;;
118         darwin-*)
119                 archive="tar.gz"
120                 ;;
121         windows-*)
122                 archive="zip"
123                 ;;
124         freebsd-*)
125                 archive="tar.gz"
126                 ;;
127         *)
128                 echo "unrecognized system environment: $goenv" >&2
129                 die "currently only linux, darwin, windows and freebsd are supported by this script"
130 esac
131
132
133 mkdir -p bin/tmp
134
135 url=$(mkurl "$distname" "$version" "$archive")
136 tmpfi="bin/tmp/$distname.$archive"
137
138 download "$url" "$tmpfi"
139 if [ $? -ne 0 ]; then
140         die "failed to download $url to $tmpfi"
141 fi
142
143 unarchive "$archive" "$tmpfi" "$outpath" "$distname"
144 if [ $? -ne 0 ]; then
145         die "failed to exract archive $tmpfi"
146 fi