Merge pull request #4831 from AkihiroSuda/expose-contents-showprogress

ctr/commands/contents: expose ShowProgress
This commit is contained in:
Maksym Pavlenko 2020-12-11 11:04:49 -08:00 committed by GitHub
commit 004214808a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -146,7 +146,7 @@ func NewFetchConfig(ctx context.Context, clicontext *cli.Context) (*FetchConfig,
// Fetch loads all resources into the content store and returns the image // Fetch loads all resources into the content store and returns the image
func Fetch(ctx context.Context, client *containerd.Client, ref string, config *FetchConfig) (images.Image, error) { func Fetch(ctx context.Context, client *containerd.Client, ref string, config *FetchConfig) (images.Image, error) {
ongoing := newJobs(ref) ongoing := NewJobs(ref)
pctx, stopProgress := context.WithCancel(ctx) pctx, stopProgress := context.WithCancel(ctx)
progress := make(chan struct{}) progress := make(chan struct{})
@ -154,14 +154,14 @@ func Fetch(ctx context.Context, client *containerd.Client, ref string, config *F
go func() { go func() {
if config.ProgressOutput != nil { if config.ProgressOutput != nil {
// no progress bar, because it hides some debug logs // no progress bar, because it hides some debug logs
showProgress(pctx, ongoing, client.ContentStore(), config.ProgressOutput) ShowProgress(pctx, ongoing, client.ContentStore(), config.ProgressOutput)
} }
close(progress) close(progress)
}() }()
h := images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) { h := images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
if desc.MediaType != images.MediaTypeDockerSchema1Manifest { if desc.MediaType != images.MediaTypeDockerSchema1Manifest {
ongoing.add(desc) ongoing.Add(desc)
} }
return nil, nil return nil, nil
}) })
@ -198,7 +198,7 @@ func Fetch(ctx context.Context, client *containerd.Client, ref string, config *F
return img, nil return img, nil
} }
func showProgress(ctx context.Context, ongoing *jobs, cs content.Store, out io.Writer) { func ShowProgress(ctx context.Context, ongoing *Jobs, cs content.Store, out io.Writer) {
var ( var (
ticker = time.NewTicker(100 * time.Millisecond) ticker = time.NewTicker(100 * time.Millisecond)
fw = progress.NewWriter(out) fw = progress.NewWriter(out)
@ -217,7 +217,7 @@ outer:
tw := tabwriter.NewWriter(fw, 1, 8, 1, ' ', 0) tw := tabwriter.NewWriter(fw, 1, 8, 1, ' ', 0)
resolved := "resolved" resolved := "resolved"
if !ongoing.isResolved() { if !ongoing.IsResolved() {
resolved = "resolving" resolved = "resolving"
} }
statuses[ongoing.name] = StatusInfo{ statuses[ongoing.name] = StatusInfo{
@ -248,7 +248,7 @@ outer:
} }
// now, update the items in jobs that are not in active // now, update the items in jobs that are not in active
for _, j := range ongoing.jobs() { for _, j := range ongoing.Jobs() {
key := remotes.MakeRefKey(ctx, j) key := remotes.MakeRefKey(ctx, j)
keys = append(keys, key) keys = append(keys, key)
if _, ok := activeSeen[key]; ok { if _, ok := activeSeen[key]; ok {
@ -315,12 +315,12 @@ outer:
} }
} }
// jobs provides a way of identifying the download keys for a particular task // Jobs provides a way of identifying the download keys for a particular task
// encountering during the pull walk. // encountering during the pull walk.
// //
// This is very minimal and will probably be replaced with something more // This is very minimal and will probably be replaced with something more
// featured. // featured.
type jobs struct { type Jobs struct {
name string name string
added map[digest.Digest]struct{} added map[digest.Digest]struct{}
descs []ocispec.Descriptor descs []ocispec.Descriptor
@ -328,14 +328,14 @@ type jobs struct {
resolved bool resolved bool
} }
func newJobs(name string) *jobs { func NewJobs(name string) *Jobs {
return &jobs{ return &Jobs{
name: name, name: name,
added: map[digest.Digest]struct{}{}, added: map[digest.Digest]struct{}{},
} }
} }
func (j *jobs) add(desc ocispec.Descriptor) { func (j *Jobs) Add(desc ocispec.Descriptor) {
j.mu.Lock() j.mu.Lock()
defer j.mu.Unlock() defer j.mu.Unlock()
j.resolved = true j.resolved = true
@ -347,7 +347,7 @@ func (j *jobs) add(desc ocispec.Descriptor) {
j.added[desc.Digest] = struct{}{} j.added[desc.Digest] = struct{}{}
} }
func (j *jobs) jobs() []ocispec.Descriptor { func (j *Jobs) Jobs() []ocispec.Descriptor {
j.mu.Lock() j.mu.Lock()
defer j.mu.Unlock() defer j.mu.Unlock()
@ -355,7 +355,7 @@ func (j *jobs) jobs() []ocispec.Descriptor {
return append(descs, j.descs...) return append(descs, j.descs...)
} }
func (j *jobs) isResolved() bool { func (j *Jobs) IsResolved() bool {
j.mu.Lock() j.mu.Lock()
defer j.mu.Unlock() defer j.mu.Unlock()
return j.resolved return j.resolved