/
common.go
68 lines (56 loc) · 1.78 KB
/
common.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
/*
Package commands contains any general setup/helpers valid for all subcommands
*/
package commands
import (
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/tendermint/tendermint/lite"
rpcclient "github.com/tendermint/tendermint/rpc/client"
"github.com/CyberMiles/travis/sdk/client"
)
var (
trustedProv lite.Provider
sourceProv lite.Provider
)
const (
ChainFlag = "chain-id"
NodeFlag = "node"
CliHomeFlag = "cli-home"
)
// AddBasicFlags adds --node and --chain-id, which we need for everything
func AddBasicFlags(cmd *cobra.Command) {
cmd.PersistentFlags().String(ChainFlag, "", "Chain ID of tendermint node")
cmd.PersistentFlags().String(NodeFlag, "", "<host>:<port> to tendermint rpc interface for this chain")
cmd.PersistentFlags().String(CliHomeFlag, os.ExpandEnv("$HOME/.travis-cli"), "directory for cli")
}
// GetChainID reads ChainID from the flags
func GetChainID() string {
return viper.GetString(ChainFlag)
}
// GetNode prepares a simple rpc.Client from the flags
func GetNode() rpcclient.Client {
return client.GetNode(viper.GetString(NodeFlag))
}
// GetSourceProvider returns a provider pointing to an rpc handler
func GetSourceProvider() lite.Provider {
if sourceProv == nil {
node := viper.GetString(NodeFlag)
sourceProv = client.GetRPCProvider(node)
}
return sourceProv
}
// GetTrustedProvider returns a reference to a local store with cache
func GetTrustedProvider() lite.Provider {
if trustedProv == nil {
rootDir := viper.GetString(CliHomeFlag)
trustedProv = client.GetLocalProvider(rootDir)
}
return trustedProv
}
// GetProviders creates a trusted (local) seed provider and a remote
// provider based on configuration.
func GetProviders() (trusted lite.Provider, source lite.Provider) {
return GetTrustedProvider(), GetSourceProvider()
}