Skip to content

Commit 10a0773

Browse files
committed
Expands chifra abis --list_events and --list_funcs
1 parent c519f80 commit 10a0773

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

src/apps/chifra/internal/abis/handle_listitems.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,39 @@ package abisPkg
33
import (
44
"errors"
55
"fmt"
6+
"path/filepath"
7+
"strings"
68

9+
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
710
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
811
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/rpc"
912
"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
1013
)
1114

1215
func (opts *AbisOptions) HandleListFuncs(rCtx *output.RenderCtx) (err error) {
13-
return opts.HandleListItems(rCtx, "function")
16+
return opts.HandleListItems(rCtx)
1417
}
1518

1619
func (opts *AbisOptions) HandleListEvents(rCtx *output.RenderCtx) (err error) {
17-
return opts.HandleListItems(rCtx, "event")
20+
return opts.HandleListItems(rCtx)
1821
}
1922

20-
func (opts *AbisOptions) HandleListItems(rCtx *output.RenderCtx, filter string) (err error) {
23+
func (opts *AbisOptions) HandleListItems(rCtx *output.RenderCtx) (err error) {
24+
if len(opts.Addrs) == 0 {
25+
vFunc := func(fn string, vP any) (bool, error) {
26+
_ = vP
27+
_, name := filepath.Split(fn)
28+
if strings.HasSuffix(name, ".json") {
29+
if base.IsValidAddress(strings.TrimSuffix(name, ".json")) {
30+
// silent ignore
31+
opts.Addrs = append(opts.Addrs, strings.TrimSuffix(name, ".json"))
32+
}
33+
}
34+
return true, nil
35+
}
36+
opts.ForEveryAbi(true, vFunc, nil)
37+
}
38+
2139
fetchData := func(modelChan chan types.Modeler, errorChan chan error) {
2240
for _, addr := range opts.Addrs {
2341
functions, which, err := opts.LoadAbis([]string{addr}, false /* known */)
@@ -36,7 +54,7 @@ func (opts *AbisOptions) HandleListItems(rCtx *output.RenderCtx, filter string)
3654
}
3755

3856
for _, f := range functions {
39-
if filter == "" || filter == f.FunctionType {
57+
if opts.ListFilter == "" || opts.ListFilter == f.FunctionType {
4058
modelChan <- f
4159
}
4260
}

src/apps/chifra/internal/abis/options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type AbisOptions struct {
4343
BadFlag error `json:"badFlag,omitempty"` // An error flag if needed
4444
// EXISTING_CODE
4545
ProxyForAddr base.Address `json:"-"`
46+
ListFilter string `json:"-"`
4647
// EXISTING_CODE
4748
}
4849

src/apps/chifra/internal/abis/validate.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ func (opts *AbisOptions) validateAbis() error {
4343
len(opts.Find) == 0 &&
4444
!opts.Count &&
4545
!opts.List &&
46+
!opts.ListFuncs &&
47+
!opts.ListEvents &&
4648
!opts.Known &&
4749
!opts.Globals.Decache {
4850
// If we're not find and not known we better have at least one address
@@ -52,6 +54,18 @@ func (opts *AbisOptions) validateAbis() error {
5254
}
5355
}
5456

57+
if opts.ListFuncs || opts.ListEvents {
58+
if opts.List {
59+
return validate.Usage("The {0} options cannot be used with {1}.", "--list", "--listFuncs or --listEvents")
60+
}
61+
opts.ListFilter = "" // assume both pass
62+
if opts.ListFuncs && !opts.ListEvents {
63+
opts.ListFilter = "function"
64+
} else if opts.ListEvents && !opts.ListFuncs {
65+
opts.ListFilter = "event"
66+
}
67+
}
68+
5569
other := len(opts.Encode) != 0 || len(opts.Find) != 0 || opts.Globals.Decache
5670
if other && (opts.Count || opts.List) {
5771
return validate.Usage("The {0} options must be used alone.", "--count and --list")

0 commit comments

Comments
 (0)