Move content.Fetch configuration to struct

This makes it easier for callers to call this function and populate the
config without relying on specific flags across commands.

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2018-08-09 10:16:06 -04:00
parent b9eeaa1ce8
commit 3be457d7d6
2 changed files with 52 additions and 23 deletions

View File

@ -76,28 +76,61 @@ Most of this is experimental and there are few leaps to make this work.`,
return err return err
} }
defer cancel() defer cancel()
config, err := NewFetchConfig(ctx, clicontext)
_, err = Fetch(ctx, client, ref, clicontext) if err != nil {
return err
}
_, err = Fetch(ctx, client, ref, config)
return err return err
}, },
} }
// Fetch loads all resources into the content store and returns the image // FetchConfig for content fetch
func Fetch(ctx context.Context, client *containerd.Client, ref string, cliContext *cli.Context) (images.Image, error) { type FetchConfig struct {
resolver, err := commands.GetResolver(ctx, cliContext) // Resolver
if err != nil { Resolver remotes.Resolver
return images.Image{}, err // ProgressOutput to display progress
} ProgressOutput io.Writer
// Labels to set on the content
Labels []string
// Platforms to fetch
Platforms []string
}
// NewFetchConfig returns the default FetchConfig from cli flags
func NewFetchConfig(ctx context.Context, clicontext *cli.Context) (*FetchConfig, error) {
resolver, err := commands.GetResolver(ctx, clicontext)
if err != nil {
return nil, err
}
config := &FetchConfig{
Resolver: resolver,
Labels: clicontext.StringSlice("label"),
}
if !clicontext.GlobalBool("debug") {
config.ProgressOutput = os.Stdout
}
if !clicontext.Bool("all-platforms") {
p := clicontext.StringSlice("platform")
if len(p) == 0 {
p = append(p, platforms.Default())
}
config.Platforms = p
}
return config, nil
}
// 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) pctx, stopProgress := context.WithCancel(ctx)
progress := make(chan struct{}) progress := make(chan struct{})
go func() { go func() {
if !cliContext.GlobalBool("debug") { 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(), os.Stdout) showProgress(pctx, ongoing, client.ContentStore(), config.ProgressOutput)
} }
close(progress) close(progress)
}() }()
@ -110,24 +143,16 @@ func Fetch(ctx context.Context, client *containerd.Client, ref string, cliContex
}) })
log.G(pctx).WithField("image", ref).Debug("fetching") log.G(pctx).WithField("image", ref).Debug("fetching")
labels := commands.LabelArgs(cliContext.StringSlice("label")) labels := commands.LabelArgs(config.Labels)
opts := []containerd.RemoteOpt{ opts := []containerd.RemoteOpt{
containerd.WithPullLabels(labels), containerd.WithPullLabels(labels),
containerd.WithResolver(resolver), containerd.WithResolver(config.Resolver),
containerd.WithImageHandler(h), containerd.WithImageHandler(h),
containerd.WithSchema1Conversion, containerd.WithSchema1Conversion,
} }
for _, platform := range config.Platforms {
if !cliContext.Bool("all-platforms") { opts = append(opts, containerd.WithPlatform(platform))
p := cliContext.StringSlice("platform")
if len(p) == 0 {
p = append(p, platforms.Default())
}
for _, platform := range p {
opts = append(opts, containerd.WithPlatform(platform))
}
} }
img, err := client.Fetch(pctx, ref, opts...) img, err := client.Fetch(pctx, ref, opts...)
stopProgress() stopProgress()
if err != nil { if err != nil {

View File

@ -73,7 +73,11 @@ command. As part of this process, we do the following:
} }
defer done(ctx) defer done(ctx)
img, err := content.Fetch(ctx, client, ref, context) config, err := content.NewFetchConfig(ctx, context)
if err != nil {
return err
}
img, err := content.Fetch(ctx, client, ref, config)
if err != nil { if err != nil {
return err return err
} }