Replace contextFromImageOptions by imageOptions.newSystemContext

We no longer need the *cli.Context parameter, and at that point
it looks much cleaner to make this a method (already individually;
it will be even cleaner after a similar imageDestOptions conversion).

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
This commit is contained in:
Miloslav Trmač
2018-07-18 00:56:32 +02:00
parent 39de98777d
commit 3094320203
6 changed files with 23 additions and 23 deletions

View File

@@ -18,7 +18,7 @@ import (
// contextsFromCopyOptions returns source and destionation types.SystemContext depending on c.
func contextsFromCopyOptions(c *cli.Context, opts *copyOptions) (*types.SystemContext, *types.SystemContext, error) {
sourceCtx, err := contextFromImageOptions(c, opts.srcImage)
sourceCtx, err := opts.srcImage.newSystemContext()
if err != nil {
return nil, nil, err
}

View File

@@ -49,7 +49,7 @@ func (opts *deleteOptions) run(c *cli.Context) error {
return fmt.Errorf("Invalid source name %s: %v", c.Args()[0], err)
}
sys, err := contextFromImageOptions(c, opts.image)
sys, err := opts.image.newSystemContext()
if err != nil {
return err
}

View File

@@ -115,7 +115,7 @@ func (opts *inspectOptions) run(c *cli.Context) (retErr error) {
outputData.Name = dockerRef.Name()
}
if img.Reference().Transport() == docker.Transport {
sys, err := contextFromImageOptions(c, opts.image)
sys, err := opts.image.newSystemContext()
if err != nil {
return err
}

View File

@@ -46,7 +46,7 @@ func (opts *layersOptions) run(c *cli.Context) (retErr error) {
ctx, cancel := opts.global.commandTimeoutContext()
defer cancel()
sys, err := contextFromImageOptions(c, opts.image)
sys, err := opts.image.newSystemContext()
if err != nil {
return err
}

View File

@@ -83,9 +83,9 @@ func imageFlags(global *globalOptions, shared *sharedImageOptions, flagPrefix, c
}, &opts
}
// contextFromImageOptions returns a *types.SystemContext corresponding to opts.
// newSystemContext returns a *types.SystemContext corresponding to opts.
// It is guaranteed to return a fresh instance, so it is safe to make additional updates to it.
func contextFromImageOptions(c *cli.Context, opts *imageOptions) (*types.SystemContext, error) {
func (opts *imageOptions) newSystemContext() (*types.SystemContext, error) {
ctx := &types.SystemContext{
RegistriesDirPath: opts.global.registriesDirPath,
ArchitectureChoice: opts.global.overrideArch,
@@ -145,7 +145,7 @@ func imageDestFlags(global *globalOptions, shared *sharedImageOptions, flagPrefi
// contextFromImageDestOptions returns a *types.SystemContext corresponding to opts.
// It is guaranteed to return a fresh instance, so it is safe to make additional updates to it.
func contextFromImageDestOptions(c *cli.Context, opts *imageDestOptions) (*types.SystemContext, error) {
ctx, err := contextFromImageOptions(c, opts.imageOptions)
ctx, err := opts.imageOptions.newSystemContext()
if err != nil {
return nil, err
}
@@ -188,7 +188,7 @@ func parseImage(ctx context.Context, c *cli.Context, opts *imageOptions) (types.
if err != nil {
return nil, err
}
sys, err := contextFromImageOptions(c, opts)
sys, err := opts.newSystemContext()
if err != nil {
return nil, err
}
@@ -202,7 +202,7 @@ func parseImageSource(ctx context.Context, c *cli.Context, opts *imageOptions, n
if err != nil {
return nil, err
}
sys, err := contextFromImageOptions(c, opts)
sys, err := opts.newSystemContext()
if err != nil {
return nil, err
}

View File

@@ -26,31 +26,31 @@ func fakeGlobalOptions(t *testing.T, flags []string) (*cli.App, *cli.Context, *g
return app, ctx, opts
}
// fakeImageContext creates inputs for contextFromImageOptions.
// fakeImageOptions creates imageOptions and sets it according to globalFlags/cmdFlags.
// NOTE: This is QUITE FAKE; none of the urfave/cli normalization and the like happens.
func fakeImageContext(t *testing.T, flagPrefix string, globalFlags []string, cmdFlags []string) (*cli.Context, *imageOptions) {
app, globalCtx, globalOpts := fakeGlobalOptions(t, globalFlags)
func fakeImageOptions(t *testing.T, flagPrefix string, globalFlags []string, cmdFlags []string) *imageOptions {
_, _, globalOpts := fakeGlobalOptions(t, globalFlags)
sharedFlags, sharedOpts := sharedImageFlags()
imageFlags, imageOpts := imageFlags(globalOpts, sharedOpts, flagPrefix, "")
flagSet := flag.NewFlagSet("fakeImageContext", flag.ContinueOnError)
flagSet := flag.NewFlagSet("fakeImageOptions", flag.ContinueOnError)
for _, f := range append(sharedFlags, imageFlags...) {
f.Apply(flagSet)
}
err := flagSet.Parse(cmdFlags)
require.NoError(t, err)
return cli.NewContext(app, flagSet, globalCtx), imageOpts
return imageOpts
}
func TestContextFromImageOptions(t *testing.T) {
func TestImageOptionsNewSystemContext(t *testing.T) {
// Default state
c, opts := fakeImageContext(t, "dest-", []string{}, []string{})
res, err := contextFromImageOptions(c, opts)
opts := fakeImageOptions(t, "dest-", []string{}, []string{})
res, err := opts.newSystemContext()
require.NoError(t, err)
assert.Equal(t, &types.SystemContext{}, res)
// Set everything to non-default values.
c, opts = fakeImageContext(t, "dest-", []string{
opts = fakeImageOptions(t, "dest-", []string{
"--registries.d", "/srv/registries.d",
"--override-arch", "overridden-arch",
"--override-os", "overridden-os",
@@ -62,7 +62,7 @@ func TestContextFromImageOptions(t *testing.T) {
"--dest-tls-verify=false",
"--dest-creds", "creds-user:creds-password",
})
res, err = contextFromImageOptions(c, opts)
res, err = opts.newSystemContext()
require.NoError(t, err)
assert.Equal(t, &types.SystemContext{
RegistriesDirPath: "/srv/registries.d",
@@ -102,16 +102,16 @@ func TestContextFromImageOptions(t *testing.T) {
if c.cmd != "" {
cmdFlags = append(cmdFlags, "--dest-tls-verify="+c.cmd)
}
ctx, opts := fakeImageContext(t, "dest-", globalFlags, cmdFlags)
res, err = contextFromImageOptions(ctx, opts)
opts := fakeImageOptions(t, "dest-", globalFlags, cmdFlags)
res, err = opts.newSystemContext()
require.NoError(t, err)
assert.Equal(t, c.expectedDocker, res.DockerInsecureSkipTLSVerify, "%#v", c)
assert.Equal(t, c.expectedDockerDaemon, res.DockerDaemonInsecureSkipTLSVerify, "%#v", c)
}
// Invalid option values
c, opts = fakeImageContext(t, "dest-", []string{}, []string{"--dest-creds", ""})
_, err = contextFromImageOptions(c, opts)
opts = fakeImageOptions(t, "dest-", []string{}, []string{"--dest-creds", ""})
_, err = opts.newSystemContext()
assert.Error(t, err)
}