Skip to content

Commit 3be457d

Browse files
committed
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 <[email protected]>
1 parent b9eeaa1 commit 3be457d

2 files changed

Lines changed: 50 additions & 21 deletions

File tree

cmd/ctr/commands/content/fetch.go

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -76,28 +76,61 @@ Most of this is experimental and there are few leaps to make this work.`,
7676
return err
7777
}
7878
defer cancel()
79-
80-
_, err = Fetch(ctx, client, ref, clicontext)
79+
config, err := NewFetchConfig(ctx, clicontext)
80+
if err != nil {
81+
return err
82+
}
83+
_, err = Fetch(ctx, client, ref, config)
8184
return err
8285
},
8386
}
8487

85-
// Fetch loads all resources into the content store and returns the image
86-
func Fetch(ctx context.Context, client *containerd.Client, ref string, cliContext *cli.Context) (images.Image, error) {
87-
resolver, err := commands.GetResolver(ctx, cliContext)
88+
// FetchConfig for content fetch
89+
type FetchConfig struct {
90+
// Resolver
91+
Resolver remotes.Resolver
92+
// ProgressOutput to display progress
93+
ProgressOutput io.Writer
94+
// Labels to set on the content
95+
Labels []string
96+
// Platforms to fetch
97+
Platforms []string
98+
}
99+
100+
// NewFetchConfig returns the default FetchConfig from cli flags
101+
func NewFetchConfig(ctx context.Context, clicontext *cli.Context) (*FetchConfig, error) {
102+
resolver, err := commands.GetResolver(ctx, clicontext)
88103
if err != nil {
89-
return images.Image{}, err
104+
return nil, err
105+
}
106+
config := &FetchConfig{
107+
Resolver: resolver,
108+
Labels: clicontext.StringSlice("label"),
109+
}
110+
if !clicontext.GlobalBool("debug") {
111+
config.ProgressOutput = os.Stdout
112+
}
113+
if !clicontext.Bool("all-platforms") {
114+
p := clicontext.StringSlice("platform")
115+
if len(p) == 0 {
116+
p = append(p, platforms.Default())
117+
}
118+
config.Platforms = p
90119
}
120+
return config, nil
121+
}
91122

123+
// Fetch loads all resources into the content store and returns the image
124+
func Fetch(ctx context.Context, client *containerd.Client, ref string, config *FetchConfig) (images.Image, error) {
92125
ongoing := newJobs(ref)
93126

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

97130
go func() {
98-
if !cliContext.GlobalBool("debug") {
131+
if config.ProgressOutput != nil {
99132
// no progress bar, because it hides some debug logs
100-
showProgress(pctx, ongoing, client.ContentStore(), os.Stdout)
133+
showProgress(pctx, ongoing, client.ContentStore(), config.ProgressOutput)
101134
}
102135
close(progress)
103136
}()
@@ -110,24 +143,16 @@ func Fetch(ctx context.Context, client *containerd.Client, ref string, cliContex
110143
})
111144

112145
log.G(pctx).WithField("image", ref).Debug("fetching")
113-
labels := commands.LabelArgs(cliContext.StringSlice("label"))
146+
labels := commands.LabelArgs(config.Labels)
114147
opts := []containerd.RemoteOpt{
115148
containerd.WithPullLabels(labels),
116-
containerd.WithResolver(resolver),
149+
containerd.WithResolver(config.Resolver),
117150
containerd.WithImageHandler(h),
118151
containerd.WithSchema1Conversion,
119152
}
120-
121-
if !cliContext.Bool("all-platforms") {
122-
p := cliContext.StringSlice("platform")
123-
if len(p) == 0 {
124-
p = append(p, platforms.Default())
125-
}
126-
for _, platform := range p {
127-
opts = append(opts, containerd.WithPlatform(platform))
128-
}
153+
for _, platform := range config.Platforms {
154+
opts = append(opts, containerd.WithPlatform(platform))
129155
}
130-
131156
img, err := client.Fetch(pctx, ref, opts...)
132157
stopProgress()
133158
if err != nil {

cmd/ctr/commands/images/pull.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ command. As part of this process, we do the following:
7373
}
7474
defer done(ctx)
7575

76-
img, err := content.Fetch(ctx, client, ref, context)
76+
config, err := content.NewFetchConfig(ctx, context)
77+
if err != nil {
78+
return err
79+
}
80+
img, err := content.Fetch(ctx, client, ref, config)
7781
if err != nil {
7882
return err
7983
}

0 commit comments

Comments
 (0)