Skip to content

Commit 7e0ede8

Browse files
author
gregorgololicic
committed
implement pass in api url for all commands
1 parent 426fe1c commit 7e0ede8

5 files changed

Lines changed: 38 additions & 2 deletions

File tree

cmd/account/get/get.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ func NewCmdGet(gateway gateway.IGateway, version string) *cobra.Command {
1313
blockHeight int
1414
filter string
1515
json bool
16+
api string
1617
)
1718

1819
getCmd := &cobra.Command{
@@ -23,6 +24,10 @@ func NewCmdGet(gateway gateway.IGateway, version string) *cobra.Command {
2324
Gets an account by address (address, balance, keys, code)`,
2425
Args: cobra.ExactArgs(1),
2526
Run: func(cmd *cobra.Command, args []string) {
27+
if api != "" {
28+
gateway.SetAPIURL(api)
29+
}
30+
2631
account := gateway.GetAccount(args[0])
2732

2833
if !json {
@@ -34,6 +39,7 @@ Gets an account by address (address, balance, keys, code)`,
3439
}
3540

3641
getCmd.PersistentFlags().BoolVarP(&json, "json", "j", false, "show output format as JSON")
42+
getCmd.PersistentFlags().StringVarP(&api, "api", "a", "", "set discovery api host on the fly only for this request")
3743
getCmd.Flags().IntVarP(&blockHeight, "block-height", "b", -1, "gets an account at the given block height")
3844
getCmd.Flags().StringVarP(&filter, "filter", "f", "", "filter output to show only provided field (address, balance, code, keys)")
3945

gateway/gateway.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
// needed by the cli to be implemented by each gateway
99
type IGateway interface {
1010
GetAccount(address string) *models.Account
11+
SetAPIURL(url string)
1112
}
1213

1314
// CreateGateway creates a gateway from a factory

gateway/grpc.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@ type GRPC struct {
1616
APIURL string
1717
}
1818

19+
// SetAPIURL sets the url of the discovery api
20+
func (g *GRPC) SetAPIURL(url string) {
21+
// todo do url validation
22+
23+
g.APIURL = url
24+
}
25+
1926
// GetAccount gets account by the address via grpc call
2027
func (g *GRPC) GetAccount(address string) *models.Account {
21-
// todo move this to singleton for grpc and should be executed by root cmd
2228
flowClient, err := client.New(g.APIURL, grpc.WithInsecure())
2329

2430
if err != nil {

gateway/rest.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ import "github.com/sideninja/flow-cli/models"
55
// Rest possible implementation
66
type Rest struct{}
77

8+
// SetAPIURL sets the url
9+
func (r *Rest) SetAPIURL(url string) {
10+
// not implemented
11+
}
12+
813
// GetAccount gets account over rest api
914
func (r *Rest) GetAccount(address string) *models.Account {
1015
// not implemented

main.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,28 @@ import (
66

77
cmd "github.com/sideninja/flow-cli/cmd"
88
"github.com/sideninja/flow-cli/gateway"
9+
"github.com/spf13/viper"
910
)
1011

12+
func init() {
13+
viper.SetConfigName("config")
14+
viper.AddConfigPath(".")
15+
viper.SetDefault("APIURL", "localhost:3569")
16+
17+
if err := viper.ReadInConfig(); err != nil {
18+
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
19+
20+
} else {
21+
// Config file was found but another error was produced
22+
}
23+
}
24+
}
25+
1126
func main() {
12-
gateway := gateway.CreateGateway("", "localhost:3569")
27+
28+
APIURL := viper.GetString("APIURL")
29+
30+
gateway := gateway.CreateGateway("", APIURL)
1331
version := "1.0"
1432

1533
rootCmd := cmd.NewCmdRoot(gateway, version)

0 commit comments

Comments
 (0)