-
Notifications
You must be signed in to change notification settings - Fork 361
/
status.go
79 lines (63 loc) · 1.91 KB
/
status.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
package cmd
import (
"fmt"
"os"
"time"
"github.com/spf13/cobra"
"github.com/stripe/stripe-cli/pkg/ansi"
"github.com/stripe/stripe-cli/pkg/status"
"github.com/stripe/stripe-cli/pkg/validators"
)
type statusCmd struct {
cmd *cobra.Command
format string
hideSpinner bool
poll bool
pollRate int
verbose bool
}
func newStatusCmd() *statusCmd {
sc := &statusCmd{}
sc.cmd = &cobra.Command{
Use: "status",
Args: validators.NoArgs,
Short: "Check the status of the Stripe API",
RunE: sc.runStatusCmd,
}
sc.cmd.Flags().StringVar(&sc.format, "format", "default", "The format to print the status as (either 'default' or 'json')")
sc.cmd.Flags().BoolVar(&sc.verbose, "verbose", false, "Show status for all Stripe systems")
sc.cmd.Flags().BoolVar(&sc.poll, "poll", false, "Keep polling for status updates")
sc.cmd.Flags().IntVar(&sc.pollRate, "poll-rate", 60, "How many seconds to wait between status updates (minimum: 5)")
sc.cmd.Flags().BoolVar(&sc.hideSpinner, "hide-spinner", false, "Hide the loading spinner when polling")
return sc
}
func (sc *statusCmd) runStatusCmd(cmd *cobra.Command, args []string) error {
if sc.pollRate < 5 {
return fmt.Errorf("poll-rate must be at least 5 seconds, received %d", sc.pollRate)
}
if sc.format != "default" && sc.format != "json" {
return fmt.Errorf("invalid format, must be one of 'default' or 'json', received %s", sc.format)
}
for {
stripeStatus, err := status.GetStatus()
if err != nil {
return err
}
formattedStatus, err := stripeStatus.FormattedMessage(sc.format, sc.verbose)
if err != nil {
return err
}
fmt.Println(formattedStatus)
if !sc.poll {
break
}
if sc.hideSpinner {
time.Sleep(time.Duration(sc.pollRate) * time.Second)
} else {
spinner := ansi.StartSpinner("", os.Stderr)
time.Sleep(time.Duration(sc.pollRate) * time.Second)
ansi.StopSpinner(spinner, "", os.Stderr)
}
}
return nil
}