-
Notifications
You must be signed in to change notification settings - Fork 20
/
runstatus_maintenance_update.go
111 lines (90 loc) · 2.72 KB
/
runstatus_maintenance_update.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
109
110
111
package cmd
import (
"bufio"
"fmt"
"os"
"text/tabwriter"
"github.com/exoscale/egoscale"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)
// updateCmd represents the update command
var runstatusMaintenanceUpdateCmd = &cobra.Command{
Use: "update [page name] <maintenance name>",
Short: "update a maintenance",
Long: `Update a maintenance.
This is also used to close an maintenance`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}
pageName := gCurrentAccount.DefaultRunstatusPage
maintenanceName := args[0]
if gCurrentAccount.DefaultRunstatusPage == "" && len(args) == 1 {
fmt.Fprintf(os.Stderr, `Error: No default runstat.us page is set:
Please specify a page in parameter or add it to %q
`, gConfigFilePath)
return cmd.Usage()
}
if len(args) > 1 {
pageName = args[0]
maintenanceName = args[1]
}
runstatusPage, err := csRunstatus.GetRunstatusPage(gContext, egoscale.RunstatusPage{Subdomain: pageName})
if err != nil {
return err
}
maintenance, err := getRunstatusMaintenanceByNameOrID(*runstatusPage, maintenanceName)
if err != nil {
return err
}
reader := bufio.NewReader(os.Stdin)
description, err := cmd.Flags().GetString(runstatusFlagDescription)
if err != nil {
return err
}
if description == "" {
description, err = readInput(reader, "Description for the maintenance", "none")
if err != nil {
return err
}
}
status, err := cmd.Flags().GetString(runstatusFlagStatus)
if err != nil {
return nil
}
if status == "" {
prompt := promptui.Select{
Label: "Status",
Items: []string{"scheduled", "in-progress", "completed"},
}
_, status, err = prompt.Run()
if err != nil {
return fmt.Errorf("prompt failed %v", err)
}
}
fmt.Println()
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent)
fmt.Fprintf(w, "Description:\t%s\n", description) // nolint: errcheck
fmt.Fprintf(w, "Status:\t%s\n", status) // nolint: errcheck
if err := w.Flush(); err != nil {
return err
}
if !askQuestion("sure you want to update this maintenance") {
return nil
}
if err := csRunstatus.UpdateRunstatusMaintenance(gContext, *maintenance, egoscale.RunstatusEvent{
Text: description,
Status: status,
}); err != nil {
return err
}
fmt.Printf("Maintenance %q successfully updated\n", maintenanceName)
return nil
},
}
func init() {
runstatusMaintenanceCmd.AddCommand(runstatusMaintenanceUpdateCmd)
runstatusMaintenanceUpdateCmd.Flags().StringP(runstatusFlagDescription, "d", "", "Description of the maintenance")
runstatusMaintenanceUpdateCmd.Flags().StringP(runstatusFlagStatus, "s", "", "<scheduled | in-progress | completed>")
}