github.com/ph/moby@v1.13.1/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 an Output that writes progress updates to the
    42  // supplied channel.
    43  func ChanOutput(progressChan chan<- Progress) Output {
    44  	return chanOutput(progressChan)
    45  }
    46  
    47  type discardOutput struct{}
    48  
    49  func (discardOutput) WriteProgress(Progress) error {
    50  	return nil
    51  }
    52  
    53  // DiscardOutput returns an Output that discards progress
    54  func DiscardOutput() Output {
    55  	return discardOutput{}
    56  }
    57  
    58  // Update is a convenience function to write a progress update to the channel.
    59  func Update(out Output, id, action string) {
    60  	out.WriteProgress(Progress{ID: id, Action: action})
    61  }
    62  
    63  // Updatef is a convenience function to write a printf-formatted progress update
    64  // to the channel.
    65  func Updatef(out Output, id, format string, a ...interface{}) {
    66  	Update(out, id, fmt.Sprintf(format, a...))
    67  }
    68  
    69  // Message is a convenience function to write a progress message to the channel.
    70  func Message(out Output, id, message string) {
    71  	out.WriteProgress(Progress{ID: id, Message: message})
    72  }
    73  
    74  // Messagef is a convenience function to write a printf-formatted progress
    75  // message to the channel.
    76  func Messagef(out Output, id, format string, a ...interface{}) {
    77  	Message(out, id, fmt.Sprintf(format, a...))
    78  }
    79  
    80  // Aux sends auxiliary information over a progress interface, which will not be
    81  // formatted for the UI. This is used for things such as push signing.
    82  func Aux(out Output, a interface{}) {
    83  	out.WriteProgress(Progress{Aux: a})
    84  }