Skip to content

Commit

Permalink
ctr/commands/contents: expose ShowProgress
Browse files Browse the repository at this point in the history
Expected to be used by nerdctl

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
  • Loading branch information
AkihiroSuda committed Dec 11, 2020
1 parent 7b0149a commit 419ad73
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions cmd/ctr/commands/content/fetch.go
Expand Up @@ -146,22 +146,22 @@ func NewFetchConfig(ctx context.Context, clicontext *cli.Context) (*FetchConfig,

// 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) {
ongoing := newJobs(ref)
ongoing := NewJobs(ref)

pctx, stopProgress := context.WithCancel(ctx)
progress := make(chan struct{})

go func() {
if config.ProgressOutput != nil {
// 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)
}()

h := images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
if desc.MediaType != images.MediaTypeDockerSchema1Manifest {
ongoing.add(desc)
ongoing.Add(desc)
}
return nil, nil
})
Expand Down Expand Up @@ -198,7 +198,7 @@ func Fetch(ctx context.Context, client *containerd.Client, ref string, config *F
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 (
ticker = time.NewTicker(100 * time.Millisecond)
fw = progress.NewWriter(out)
Expand All @@ -217,7 +217,7 @@ outer:
tw := tabwriter.NewWriter(fw, 1, 8, 1, ' ', 0)

resolved := "resolved"
if !ongoing.isResolved() {
if !ongoing.IsResolved() {
resolved = "resolving"
}
statuses[ongoing.name] = StatusInfo{
Expand Down Expand Up @@ -248,7 +248,7 @@ outer:
}

// 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)
keys = append(keys, key)
if _, ok := activeSeen[key]; ok {
Expand Down Expand Up @@ -315,27 +315,27 @@ 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.
//
// This is very minimal and will probably be replaced with something more
// featured.
type jobs struct {
type Jobs struct {
name string
added map[digest.Digest]struct{}
descs []ocispec.Descriptor
mu sync.Mutex
resolved bool
}

func newJobs(name string) *jobs {
return &jobs{
func NewJobs(name string) *Jobs {
return &Jobs{
name: name,
added: map[digest.Digest]struct{}{},
}
}

func (j *jobs) add(desc ocispec.Descriptor) {
func (j *Jobs) Add(desc ocispec.Descriptor) {
j.mu.Lock()
defer j.mu.Unlock()
j.resolved = true
Expand All @@ -347,15 +347,15 @@ func (j *jobs) add(desc ocispec.Descriptor) {
j.added[desc.Digest] = struct{}{}
}

func (j *jobs) jobs() []ocispec.Descriptor {
func (j *Jobs) Jobs() []ocispec.Descriptor {
j.mu.Lock()
defer j.mu.Unlock()

var descs []ocispec.Descriptor
return append(descs, j.descs...)
}

func (j *jobs) isResolved() bool {
func (j *Jobs) IsResolved() bool {
j.mu.Lock()
defer j.mu.Unlock()
return j.resolved
Expand Down

0 comments on commit 419ad73

Please sign in to comment.