Repository navigation
Expand file tree
/
Copy pathtty_progress_sim.go
More file actions
94 lines (87 loc) · 2.35 KB
/
Copy pathtty_progress_sim.go
File metadata and controls
94 lines (87 loc) · 2.35 KB
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
//go:build simulate
package main
import (
"fmt"
"io"
"strings"
"sync"
"time"
progress "github.com/san-gg/mdeploy/pkg/progress"
ssh "github.com/san-gg/mdeploy/pkg/ssh"
"github.com/spf13/cobra"
)
func eventOutputWriter(lines int, sleep int, w io.Writer) {
for i := range lines {
fmt.Fprintf(w, "Line %d", i)
time.Sleep(time.Duration(sleep) * time.Second)
}
}
func progressBarWriter(lines int, sleep int, w io.Writer) {
for i := range lines {
fmt.Fprint(w, fmt.Sprintf("[%s>%s]", strings.Repeat("=", i), strings.Repeat(" ", lines-i-1)))
time.Sleep(time.Duration(sleep) * time.Second)
}
}
func task(wg *sync.WaitGroup, event progress.ProgressEvent, i int, e progress.Event, cmd *cobra.Command) {
defer wg.Done()
e.Message = "EventOutputTest"
event.SetStatus(&e)
eo := progress.NewEventOutputWriter(cmd)
////////////////////////////////////////////////////////////////////////////
event.SetEventOutput(&e, eo)
eventOutputWriter(i+10, min(i+2, 4), eo)
eo.Wait()
event.UnSetEventOutput(&e)
////////////////////////////////////////////////////////////////////////////
eo = progress.NewEventProgressBarWriter(cmd)
event.SetEventOutput(&e, eo)
progressBarWriter(i+10, min(i+2, 4), eo)
eo.Wait()
event.UnSetEventOutput(&e)
////////////////////////////////////////////////////////////////////////////
e.Status = progress.COMPLETED
event.SetStatus(&e)
}
func simdeploy(cmd *cobra.Command) error {
wg := &sync.WaitGroup{}
event := progress.NewEventProgress(cmd)
event.StartEvent()
for i := 0; i < 5; i++ {
e := progress.Event{
Id: uint32(i),
EventName: fmt.Sprintf("Test %d", i),
Status: progress.RUNNING,
}
wg.Add(1)
go task(wg, event, i, e, cmd)
}
wg.Wait()
event.StopEvent()
return nil
}
func simcopy(cmd *cobra.Command) {
prog := progress.NewProgressBar(cmd)
ssh.CopyProgressBar(prog)
prog.Completed()
}
func simulate(cmd *cobra.Command, args []string) error {
if args[0] == "deploy" {
return simdeploy(cmd)
} else if args[0] == "copy" {
simcopy(cmd)
return nil
}
return fmt.Errorf("unknown command: %s", args[0])
}
func main() {
var rootCmd cobra.Command = cobra.Command{
Use: "simulate",
Short: "simulate tty progress output",
Args: cobra.ExactArgs(1),
RunE: simulate,
}
rootCmd.PersistentFlags().Bool("plain", false, "print plain output")
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
}
}