ProgressHandler returns a progress callback and a cleanup function to render transfer progress. This implementation is based on containerd's ctr command progress handler.
(ctx context.Context, out io.Writer)
| 55 | // ProgressHandler returns a progress callback and a cleanup function to render transfer progress. |
| 56 | // This implementation is based on containerd's ctr command progress handler. |
| 57 | func ProgressHandler(ctx context.Context, out io.Writer) (transfer.ProgressFunc, func()) { |
| 58 | ctx, cancel := context.WithCancel(ctx) |
| 59 | var ( |
| 60 | fw = progress.NewWriter(out) |
| 61 | start = time.Now() |
| 62 | statuses = map[string]*progressNode{} |
| 63 | roots = []*progressNode{} |
| 64 | pc = make(chan transfer.Progress, 5) |
| 65 | status string |
| 66 | closeC = make(chan struct{}) |
| 67 | ) |
| 68 | |
| 69 | progressFn := func(p transfer.Progress) { |
| 70 | select { |
| 71 | case pc <- p: |
| 72 | case <-ctx.Done(): |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | done := func() { |
| 77 | cancel() |
| 78 | <-closeC |
| 79 | } |
| 80 | |
| 81 | go func() { |
| 82 | defer close(closeC) |
| 83 | for { |
| 84 | select { |
| 85 | case p := <-pc: |
| 86 | if p.Name == "" { |
| 87 | status = p.Event |
| 88 | continue |
| 89 | } |
| 90 | if node, ok := statuses[p.Name]; !ok { |
| 91 | node = &progressNode{ |
| 92 | Progress: p, |
| 93 | root: true, |
| 94 | } |
| 95 | if len(p.Parents) == 0 { |
| 96 | roots = append(roots, node) |
| 97 | } else { |
| 98 | var parents []string |
| 99 | for _, parent := range p.Parents { |
| 100 | pStatus, ok := statuses[parent] |
| 101 | if ok { |
| 102 | parents = append(parents, parent) |
| 103 | pStatus.children = append(pStatus.children, node) |
| 104 | node.root = false |
| 105 | } |
| 106 | } |
| 107 | node.Progress.Parents = parents |
| 108 | if node.root { |
| 109 | roots = append(roots, node) |
| 110 | } |
| 111 | } |
| 112 | statuses[p.Name] = node |
| 113 | } else { |
| 114 | if len(node.Progress.Parents) != len(p.Parents) { |
no test coverage detected
searching dependent graphs…