cmd/create, cmd/run: Style fixes

It's better not to use the global flag variables beyond the top-level
RunE functions, because sometimes the lower-level functions are re-used
from other files within the 'cmd' package.  In this case,
createContainer(), and hence pullImage(), is also used in src/cmd/run.go
to implement the 'run' command.  However, the 'run' command doesn't have
a --authflags option.

Since the default value of the flag is the zero value of the type, which
is a NOP in the code, it's likely that the code was still correct, but
it will be better to maintain some discipline here to highlight the
inputs needed by the lower-level functions.  Otherwise, things can get
tangled up.

Fallout from ecd1ced719

https://github.com/containers/toolbox/pull/1240
This commit is contained in:
Debarshi Ray 2023-02-16 15:14:33 +01:00
parent 48c07b8e5b
commit 8af015ed37
2 changed files with 8 additions and 8 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright © 2019 2022 Red Hat Inc.
* Copyright © 2019 2023 Red Hat Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -176,14 +176,14 @@ func create(cmd *cobra.Command, args []string) error {
return err
}
if err := createContainer(container, image, release, true); err != nil {
if err := createContainer(container, image, release, createFlags.authFile, true); err != nil {
return err
}
return nil
}
func createContainer(container, image, release string, showCommandToEnter bool) error {
func createContainer(container, image, release, authFile string, showCommandToEnter bool) error {
if container == "" {
panic("container not specified")
}
@ -210,7 +210,7 @@ func createContainer(container, image, release string, showCommandToEnter bool)
return errors.New(errMsg)
}
pulled, err := pullImage(image, release)
pulled, err := pullImage(image, release, authFile)
if err != nil {
return err
}
@ -646,7 +646,7 @@ func getServiceSocket(serviceName string, unitName string) (string, error) {
return "", fmt.Errorf("failed to find a SOCK_STREAM socket for %s", unitName)
}
func pullImage(image, release string) (bool, error) {
func pullImage(image, release, authFile string) (bool, error) {
if ok := utils.ImageReferenceCanBeID(image); ok {
logrus.Debugf("Looking for image %s", image)
@ -721,7 +721,7 @@ func pullImage(image, release string) (bool, error) {
defer s.Stop()
}
if err := podman.Pull(imageFull, createFlags.authFile); err != nil {
if err := podman.Pull(imageFull, authFile); err != nil {
var builder strings.Builder
fmt.Fprintf(&builder, "failed to pull image %s\n", imageFull)
fmt.Fprintf(&builder, "If it was a private image, log in with: podman login %s\n", domain)

View file

@ -1,5 +1,5 @@
/*
* Copyright © 2019 2022 Red Hat Inc.
* Copyright © 2019 2023 Red Hat Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -215,7 +215,7 @@ func runCommand(container string,
return nil
}
if err := createContainer(container, image, release, false); err != nil {
if err := createContainer(container, image, release, "", false); err != nil {
return err
}
} else if containersCount == 1 && defaultContainer {