-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathtask.go
More file actions
137 lines (117 loc) · 2.32 KB
/
Copy pathtask.go
File metadata and controls
137 lines (117 loc) · 2.32 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"fmt"
"os"
"strings"
"sync"
)
type TaskManager struct {
renderer taskRenderer
silent bool
async sync.WaitGroup
}
type Task struct {
handle taskHandle
manager *TaskManager
title string
}
func InitTaskManager(jsonOutput, unixOutput bool) *TaskManager {
return &TaskManager{
renderer: newTaskRenderer(jsonOutput, unixOutput),
silent: jsonOutput && !unixOutput,
}
}
func (tm *TaskManager) Stop() {
if tm == nil {
return
}
tm.async.Wait()
tm.stopRenderer()
}
func (tm *TaskManager) Wait() {
if tm == nil {
return
}
tm.async.Wait()
}
func (tm *TaskManager) stopRenderer() {
if tm.renderer == nil {
return
}
tm.renderer.Stop()
}
func (tm *TaskManager) Println(message string) {
if tm == nil || tm.renderer == nil {
return
}
tm.renderer.Println(message)
}
func (tm *TaskManager) BlankLine() {
if tm == nil || tm.renderer == nil {
return
}
tm.renderer.BlankLine()
}
func (tm *TaskManager) RunWithTrigger(enable bool, title string, callback func(task *Task)) {
if enable {
tm.Run(title, callback)
}
}
func (tm *TaskManager) Run(title string, callback func(task *Task)) {
task := tm.newTask(title)
callback(task)
}
func (tm *TaskManager) AsyncRun(title string, callback func(task *Task)) {
task := tm.newTask(title)
tm.async.Add(1)
go func() {
defer tm.async.Done()
callback(task)
}()
}
func (tm *TaskManager) newTask(title string) *Task {
return &Task{
handle: tm.renderer.NewTask(title),
manager: tm,
title: title,
}
}
func (t *Task) Complete() {
if t == nil || t.handle == nil {
return
}
t.handle.Complete()
}
func (t *Task) Updatef(format string, a ...interface{}) {
if t == nil || t.handle == nil {
return
}
t.handle.Update(fmt.Sprintf(format, a...))
}
func (t *Task) Update(message string) {
if t == nil || t.handle == nil {
return
}
t.handle.Update(message)
}
func (t *Task) Println(message string) {
t.Update(message)
}
func (t *Task) Printf(format string, a ...interface{}) {
t.Update(fmt.Sprintf(format, a...))
}
func (t *Task) CheckError(err error) {
if err == nil {
return
}
message := fmt.Sprintf("Fatal: %s, err: %v", strings.ToLower(t.title), err)
if t.handle != nil {
t.handle.Update(message)
t.handle.Error()
}
if t.manager.silent {
_, _ = fmt.Fprintln(os.Stderr, message)
}
t.manager.stopRenderer()
os.Exit(1)
}