github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/pkg/progress/progress.go (about)

     1  package progress // import "github.com/docker/docker/pkg/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  	// If true, don't show xB/yB
    20  	HideCounts bool
    21  	// If not empty, use units instead of bytes for counts
    22  	Units string
    23  
    24  	// Aux contains extra information not presented to the user, such as
    25  	// digests for push signing.
    26  	Aux interface{}
    27  
    28  	LastUpdate bool
    29  }
    30  
    31  // Output is an interface for writing progress information. It's
    32  // like a writer for progress, but we don't call it Writer because
    33  // that would be confusing next to ProgressReader (also, because it
    34  // doesn't implement the io.Writer interface).
    35  type Output interface {
    36  	WriteProgress(Progress) error
    37  }
    38  
    39  type chanOutput chan<- Progress
    40  
    41  func (out chanOutput) WriteProgress(p Progress) error {
    42  	// FIXME: workaround for panic in #37735
    43  	defer func() {
    44  		recover()
    45  	}()
    46  	out <- p
    47  	return nil
    48  }
    49  
    50  // ChanOutput returns an Output that writes progress updates to the
    51  // supplied channel.
    52  func ChanOutput(progressChan chan<- Progress) Output {
    53  	return chanOutput(progressChan)
    54  }
    55  
    56  type discardOutput struct{}
    57  
    58  func (discardOutput) WriteProgress(Progress) error {
    59  	return nil
    60  }
    61  
    62  // DiscardOutput returns an Output that discards progress
    63  func DiscardOutput() Output {
    64  	return discardOutput{}
    65  }
    66  
    67  // Update is a convenience function to write a progress update to the channel.
    68  func Update(out Output, id, action string) {
    69  	out.WriteProgress(Progress{ID: id, Action: action})
    70  }
    71  
    72  // Updatef is a convenience function to write a printf-formatted progress update
    73  // to the channel.
    74  func Updatef(out Output, id, format string, a ...interface{}) {
    75  	Update(out, id, fmt.Sprintf(format, a...))
    76  }
    77  
    78  // Message is a convenience function to write a progress message to the channel.
    79  func Message(out Output, id, message string) {
    80  	out.WriteProgress(Progress{ID: id, Message: message})
    81  }
    82  
    83  // Messagef is a convenience function to write a printf-formatted progress
    84  // message to the channel.
    85  func Messagef(out Output, id, format string, a ...interface{}) {
    86  	Message(out, id, fmt.Sprintf(format, a...))
    87  }
    88  
    89  // Aux sends auxiliary information over a progress interface, which will not be
    90  // formatted for the UI. This is used for things such as push signing.
    91  func Aux(out Output, a interface{}) {
    92  	out.WriteProgress(Progress{Aux: a})
    93  }