github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/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  	LastUpdate bool
    20  }
    21  
    22  // Output is an interface for writing progress information. It's
    23  // like a writer for progress, but we don't call it Writer because
    24  // that would be confusing next to ProgressReader (also, because it
    25  // doesn't implement the io.Writer interface).
    26  type Output interface {
    27  	WriteProgress(Progress) error
    28  }
    29  
    30  type chanOutput chan<- Progress
    31  
    32  func (out chanOutput) WriteProgress(p Progress) error {
    33  	out <- p
    34  	return nil
    35  }
    36  
    37  // ChanOutput returns a Output that writes progress updates to the
    38  // supplied channel.
    39  func ChanOutput(progressChan chan<- Progress) Output {
    40  	return chanOutput(progressChan)
    41  }
    42  
    43  // Update is a convenience function to write a progress update to the channel.
    44  func Update(out Output, id, action string) {
    45  	out.WriteProgress(Progress{ID: id, Action: action})
    46  }
    47  
    48  // Updatef is a convenience function to write a printf-formatted progress update
    49  // to the channel.
    50  func Updatef(out Output, id, format string, a ...interface{}) {
    51  	Update(out, id, fmt.Sprintf(format, a...))
    52  }
    53  
    54  // Message is a convenience function to write a progress message to the channel.
    55  func Message(out Output, id, message string) {
    56  	out.WriteProgress(Progress{ID: id, Message: message})
    57  }
    58  
    59  // Messagef is a convenience function to write a printf-formatted progress
    60  // message to the channel.
    61  func Messagef(out Output, id, format string, a ...interface{}) {
    62  	Message(out, id, fmt.Sprintf(format, a...))
    63  }