Skip to content

Commit 183bc53

Browse files
committed
cmd/abigen: Add support for STDIN to abigen
Allow the --abi flag to be given - to indicate that it should read the ABI information from standard input. It expects to read the solc output with the --combined-json flag providing bin, abi, userdoc, devdoc, and metadata, and works very similarly to the internal invocation of solc, except it allows external invocation of solc. This facilitiates integration with more complex solc invocations, such as invocations that require path remapping or --allow-paths tweaks. Simple usage example: solc --combined-json bin,abi,userdoc,devdoc,metadata *.sol | abigen --abi -
1 parent fcca429 commit 183bc53

File tree

1 file changed

+26
-6
lines changed

1 file changed

+26
-6
lines changed

cmd/abigen/main.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
)
3030

3131
var (
32-
abiFlag = flag.String("abi", "", "Path to the Ethereum contract ABI json to bind")
32+
abiFlag = flag.String("abi", "", "Path to the Ethereum contract ABI json to bind, - for STDIN")
3333
binFlag = flag.String("bin", "", "Path to the Ethereum contract bytecode (generate deploy method)")
3434
typFlag = flag.String("type", "", "Struct name for the binding (default = package name)")
3535

@@ -75,16 +75,27 @@ func main() {
7575
bins []string
7676
types []string
7777
)
78-
if *solFlag != "" {
78+
if *solFlag != "" || *abiFlag == "-" {
7979
// Generate the list of types to exclude from binding
8080
exclude := make(map[string]bool)
8181
for _, kind := range strings.Split(*excFlag, ",") {
8282
exclude[strings.ToLower(kind)] = true
8383
}
84-
contracts, err := compiler.CompileSolidity(*solcFlag, *solFlag)
85-
if err != nil {
86-
fmt.Printf("Failed to build Solidity contract: %v\n", err)
87-
os.Exit(-1)
84+
85+
var contracts map[string]*compiler.Contract
86+
var err error
87+
if *solFlag != "" {
88+
contracts, err = compiler.CompileSolidity(*solcFlag, *solFlag)
89+
if err != nil {
90+
fmt.Printf("Failed to build Solidity contract: %v\n", err)
91+
os.Exit(-1)
92+
}
93+
} else {
94+
contracts, err = contractsFromStdin()
95+
if err != nil {
96+
fmt.Printf("Failed to read input ABIs from STDIN: %v\n", err)
97+
os.Exit(-1)
98+
}
8899
}
89100
// Gather all non-excluded contract for binding
90101
for name, contract := range contracts {
@@ -138,3 +149,12 @@ func main() {
138149
os.Exit(-1)
139150
}
140151
}
152+
153+
func contractsFromStdin() (map[string]*compiler.Contract, error) {
154+
bytes, err := ioutil.ReadAll(os.Stdin)
155+
if err != nil {
156+
return nil, err
157+
}
158+
159+
return compiler.ParseCombinedJSON(bytes, "", "", "", "")
160+
}

0 commit comments

Comments
 (0)