/
main.go
142 lines (124 loc) · 2.92 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package main
import (
"archive/zip"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
)
var (
protocType = flag.String("type", "protoc",
`type, "cli" for protoc or "grpc-web" for gRPC Web protoc plugin`)
version = flag.String("ver", "", "version to install, for ex. 3.10.1")
outDir = flag.String("out", "", "output directory")
)
func main() {
flag.Parse()
if len(*version) == 0 {
log.Fatal("Version is not set.\n")
}
if len(*outDir) == 0 {
log.Fatal("Out is not set.\n")
}
if *protocType == "cli" {
installProtoc()
return
}
if *protocType == "grpc-web" {
installGRPCWebPlugin()
return
}
log.Fatal("Unknown type.\n")
}
func download(source, file string) string {
path := filepath.Join(*outDir, file)
if _, err := os.Stat(path); !os.IsNotExist(err) {
return path
}
log.Printf("Downloading from \"%s\"...\n", source)
err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
if err != nil {
panic(err)
}
response, err := http.Get(source)
if err != nil {
panic(err)
}
defer response.Body.Close()
out, err := os.Create(path)
if err != nil {
panic(err)
}
defer out.Close()
_, err = io.Copy(out, response.Body)
if err != nil {
panic(err)
}
return path
}
func remove(path string) {
if err := os.Remove(path); err != nil {
log.Printf("Failed to remove \"%s\": \"%s\".", path, err)
}
}
func installProtoc() {
binFilePath := filepath.Join(*outDir, protocFile)
if _, err := os.Stat(binFilePath); !os.IsNotExist(err) {
log.Printf("protoc is already existing (\"%s\").\n", binFilePath)
return
}
archFile := fmt.Sprintf("protoc-%s-%s.zip", *version, protocPlatform)
url := fmt.Sprintf(
"https://github.com/protocolbuffers/protobuf/releases/download/v%s/%s",
*version, archFile)
archFilePath := download(url, archFile)
defer remove(archFilePath)
log.Printf("Unpacking into \"%s\"...\n", binFilePath)
arch, err := zip.OpenReader(archFilePath)
if err != nil {
panic(err)
}
defer arch.Close()
binInsideArchPath := "bin/" + protocFile
for _, file := range arch.File {
if file.Name != binInsideArchPath {
continue
}
bin, err := os.OpenFile(binFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
if err != nil {
panic(err)
}
r, err := file.Open()
if err != nil {
panic(err)
}
defer r.Close()
_, err = io.Copy(bin, r)
if err != nil {
panic(err)
}
bin.Close()
err = os.Chmod(binFilePath, 0777)
if err != nil {
panic(err)
}
log.Println("protoc successfully installed.")
return
}
log.Fatalf("Archive \"%s\" does not have protoc.\n", archFilePath)
}
func installGRPCWebPlugin() {
fileName := "protoc-gen-grpc-web"
url := fmt.Sprintf(
"https://github.com/grpc/grpc-web/releases/download/%s/%s-%s-%s%s",
*version, fileName, *version, grpcWebPlatform, grpcWebFileExt)
binFilePath := download(url, fileName+grpcWebFileExt)
err := os.Chmod(binFilePath, 0777)
if err != nil {
panic(err)
}
log.Println("gRPC Web protoc plugin installed.")
}