-
Notifications
You must be signed in to change notification settings - Fork 219
Expand file tree
/
Copy pathtwopc.go
More file actions
78 lines (65 loc) · 1.59 KB
/
twopc.go
File metadata and controls
78 lines (65 loc) · 1.59 KB
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
/*
* Radon
*
* Copyright 2018 The Radon Authors.
* Code is licensed under the GPLv3.
*
*/
package cmd
import (
"net/http"
"xbase"
"github.com/spf13/cobra"
)
func NewTwopcCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "twopc",
Short: "disable/enable radon to twopc",
}
cmd.AddCommand(NewTwopcEnableCommand())
cmd.AddCommand(NewTwopcDisableCommand())
cmd.PersistentFlags().StringVar(&radonHost, "radon-host", "127.0.0.1", "--radon-host=[ip]")
return cmd
}
func setTwopc(url string, twopc bool) {
type request struct {
Twopc bool `json:"twopc"`
}
req := &request{
Twopc: twopc,
}
resp, cleanup, err := xbase.HTTPPut(url, &req)
defer cleanup()
if err != nil {
log.Panicf("error:%+v", err)
}
if resp == nil || resp.StatusCode != http.StatusOK {
log.Panicf("radoncli.set.twopc.to.[%v].url[%s].response.error:%+s", twopc, url, xbase.HTTPReadBody(resp))
}
}
// NewTwopcEnableCommand enable twopc.
func NewTwopcEnableCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "enable",
Short: "enable radon to twopc",
Run: twopcEnableCommand,
}
return cmd
}
func twopcEnableCommand(cmd *cobra.Command, args []string) {
twopcUrl := "http://" + radonHost + ":8080/v1/radon/twopc"
setTwopc(twopcUrl, true)
}
// NewTwopcDisableCommand disable twopc.
func NewTwopcDisableCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "disable",
Short: "disable radon twopc",
Run: twopcDisableCommand,
}
return cmd
}
func twopcDisableCommand(cmd *cobra.Command, args []string) {
twopcUrl := "http://" + radonHost + ":8080/v1/radon/twopc"
setTwopc(twopcUrl, false)
}