fabric-samples/off_chain_data/application-go/app.go
Stanislav Jakuschevskij f6858cc7e1
Add second batch of pull request rework
- switch to ClientConnInterface
- use command type alias for map of commands
- add error return to command functions and handle in app.go
- inline formatJSON function in getAllAssets.go
- replace most panics with error returns
- remove error wrapping in listen.go and further down the line
- use strconv.ParseUint instead of ParseFloat
- use WithCancelCause in transact.go to grab and propagate error from
  go routine
- unmarshal and return []Asset in atb.GetAllAssets
- switch to rand package
- remove dependency to protobuf reflect package
- switch to sync.OnceValues for caching parser
- fixed typo in events sample connect.go

Signed-off-by: Stanislav Jakuschevskij <stas@two-giants.com>
2025-02-24 13:14:48 +01:00

61 lines
1.1 KiB
Go

package main
import (
"errors"
"fmt"
"os"
"strings"
"google.golang.org/grpc"
)
type command func(grpc.ClientConnInterface) error
var allCommands = map[string]command{
"getAllAssets": getAllAssets,
"transact": transact,
"listen": listen,
}
func main() {
commands := os.Args[1:]
if len(commands) == 0 {
printUsage()
panic(errors.New("missing command"))
}
for _, name := range commands {
if _, exists := allCommands[name]; !exists {
printUsage()
panic(fmt.Errorf("unknown command: %s", name))
}
fmt.Println("command:", name)
}
client := newGrpcConnection()
defer client.Close()
for _, name := range commands {
command := allCommands[name]
if err := command(client); err != nil {
panic(err)
}
}
}
func printUsage() {
fmt.Println("Arguments: <command1> [<command2> ...]")
fmt.Println("Available commands:", availableCommands())
}
func availableCommands() string {
result := make([]string, len(allCommands))
i := 0
for command := range allCommands {
result[i] = command
i++
}
return strings.Join(result, ", ")
}