github.com/docker/cnab-to-oci@v0.3.0-beta4/remotes/fixupevent.go (about)

     1  package remotes
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/docker/distribution/reference"
     7  	ocischemav1 "github.com/opencontainers/image-spec/specs-go/v1"
     8  )
     9  
    10  // FixupEvent is an event that is raised by the Fixup Logic
    11  type FixupEvent struct {
    12  	SourceImage    string
    13  	DestinationRef reference.Named
    14  	EventType      FixupEventType
    15  	Message        string
    16  	Error          error
    17  	Progress       ProgressSnapshot
    18  }
    19  
    20  // FixupEventType is the the type of event raised by the Fixup logic
    21  type FixupEventType string
    22  
    23  const (
    24  	// FixupEventTypeCopyImageStart is raised when the Fixup logic starts copying an
    25  	// image
    26  	FixupEventTypeCopyImageStart = FixupEventType("CopyImageStart")
    27  
    28  	// FixupEventTypeCopyImageEnd is raised when the Fixup logic stops copying an
    29  	// image. Error might be populated
    30  	FixupEventTypeCopyImageEnd = FixupEventType("CopyImageEnd")
    31  
    32  	// FixupEventTypeProgress is raised when Fixup logic reports progression
    33  	FixupEventTypeProgress = FixupEventType("Progress")
    34  )
    35  
    36  type descriptorProgress struct {
    37  	ocischemav1.Descriptor
    38  	done     bool
    39  	action   string
    40  	err      error
    41  	children []*descriptorProgress
    42  	mut      sync.RWMutex
    43  }
    44  
    45  func (p *descriptorProgress) markDone() {
    46  	p.mut.Lock()
    47  	defer p.mut.Unlock()
    48  	p.done = true
    49  }
    50  
    51  func (p *descriptorProgress) setAction(a string) {
    52  	p.mut.Lock()
    53  	defer p.mut.Unlock()
    54  	p.action = a
    55  }
    56  
    57  func (p *descriptorProgress) setError(err error) {
    58  	p.mut.Lock()
    59  	defer p.mut.Unlock()
    60  	p.err = err
    61  }
    62  
    63  func (p *descriptorProgress) addChild(child *descriptorProgress) {
    64  	p.mut.Lock()
    65  	defer p.mut.Unlock()
    66  	p.children = append(p.children, child)
    67  }
    68  
    69  func (p *descriptorProgress) snapshot() DescriptorProgressSnapshot {
    70  	p.mut.RLock()
    71  	defer p.mut.RUnlock()
    72  	result := DescriptorProgressSnapshot{
    73  		Descriptor: p.Descriptor,
    74  		Done:       p.done,
    75  		Action:     p.action,
    76  		Error:      p.err,
    77  	}
    78  	if len(p.children) != 0 {
    79  		result.Children = make([]DescriptorProgressSnapshot, len(p.children))
    80  		for ix, child := range p.children {
    81  			result.Children[ix] = child.snapshot()
    82  		}
    83  	}
    84  	return result
    85  }
    86  
    87  type progress struct {
    88  	roots []*descriptorProgress
    89  	mut   sync.RWMutex
    90  }
    91  
    92  func (p *progress) addRoot(root *descriptorProgress) {
    93  	p.mut.Lock()
    94  	defer p.mut.Unlock()
    95  	p.roots = append(p.roots, root)
    96  }
    97  
    98  func (p *progress) snapshot() ProgressSnapshot {
    99  	p.mut.RLock()
   100  	defer p.mut.RUnlock()
   101  	result := ProgressSnapshot{}
   102  	if len(p.roots) != 0 {
   103  		result.Roots = make([]DescriptorProgressSnapshot, len(p.roots))
   104  		for ix, root := range p.roots {
   105  			result.Roots[ix] = root.snapshot()
   106  		}
   107  	}
   108  	return result
   109  }
   110  
   111  // DescriptorProgressSnapshot describes the current progress of a descriptor
   112  type DescriptorProgressSnapshot struct {
   113  	ocischemav1.Descriptor
   114  	Done     bool
   115  	Action   string
   116  	Error    error
   117  	Children []DescriptorProgressSnapshot
   118  }
   119  
   120  // ProgressSnapshot describes the current progress of a Fixup operation
   121  type ProgressSnapshot struct {
   122  	Roots []DescriptorProgressSnapshot
   123  }