-
Notifications
You must be signed in to change notification settings - Fork 11
/
cmd.go
102 lines (94 loc) · 2.29 KB
/
cmd.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
package relay
import (
"github.com/hyperledger-labs/yui-relayer/config"
"github.com/hyperledger-labs/yui-relayer/core"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
const (
flagSrc = "src"
)
func LCPCmd(ctx *config.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "lcp",
Short: "LCP commands",
}
cmd.AddCommand(
registerEnclaveKeyCmd(ctx),
activateClientCmd(ctx),
)
return cmd
}
func registerEnclaveKeyCmd(ctx *config.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "register-key [path]",
Short: "Register an enclave key into the LCP client",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, src, dst, err := ctx.Config.ChainsFromPath(args[0])
if err != nil {
return err
}
path, err := ctx.Config.Paths.Get(args[0])
if err != nil {
return err
}
var (
pathEnd *core.PathEnd
target *core.ProvableChain
)
if viper.GetBool(flagSrc) {
pathEnd = path.Src
target = c[src]
} else {
pathEnd = path.Dst
target = c[dst]
}
prover := target.Prover.(*Prover)
if err := prover.initServiceClient(); err != nil {
return err
}
// TODO add debug option
return registerEnclaveKey(pathEnd, prover, true)
},
}
return srcFlag(cmd)
}
func activateClientCmd(ctx *config.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "activate-client [path]",
Short: "Activate the LCP client",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, src, dst, err := ctx.Config.ChainsFromPath(args[0])
if err != nil {
return err
}
path, err := ctx.Config.Paths.Get(args[0])
if err != nil {
return err
}
var (
pathEnd *core.PathEnd
target *core.ProvableChain
counterparty *core.ProvableChain
)
if viper.GetBool(flagSrc) {
pathEnd = path.Src
target, counterparty = c[src], c[dst]
} else {
pathEnd = path.Dst
target, counterparty = c[dst], c[src]
}
return activateClient(pathEnd, target, counterparty)
},
}
return srcFlag(cmd)
}
func srcFlag(cmd *cobra.Command) *cobra.Command {
cmd.Flags().BoolP(flagSrc, "", true, "a boolean value whether src is the target chain")
if err := viper.BindPFlag(flagSrc, cmd.Flags().Lookup(flagSrc)); err != nil {
panic(err)
}
return cmd
}