-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.go
137 lines (116 loc) · 3.54 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
// Copyright 2018 AMIS Technologies
// This file is part of the sol2proto
//
// The sol2proto is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The sol2proto is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the sol2proto. If not, see <http://www.gnu.org/licenses/>.
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/getamis/sirius/util"
flag "github.com/spf13/pflag"
"github.com/getamis/sol2proto/grpc"
)
var (
GitVersion string = "dirty"
abiFiles []string
pkgName string
output string
outputDir string
)
func init() {
flag.StringArrayVar(&abiFiles, "abi", []string{}, "ABI files generated by solc")
flag.StringVar(&pkgName, "pkg", "pb", "go package name for the generated proto")
flag.StringVarP(&output, "output", "o", "stdout", "Output destination, could be 'stdout', 'stderr' or a directory")
}
func main() {
flag.Parse()
if len(abiFiles) == 0 {
fmt.Printf("Please specify the abi files\n")
os.Exit(-1)
}
if pkgName == "" {
fmt.Printf("Please specify package name\n")
os.Exit(-1)
}
var serviceProtos []grpc.ProtoFile
var requiredMsgs []grpc.Message
for _, f := range abiFiles {
abiString, err := ioutil.ReadFile(f)
if err != nil {
fmt.Printf("Failed to read input ABI: %v\n", err)
os.Exit(-1)
}
contractAbi, err := abi.JSON(bytes.NewReader(abiString))
if err != nil {
fmt.Printf("Failed to parse contract ABI: %v\n", err)
os.Exit(-1)
}
srvName := util.ToCamelCase(strings.TrimSuffix(filepath.Base(f), filepath.Ext(filepath.Base(f))))
proto, msgs := grpc.GenerateServiceProtoFile(srvName, pkgName, contractAbi, GitVersion)
serviceProtos = append(serviceProtos, proto)
requiredMsgs = append(requiredMsgs, msgs...)
}
// generate messages proto file
msgProto := grpc.GenerateMessageProtoFile("Messages", pkgName, abiFiles, requiredMsgs, GitVersion)
writeCloser, needClose := getDestinationWriter(msgProto.Name)
if needClose {
defer writeCloser.Close()
}
err := msgProto.Render(writeCloser)
if err != nil {
fmt.Printf("Failed to render %s, %v\n", msgProto.Name, err)
os.Exit(-1)
}
// generate service proto files
for _, srvProto := range serviceProtos {
writeCloser, needClose := getDestinationWriter(srvProto.Name)
if needClose {
defer writeCloser.Close()
}
err := srvProto.Render(writeCloser)
if err != nil {
fmt.Printf("Failed to render %s, %v\n", srvProto.Name, err)
os.Exit(-1)
}
}
}
func getDestinationWriter(filename string) (destination io.WriteCloser, needClose bool) {
var err error
filename = fmt.Sprintf("%s.proto", util.ToUnderScore(filename))
switch output {
case "stdout":
destination = os.Stdout
needClose = false
case "stderr":
destination = os.Stderr
needClose = false
default:
if file, err := os.Lstat(output); err == nil && file.IsDir() {
filename = filepath.Join(output, filename)
}
destination, err = os.Create(filename)
if err != nil {
fmt.Printf("Failed to create file %s, %v\n", filename, err)
os.Exit(-1)
}
needClose = true
}
return destination, needClose
}