-
Notifications
You must be signed in to change notification settings - Fork 0
/
protoc.go
79 lines (70 loc) · 2.17 KB
/
protoc.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
package protoc
import (
"archive/zip"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/zhangbao138208/goctls/pkg/downloader"
"github.com/zhangbao138208/goctls/pkg/goctl"
"github.com/zhangbao138208/goctls/rpc/execx"
"github.com/zhangbao138208/goctls/util/env"
"github.com/zhangbao138208/goctls/util/zipx"
"github.com/zhangbao138208/goctls/vars"
)
var url = map[string]string{
"linux_32": "https://github.com/protocolbuffers/protobuf/releases/download/v3.19.4/protoc-3.19.4-linux-x86_32.zip",
"linux_64": "https://github.com/protocolbuffers/protobuf/releases/download/v3.19.4/protoc-3.19.4-linux-x86_64.zip",
"darwin": "https://github.com/protocolbuffers/protobuf/releases/download/v3.19.4/protoc-3.19.4-osx-x86_64.zip",
"windows_32": "https://github.com/protocolbuffers/protobuf/releases/download/v3.19.4/protoc-3.19.4-win32.zip",
"windows_64": "https://github.com/protocolbuffers/protobuf/releases/download/v3.19.4/protoc-3.19.4-win64.zip",
}
const (
Name = "protoc"
ZipFileName = Name + ".zip"
)
func Install(cacheDir string) (string, error) {
return goctl.Install(cacheDir, Name, func(dest string) (string, error) {
goos := runtime.GOOS
tempFile := filepath.Join(os.TempDir(), ZipFileName)
bit := 32 << (^uint(0) >> 63)
var downloadUrl string
switch goos {
case vars.OsMac:
downloadUrl = url[vars.OsMac]
case vars.OsWindows:
downloadUrl = url[fmt.Sprintf("%s_%d", vars.OsWindows, bit)]
case vars.OsLinux:
downloadUrl = url[fmt.Sprintf("%s_%d", vars.OsLinux, bit)]
default:
return "", fmt.Errorf("unsupport OS: %q", goos)
}
err := downloader.Download(downloadUrl, tempFile)
if err != nil {
return "", err
}
return dest, zipx.Unpacking(tempFile, filepath.Dir(dest), func(f *zip.File) bool {
return filepath.Base(f.Name) == filepath.Base(dest)
})
})
}
func Exists() bool {
_, err := env.LookUpProtoc()
return err == nil
}
func Version() (string, error) {
path, err := env.LookUpProtoc()
if err != nil {
return "", err
}
version, err := execx.Run(path+" --version", "")
if err != nil {
return "", err
}
fields := strings.Fields(version)
if len(fields) > 1 {
return fields[1], nil
}
return "", nil
}