Skip to content

Commit e76e441

Browse files
committed
add --watch and --interval flags to
1 parent c3397ce commit e76e441

File tree

1 file changed

+48
-25
lines changed

1 file changed

+48
-25
lines changed

cmd/cloudServerStatus.go

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package cmd
1818
import (
1919
"fmt"
2020
"strings"
21+
"time"
2122

2223
"github.com/spf13/cobra"
2324

@@ -67,46 +68,68 @@ If nothing is currently running, only the 'status' field will be returned with o
6768
Stopped
6869
`,
6970
Run: func(cmd *cobra.Command, args []string) {
71+
watchFlag, _ := cmd.Flags().GetBool("watch")
72+
intervalFlag, _ := cmd.Flags().GetInt("interval")
7073

71-
if len(cloudServerStatusCmdUniqIdFlag) == 0 {
72-
// fetch status of all cloud servers on account
73-
methodArgs := instance.AllPaginatedResultsArgs{
74-
Method: "bleed/storm/server/list",
75-
ResultsPerPage: 100,
74+
if watchFlag {
75+
if intervalFlag <= 0 {
76+
lwCliInst.Die(fmt.Errorf("You must specify an interval greater than zero."))
7677
}
77-
results, err := lwCliInst.AllPaginatedResults(&methodArgs)
78-
if err != nil {
79-
lwCliInst.Die(err)
78+
79+
for {
80+
fmt.Println("\nDisplaying server status (CTRL-C to exit):")
81+
displayCloudSErverStatus(cloudServerStatusCmdUniqIdFlag)
82+
time.Sleep(time.Duration(intervalFlag) * time.Second)
8083
}
84+
} else {
85+
displayCloudSErverStatus(cloudServerStatusCmdUniqIdFlag)
86+
}
87+
},
88+
}
89+
90+
func displayCloudSErverStatus(uniqIdList []string) {
8191

82-
for _, item := range results.Items {
83-
var details apiTypes.CloudServerDetails
84-
if err := instance.CastFieldTypes(item, &details); err != nil {
85-
lwCliInst.Die(err)
86-
}
92+
if len(uniqIdList) == 0 {
93+
// fetch status of all cloud servers on account
94+
methodArgs := instance.AllPaginatedResultsArgs{
95+
Method: "bleed/storm/server/list",
96+
ResultsPerPage: 100,
97+
}
98+
results, err := lwCliInst.AllPaginatedResults(&methodArgs)
99+
if err != nil {
100+
lwCliInst.Die(err)
101+
}
87102

88-
_printCloudServerStatus(details.UniqId, details.Domain)
103+
for _, item := range results.Items {
104+
var details apiTypes.CloudServerDetails
105+
if err := instance.CastFieldTypes(item, &details); err != nil {
106+
lwCliInst.Die(err)
89107
}
90-
} else {
91-
for _, uid := range cloudServerStatusCmdUniqIdFlag {
92-
validateFields := map[interface{}]interface{}{
93-
uid: "UniqId",
94-
}
95-
if err := validate.Validate(validateFields); err != nil {
96-
fmt.Printf("%s ... skipping\n", err)
97-
continue
98-
}
99-
_printCloudServerStatus(uid, "")
108+
109+
_printCloudServerStatus(details.UniqId, details.Domain)
110+
}
111+
} else {
112+
for _, uid := range uniqIdList {
113+
validateFields := map[interface{}]interface{}{
114+
uid: "UniqId",
100115
}
116+
if err := validate.Validate(validateFields); err != nil {
117+
fmt.Printf("%s ... skipping\n", err)
118+
continue
119+
}
120+
_printCloudServerStatus(uid, "")
101121
}
102-
},
122+
}
123+
103124
}
104125

105126
func init() {
106127
cloudServerCmd.AddCommand(cloudServerStatusCmd)
107128

108129
cloudServerStatusCmd.Flags().StringSliceVar(&cloudServerStatusCmdUniqIdFlag, "uniq-id", []string{},
109130
"uniq-id(s) to get status of. For multiple, must be ',' separated")
131+
cloudServerStatusCmd.Flags().Bool("watch", false, "continue to redisplay status at --interval")
132+
cloudServerStatusCmd.Flags().Int("interval", 10, "the interval to fetch the status when --watch is specified")
110133
}
111134

112135
func _printCloudServerStatus(uniqId string, domain string) {

0 commit comments

Comments
 (0)