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
|
package task
import (
"bytes"
"fmt"
"sync"
"github.com/aptly-dev/aptly/aptly"
)
// Output represents a safe standard output of task
// which is compatbile to AptlyProgress.
type Output struct {
mu *sync.Mutex
output *bytes.Buffer
}
// PublishOutput specific output for publishing api
type PublishOutput struct {
aptly.Progress
PublishDetail
barType *aptly.BarType
}
// NewOutput creates new output
func NewOutput() *Output {
return &Output{mu: &sync.Mutex{}, output: &bytes.Buffer{}}
}
func (t *Output) String() string {
t.mu.Lock()
defer t.mu.Unlock()
return t.output.String()
}
// Write is used to determine how many bytes have been written
// not needed in our case.
func (t *Output) Write(p []byte) (n int, err error) {
return len(p), err
}
// WriteString writes string to output
func (t *Output) WriteString(s string) (n int, err error) {
t.mu.Lock()
defer t.mu.Unlock()
return t.output.WriteString(s)
}
// Start is needed for progress compatibility
func (t *Output) Start() {
// Not implemented
}
// Shutdown is needed for progress compatibility
func (t *Output) Shutdown() {
// Not implemented
}
// Flush is needed for progress compatibility
func (t *Output) Flush() {
// Not implemented
}
// InitBar is needed for progress compatibility
func (t *Output) InitBar(_ int64, _ bool, _ aptly.BarType) {
// Not implemented
}
// InitBar publish output specific
func (t *PublishOutput) InitBar(count int64, _ bool, barType aptly.BarType) {
t.barType = &barType
if barType == aptly.BarPublishGeneratePackageFiles {
t.TotalNumberOfPackages = count
t.RemainingNumberOfPackages = count
t.Store(t)
}
}
// ShutdownBar is needed for progress compatibility
func (t *Output) ShutdownBar() {
// Not implemented
}
// ShutdownBar publish output specific
func (t *PublishOutput) ShutdownBar() {
t.barType = nil
}
// AddBar is needed for progress compatibility
func (t *Output) AddBar(_ int) {
// Not implemented
}
// AddBar publish output specific
func (t *PublishOutput) AddBar(_ int) {
if t.barType != nil && *t.barType == aptly.BarPublishGeneratePackageFiles {
t.RemainingNumberOfPackages--
t.Store(t)
}
}
// SetBar sets current position for progress bar
func (t *Output) SetBar(_ int) {
// Not implemented
}
// Printf does printf in a safe manner
func (t *Output) Printf(msg string, a ...interface{}) {
_, _ = t.WriteString(fmt.Sprintf(msg, a...))
}
// Print does printf in a safe manner
func (t *Output) Print(msg string) {
_, _ = t.WriteString(msg)
}
// ColoredPrintf does printf in a safe manner + newline
// currently are no colors supported.
func (t *Output) ColoredPrintf(msg string, a ...interface{}) {
_, _ = t.WriteString(fmt.Sprintf(msg+"\n", a...))
}
// PrintfStdErr does printf but in safe manner to output
func (t *Output) PrintfStdErr(msg string, a ...interface{}) {
_, _ = t.WriteString(fmt.Sprintf(msg, a...))
}
|