/
enablemaintenance.go
111 lines (96 loc) · 2.88 KB
/
enablemaintenance.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 migratorv4
import (
"fmt"
"time"
"github.com/briandowns/spinner"
"github.com/chef/automate/components/automate-deployment/pkg/cli"
"github.com/chef/automate/lib/io/fileutils"
"github.com/fatih/color"
)
type EnableMaintenance struct {
writer *cli.Writer
utils MigratorV4Utils
fileutils fileutils.FileUtils
spinner *spinner.Spinner
runError error
hasError bool
isExecuted bool
spinnerTimeout time.Duration
timeout int64
}
func NewEnableMaintenance(w *cli.Writer, utils MigratorV4Utils, timeout int64, spinnerTimeout time.Duration) *EnableMaintenance {
return &EnableMaintenance{
writer: w,
utils: utils,
timeout: timeout,
spinnerTimeout: spinnerTimeout,
}
}
func (em *EnableMaintenance) Run() (err error) {
em.showTurningOn()
_, _, err = em.utils.SetMaintenanceMode(em.timeout, true)
if err != nil {
em.showError()
em.setError(err)
return err
}
em.setExecuted()
em.showSuccess()
return nil
}
func (em *EnableMaintenance) showTurningOn() {
em.spinner = em.writer.NewSpinnerWithTab()
em.spinner.Suffix = fmt.Sprintf(" Turning ON maintenance mode")
em.spinner.Start()
time.Sleep(em.spinnerTimeout)
}
func (em *EnableMaintenance) showSuccess() {
em.spinner.FinalMSG = SPACES_BEFORE_STEPS + " " + fmt.Sprintf(color.New(color.FgGreen).Sprint("✔")+" Maintenance mode turned ON successfully")
em.spinner.Stop()
em.writer.Println("")
}
func (em *EnableMaintenance) showError() {
em.spinner.FinalMSG = SPACES_BEFORE_STEPS + " " + color.New(color.FgRed).Sprint("✖") + " Failed to turn maintenance mode ON"
em.spinner.Stop()
em.writer.Println("")
}
func (em *EnableMaintenance) showTurningOff() {
em.spinner = em.writer.NewSpinnerWithTab()
em.spinner.Suffix = fmt.Sprintf(" Turning OFF maintenance mode")
em.spinner.Start()
time.Sleep(em.spinnerTimeout)
}
func (em *EnableMaintenance) showOffSuccess() {
em.spinner.FinalMSG = SPACES_BEFORE_STEPS + " " + fmt.Sprintf(color.New(color.FgGreen).Sprint("✔")+" Maintenance mode turned OFF successfully")
em.spinner.Stop()
em.writer.Println("")
}
func (em *EnableMaintenance) showOffError() {
em.spinner.FinalMSG = SPACES_BEFORE_STEPS + " " + color.New(color.FgRed).Sprint("✖") + " Failed to turn maintenance mode OFF"
em.spinner.Stop()
em.writer.Println("")
}
func (em *EnableMaintenance) setExecuted() {
em.isExecuted = true
}
func (em *EnableMaintenance) OnSuccess() (err error) {
em.showTurningOff()
_, _, err = em.utils.SetMaintenanceMode(em.timeout, false)
if err != nil {
em.showOffError()
em.ErrorHandler()
return err
}
em.showOffSuccess()
return nil
}
func (em *EnableMaintenance) setError(err error) error {
em.runError = err
em.hasError = true
return err
}
func (em *EnableMaintenance) ErrorHandler() {
if em.hasError {
em.writer.Println("[" + color.New(color.FgRed).Sprint("Error") + "] " + em.runError.Error())
}
}