/
down.go
108 lines (90 loc) · 2.45 KB
/
down.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
package command
import (
"context"
"flag"
"fmt"
"strings"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/mitchellh/cli"
"github.com/yasuoza/switchbot-ble-go/v2/pkg/switchbot"
)
// DownCommand reperesents down command.
type DownCommand struct {
UI *cli.BasicUi
}
type downCfg struct {
Addr string
TimeoutSec int
MaxRetry int
WaitResp bool
}
// Run executes parse args and pass args to RunContext.
func (c *DownCommand) Run(args []string) int {
cfg, parseStatus := c.parseArgs(args)
if parseStatus != 0 {
return parseStatus
}
if err := c.runWithRetry(context.Background(), cfg); err != nil {
msg := fmt.Sprintf("Failed to down SwitchBot: %s", err.Error())
c.UI.Error(msg)
return 1
}
return 0
}
// ConnectAndDown executes connect and on.
func (c *DownCommand) ConnectAndDown(ctx context.Context, cfg *downCfg) error {
bot, err := switchbot.Connect(ctx, cfg.Addr, time.Duration(cfg.TimeoutSec)*time.Second)
if err != nil {
return err
}
defer bot.Disconnect()
if err := bot.Down(cfg.WaitResp); err != nil {
return err
}
return nil
}
// Help represents help message for down command.
func (c *DownCommand) Help() string {
helpText := `
Usage: switchbot down [options] ADDRESS
Will execute down command against a SwitchBot specified by ADDRESS.
Options:
-timeout=10 Connection timeout seconds. (Default 10)
-max-retry=0 Maximum retry count. (Default 0)
-wait=true Wait success/failure response from SwitchBot. (Default true)
`
return strings.TrimSpace(helpText)
}
// Synopsis represents synopsis message for down command.
func (c *DownCommand) Synopsis() string {
return "Trigger down command"
}
func (c *DownCommand) parseArgs(args []string) (*downCfg, int) {
cfg := &downCfg{}
flags := flag.NewFlagSet("on", flag.ContinueOnError)
flags.IntVar(&cfg.TimeoutSec, "timeout", 10, "")
flags.IntVar(&cfg.MaxRetry, "max-retry", 0, "")
flags.BoolVar(&cfg.WaitResp, "wait", true, "")
flags.Usage = func() {
c.UI.Info(c.Help())
}
if err := flags.Parse(args); err != nil {
return cfg, 127
}
args = flags.Args()
if len(args) != 1 {
flags.Usage()
return cfg, 127
}
cfg.Addr = args[0]
return cfg, 0
}
func (c *DownCommand) runWithRetry(ctx context.Context, cfg *downCfg) error {
f := func() error {
return c.ConnectAndDown(ctx, cfg)
}
bo := backoff.NewConstantBackOff(1 * time.Second)
bw := backoff.WithMaxRetries(bo, uint64(cfg.MaxRetry))
return backoff.Retry(f, bw)
}