forked from FeLvi-zzz/tentez
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapply.go
96 lines (76 loc) · 1.91 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
95
96
package tentez
import (
"errors"
"fmt"
"os"
"sync"
"time"
)
func pause(cfg Config) {
fmt.Fprintln(cfg.io.out, "Pause")
fmt.Fprintln(cfg.io.out, `enter "yes", continue steps.`)
fmt.Fprintln(cfg.io.out, `If you'd like to interrupt steps, enter "quit".`)
for {
input := ""
fmt.Fprint(cfg.io.out, "> ")
fmt.Fscan(cfg.io.in, &input)
if input == "yes" {
fmt.Fprintln(cfg.io.out, "continue step")
break
} else if input == "quit" {
fmt.Fprintln(cfg.io.out, "Bye")
os.Exit(0)
}
}
}
func sleep(sec int, cfg Config) {
seconds := time.Duration(sec) * time.Second
finishAt := time.Now().Add(seconds)
fmt.Fprintf(cfg.io.out, "Sleep %ds\n", sec)
fmt.Fprintf(cfg.io.out, "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 t := <-ticker.C:
fmt.Fprintf(cfg.io.out, "\rRemain: %ds ", int(finishAt.Sub(t).Seconds()))
case <-tickerStop:
fmt.Fprintln(cfg.io.out, "\a")
return
}
}
}()
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(seconds)
ticker.Stop()
tickerStop <- true
}()
wg.Wait()
fmt.Fprintln(cfg.io.out, "Resume")
}
func execSwitch(targets map[TargetType]Targets, weight Weight, isForce bool, cfg Config) error {
fmt.Fprintf(cfg.io.out, "Switch old:new = %d:%d\n", weight.Old, weight.New)
i := 0
for _, targetResouces := range targets {
for _, target := range targetResouces.targetsSlice() {
i++
fmt.Fprintf(cfg.io.out, "%d. %s ", i, target.getName())
if err := target.execSwitch(weight, isForce, cfg); err != nil {
if !errors.As(err, &SkipSwitchError{}) {
return err
}
fmt.Fprintln(cfg.io.out, err.Error())
} else {
fmt.Fprintln(cfg.io.out, "switched!")
}
}
}
fmt.Fprintf(cfg.io.out, "Switched at %s\n", time.Now().Format("2006-01-02 15:04:05"))
return nil
}