2017-11-22 22:07:24 +00:00
|
|
|
package tasklog
|
2017-06-16 21:06:32 +00:00
|
|
|
|
2017-08-09 21:45:18 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
2017-06-16 21:06:32 +00:00
|
|
|
|
|
|
|
// ListTask is a Task implementation that logs all updates in a list where each
|
|
|
|
// entry is line-delimited.
|
|
|
|
//
|
|
|
|
// For example:
|
|
|
|
// entry #1
|
|
|
|
// entry #2
|
|
|
|
// msg: ..., done
|
|
|
|
type ListTask struct {
|
|
|
|
msg string
|
2017-08-09 21:45:18 +00:00
|
|
|
ch chan *Update
|
2017-06-16 21:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewListTask instantiates a new *ListTask instance with the given message.
|
|
|
|
func NewListTask(msg string) *ListTask {
|
|
|
|
return &ListTask{
|
|
|
|
msg: msg,
|
2017-08-09 21:45:18 +00:00
|
|
|
ch: make(chan *Update, 1),
|
2017-06-16 21:06:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Entry logs a line-delimited task entry.
|
|
|
|
func (l *ListTask) Entry(update string) {
|
2017-08-09 21:45:18 +00:00
|
|
|
l.ch <- &Update{
|
|
|
|
S: fmt.Sprintf("%s\n", update),
|
|
|
|
At: time.Now(),
|
|
|
|
}
|
2017-06-16 21:06:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *ListTask) Complete() {
|
2017-08-09 21:45:18 +00:00
|
|
|
l.ch <- &Update{
|
|
|
|
S: fmt.Sprintf("%s: ...", l.msg),
|
|
|
|
At: time.Now(),
|
|
|
|
}
|
2017-06-16 21:06:32 +00:00
|
|
|
close(l.ch)
|
|
|
|
}
|
|
|
|
|
2017-06-19 20:20:35 +00:00
|
|
|
// Throttled implements the Task.Throttled function and ensures that all log
|
|
|
|
// updates are printed to the sink.
|
|
|
|
func (l *ListTask) Throttled() bool { return false }
|
2017-06-16 21:06:32 +00:00
|
|
|
|
|
|
|
// Updates implements the Task.Updates function and returns a channel of updates
|
|
|
|
// to log to the sink.
|
2017-08-09 21:45:18 +00:00
|
|
|
func (l *ListTask) Updates() <-chan *Update { return l.ch }
|