github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/pkg/progress/progress.go (about)

     1  package progress
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // Progress represents the progress of a transfer.
     8  type Progress struct {
     9  	ID string
    10  
    11  	// Progress contains a Message or...
    12  	Message string
    13  
    14  	// ...progress of an action
    15  	Action  string
    16  	Current int64
    17  	Total   int64
    18  
    19  	// Aux contains extra information not presented to the user, such as
    20  	// digests for push signing.
    21  	Aux interface{}
    22  
    23  	LastUpdate bool
    24  }
    25  
    26  // Output is an interface for writing progress information. It's
    27  // like a writer for progress, but we don't call it Writer because
    28  // that would be confusing next to ProgressReader (also, because it
    29  // doesn't implement the io.Writer interface).
    30  type Output interface {
    31  	WriteProgress(Progress) error
    32  }
    33  
    34  type chanOutput chan<- Progress
    35  
    36  func (out chanOutput) WriteProgress(p Progress) error {
    37  	out <- p
    38  	return nil
    39  }
    40  
    41  // ChanOutput returns a Output that writes progress updates to the
    42  // supplied channel.
    43  func ChanOutput(progressChan chan<- Progress) Output {
    44  	return chanOutput(progressChan)
    45  }
    46  
    47  // Update is a convenience function to write a progress update to the channel.
    48  func Update(out Output, id, action string) {
    49  	out.WriteProgress(Progress{ID: id, Action: action})
    50  }
    51  
    52  // Updatef is a convenience function to write a printf-formatted progress update
    53  // to the channel.
    54  func Updatef(out Output, id, format string, a ...interface{}) {
    55  	Update(out, id, fmt.Sprintf(format, a...))
    56  }
    57  
    58  // Message is a convenience function to write a progress message to the channel.
    59  func Message(out Output, id, message string) {
    60  	out.WriteProgress(Progress{ID: id, Message: message})
    61  }
    62  
    63  // Messagef is a convenience function to write a printf-formatted progress
    64  // message to the channel.
    65  func Messagef(out Output, id, format string, a ...interface{}) {
    66  	Message(out, id, fmt.Sprintf(format, a...))
    67  }
    68  
    69  // Aux sends auxiliary information over a progress interface, which will not be
    70  // formatted for the UI. This is used for things such as push signing.
    71  func Aux(out Output, a interface{}) {
    72  	out.WriteProgress(Progress{Aux: a})
    73  }