-
Notifications
You must be signed in to change notification settings - Fork 7
/
client.go
127 lines (111 loc) · 3.52 KB
/
client.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package internal
import (
"errors"
"fmt"
"os"
"runtime"
"github.com/spf13/cobra"
"github.com/threefoldfoundation/tfchain/pkg/config"
"github.com/threefoldtech/rivine/pkg/api"
"github.com/threefoldtech/rivine/pkg/cli"
"github.com/threefoldtech/rivine/pkg/client"
rivinec "github.com/threefoldtech/rivine/pkg/client"
"github.com/threefoldtech/rivine/types"
)
// Config defines the configuration for the bridge (CLI) client.
type Config struct {
// These values aren't used for validation,
// but only in order to estimate progress with the syncing of your consensus.
BlockFrequencyInSeconds int64
GenesisBlockTimestamp types.Timestamp
}
// CommandLineClient specific for bridge client
type CommandLineClient struct {
*api.HTTPClient
Config *client.Config
RootCmd *cobra.Command
ERC20Cmd *cobra.Command
TFChainCmd *cobra.Command
PreRunE func(*client.Config) (*client.Config, error)
}
// NewCommandLineClient creates a new CLI client, which can be run as it is,
// or be extended/modified to fit your needs.
func NewCommandLineClient(address, name, userAgent string) (*CommandLineClient, error) {
if address == "" {
address = "http://localhost:23111"
}
if name == "" {
name = "R?v?ne"
}
client := new(CommandLineClient)
client.HTTPClient = &api.HTTPClient{
RootURL: address,
UserAgent: userAgent,
}
createRootCmd(os.Args[0], name, client)
// create command tree
client.RootCmd.AddCommand(&cobra.Command{
Use: "version",
Short: "Print version information",
Long: "Print version information.",
Run: rivinec.Wrap(func() {
fmt.Printf("Bride Client %s", config.GetBlockchainInfo().ChainVersion)
fmt.Println()
fmt.Printf("Go Version v%s\r\n", runtime.Version()[2:])
fmt.Printf("GOOS %s\r\n", runtime.GOOS)
fmt.Printf("GOARCH %s\r\n", runtime.GOARCH)
}),
})
client.RootCmd.AddCommand(&cobra.Command{
Use: "stop",
Short: fmt.Sprintf("Stop the %s bridge", name),
Long: fmt.Sprintf("Stop the %s bridge.", name),
Run: rivinec.Wrap(func() {
err := client.Post("/bridge/stop", "")
if err != nil {
cli.Die("Could not stop bridge:", err)
}
fmt.Println("bridge stopped.")
}),
})
client.ERC20Cmd = CreateERC20Cmd(client)
client.RootCmd.AddCommand(client.ERC20Cmd)
client.TFChainCmd = createTFChainCommand(client)
client.RootCmd.AddCommand(client.TFChainCmd)
// parse flags
client.RootCmd.PersistentFlags().StringVarP(&client.HTTPClient.RootURL, "addr", "a",
client.HTTPClient.RootURL, fmt.Sprintf(
"which host/port to communicate with (i.e. the host/port %sd is listening on)",
name))
// return client
return client, nil
}
// Run the CLI, logic dependend upon the command the user used.
func (cli *CommandLineClient) Run() error {
return cli.RootCmd.Execute()
}
// preRunE checks that all preConditions match
func (cli *CommandLineClient) preRunE(*cobra.Command, []string) error {
address, err := sanitizeURL(cli.HTTPClient.RootURL)
if err != nil {
return fmt.Errorf("invalid daemon RPC address %q: %v", cli.HTTPClient.RootURL, err)
}
cli.HTTPClient.RootURL = address
if cli.Config == nil {
var err error
cli.Config, err = client.FetchConfigFromDaemon(cli.HTTPClient)
if err != nil {
fmt.Fprintf(os.Stderr, "fetching config from daemon failed: %v\r\n", err)
}
}
if cli.PreRunE != nil {
cli.Config, err = cli.PreRunE(cli.Config)
if err != nil {
return fmt.Errorf("user-defined pre-run callback failed: %v", err)
}
}
if cli.Config == nil {
return errors.New("cannot run command line client: no config is defined")
}
return nil
}