-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapply.go
94 lines (74 loc) · 1.72 KB
/
apply.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
package tentez
import (
"context"
"errors"
"os"
"sync"
"time"
)
func (t tentez) pause() {
t.ui.Outputln("Pause")
t.ui.Outputln(`enter "yes", continue steps.`)
t.ui.Outputln(`If you'd like to interrupt steps, enter "quit".`)
for {
input := t.ui.Ask("> ")
if input == "yes" {
t.ui.Outputln("continue step")
break
} else if input == "quit" {
t.ui.Outputln("Bye")
os.Exit(0)
}
}
}
func (t tentez) sleep(sec int) {
seconds := time.Duration(sec) * time.Second
finishAt := time.Now().Add(seconds)
t.ui.Outputf("Sleep %ds\n", sec)
t.ui.Outputf("Resume at %s\n", finishAt.Format("2006-01-02 15:04:05"))
var wg sync.WaitGroup
ticker := time.NewTicker(1 * time.Second)
tickerStop := make(chan bool)
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case tk := <-ticker.C:
t.ui.Outputf("\rRemain: %ds ", int(finishAt.Sub(tk).Seconds()))
case <-tickerStop:
t.ui.Outputln("\a")
return
}
}
}()
wg.Add(1)
go func() {
defer wg.Done()
t.config.clock.Sleep(seconds)
ticker.Stop()
tickerStop <- true
}()
wg.Wait()
t.ui.Outputln("Resume")
}
func (t tentez) execSwitch(ctx context.Context, weight Weight, isForce bool) error {
t.ui.Outputf("Switch old:new = %d:%d\n", weight.Old, weight.New)
i := 0
for _, targetResouces := range t.Targets {
for _, target := range targetResouces.targetsSlice() {
i++
t.ui.Outputf("%d. %s ", i, target.getName())
if err := target.execSwitch(ctx, weight, isForce, t.config); err != nil {
if !errors.As(err, &SkipSwitchError{}) {
return err
}
t.ui.Outputln(err.Error())
} else {
t.ui.Outputln("switched!")
}
}
}
t.ui.Outputf("Switched at %s\n", time.Now().Format("2006-01-02 15:04:05"))
return nil
}