Skip to content

Commit c5b8f69

Browse files
committed
Add TotalProgress field to State
1 parent 2681a83 commit c5b8f69

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

task.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,12 @@ type Task struct {
2020

2121
// State contains information about rsync process
2222
type State struct {
23-
Remain int `json:"remain"`
24-
Total int `json:"total"`
25-
Speed string `json:"speed"`
26-
Progress float64 `json:"progress"`
27-
Filename string `json:"filename"`
23+
TotalProgress int `json:"total_progress"`
24+
Remain int `json:"remain"`
25+
Total int `json:"total"`
26+
Speed string `json:"speed"`
27+
Progress float64 `json:"progress"`
28+
Filename string `json:"filename"`
2829
}
2930

3031
// Log contains raw stderr and stdout outputs
@@ -121,6 +122,7 @@ func processStdout(wg *sync.WaitGroup, task *Task, stdout io.Reader) {
121122

122123
progressMatcher := newMatcher(`\(.+-chk=(\d+.\d+)`)
123124
speedMatcher := newMatcher(`(\d+\.\d+.{2}\/s)`)
125+
totalProgressMatcher := newMatcher(`(\d+)%`)
124126

125127
// Extract data from strings:
126128
// 999,999 99% 999.99kB/s 0:00:59 (xfr#9, to-chk=999/9999)
@@ -139,6 +141,9 @@ func processStdout(wg *sync.WaitGroup, task *Task, stdout io.Reader) {
139141
task.state.Speed = getTaskSpeed(speedMatcher.ExtractAllStringSubmatch(logStr, 2))
140142
}
141143

144+
if totalProgressMatcher.Match(logStr) {
145+
task.state.TotalProgress = getTaskTotalProgress(totalProgressMatcher.Extract(logStr))
146+
}
142147
if isFilename(logStr) {
143148
task.state.Filename = logStr
144149
}
@@ -157,6 +162,11 @@ func processStderr(wg *sync.WaitGroup, task *Task, stderr io.Reader) {
157162
}
158163
}
159164

165+
func getTaskTotalProgress(totalProgress string) int {
166+
total, _ := strconv.Atoi(totalProgress)
167+
return total
168+
}
169+
160170
func getTaskProgress(remTotalString string) (int, int) {
161171
const remTotalSeparator = "/"
162172
const numbersCount = 2

task_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,11 @@ func TestTaskSpeedParse(t *testing.T) {
3939
speed := getTaskSpeed(speedMatcher.ExtractAllStringSubmatch(taskInfoString, 2))
4040
assert.Equal(t, "999.99kB/s", speed)
4141
}
42+
43+
func TestTaskTotalProgress(t *testing.T) {
44+
totalProgressMatcher := newMatcher(`(\d+)%`)
45+
const taskInfoString = `123,456 78% 87.65kB/s 0:00:59 (xfr#9, to-chk=999/9999)`
46+
total := getTaskTotalProgress(totalProgressMatcher.Extract(taskInfoString))
47+
48+
assert.Equal(t, total, 78)
49+
}

0 commit comments

Comments
 (0)