1
0
mirror of https://github.com/containers/buildah.git synced 2025-07-31 15:24:26 +03:00

Do not print directly to stdout in Commit()

Buildah is being used as a library in Podman, and as such should
avoid printing directly to standard streams. Instead, return an
image ID which can be printed by the caller (if desired)

Signed-off-by: Matthew Heon <matthew.heon@gmail.com>

Closes: #647
Approved by: rhatdan
This commit is contained in:
Matthew Heon
2018-05-01 13:20:16 -04:00
committed by Atomic Bot
parent e130f2bef3
commit a4f5707eeb
3 changed files with 33 additions and 14 deletions

View File

@ -1,6 +1,7 @@
package main package main
import ( import (
"fmt"
"os" "os"
"strings" "strings"
"time" "time"
@ -159,13 +160,16 @@ func commitCmd(c *cli.Context) error {
if !c.Bool("quiet") { if !c.Bool("quiet") {
options.ReportWriter = os.Stderr options.ReportWriter = os.Stderr
} }
err = builder.Commit(ctx, dest, options) id, err := builder.Commit(ctx, dest, options)
if err != nil { if err != nil {
return util.GetFailureCause( return util.GetFailureCause(
err, err,
errors.Wrapf(err, "error committing container %q to %q", builder.Container, image), errors.Wrapf(err, "error committing container %q to %q", builder.Container, image),
) )
} }
if options.IIDFile == "" && id != "" {
fmt.Printf("%s\n", id)
}
if c.Bool("rm") { if c.Bool("rm") {
return builder.Delete() return builder.Delete()

View File

@ -78,16 +78,19 @@ type PushOptions struct {
// Commit writes the contents of the container, along with its updated // Commit writes the contents of the container, along with its updated
// configuration, to a new image in the specified location, and if we know how, // configuration, to a new image in the specified location, and if we know how,
// add any additional tags that were specified. // add any additional tags that were specified. Returns the ID of the new image
func (b *Builder) Commit(ctx context.Context, dest types.ImageReference, options CommitOptions) error { // if commit was successful and the image destination was local
func (b *Builder) Commit(ctx context.Context, dest types.ImageReference, options CommitOptions) (string, error) {
var imgID string
systemContext := getSystemContext(options.SystemContext, options.SignaturePolicyPath) systemContext := getSystemContext(options.SystemContext, options.SignaturePolicyPath)
policy, err := signature.DefaultPolicy(systemContext) policy, err := signature.DefaultPolicy(systemContext)
if err != nil { if err != nil {
return errors.Wrapf(err, "error obtaining default signature policy") return imgID, errors.Wrapf(err, "error obtaining default signature policy")
} }
policyContext, err := signature.NewPolicyContext(policy) policyContext, err := signature.NewPolicyContext(policy)
if err != nil { if err != nil {
return errors.Wrapf(err, "error creating new signature policy context") return imgID, errors.Wrapf(err, "error creating new signature policy context")
} }
defer func() { defer func() {
if err2 := policyContext.Destroy(); err2 != nil { if err2 := policyContext.Destroy(); err2 != nil {
@ -99,23 +102,23 @@ func (b *Builder) Commit(ctx context.Context, dest types.ImageReference, options
exporting := !destIsStorage exporting := !destIsStorage
src, err := b.makeImageRef(options.PreferredManifestType, exporting, options.Compression, options.HistoryTimestamp) src, err := b.makeImageRef(options.PreferredManifestType, exporting, options.Compression, options.HistoryTimestamp)
if err != nil { if err != nil {
return errors.Wrapf(err, "error computing layer digests and building metadata") return imgID, errors.Wrapf(err, "error computing layer digests and building metadata")
} }
// "Copy" our image to where it needs to be. // "Copy" our image to where it needs to be.
err = cp.Image(ctx, policyContext, dest, src, getCopyOptions(options.ReportWriter, nil, systemContext, "")) err = cp.Image(ctx, policyContext, dest, src, getCopyOptions(options.ReportWriter, nil, systemContext, ""))
if err != nil { if err != nil {
return errors.Wrapf(err, "error copying layers and metadata") return imgID, errors.Wrapf(err, "error copying layers and metadata")
} }
if len(options.AdditionalTags) > 0 { if len(options.AdditionalTags) > 0 {
switch dest.Transport().Name() { switch dest.Transport().Name() {
case is.Transport.Name(): case is.Transport.Name():
img, err := is.Transport.GetStoreImage(b.store, dest) img, err := is.Transport.GetStoreImage(b.store, dest)
if err != nil { if err != nil {
return errors.Wrapf(err, "error locating just-written image %q", transports.ImageName(dest)) return imgID, errors.Wrapf(err, "error locating just-written image %q", transports.ImageName(dest))
} }
err = util.AddImageNames(b.store, "", systemContext, img, options.AdditionalTags) err = util.AddImageNames(b.store, "", systemContext, img, options.AdditionalTags)
if err != nil { if err != nil {
return errors.Wrapf(err, "error setting image names to %v", append(img.Names, options.AdditionalTags...)) return imgID, errors.Wrapf(err, "error setting image names to %v", append(img.Names, options.AdditionalTags...))
} }
logrus.Debugf("assigned names %v to image %q", img.Names, img.ID) logrus.Debugf("assigned names %v to image %q", img.Names, img.ID)
default: default:
@ -124,16 +127,21 @@ func (b *Builder) Commit(ctx context.Context, dest types.ImageReference, options
} }
img, err := is.Transport.GetStoreImage(b.store, dest) img, err := is.Transport.GetStoreImage(b.store, dest)
if err != nil && err != storage.ErrImageUnknown {
return imgID, err
}
if err == nil { if err == nil {
imgID = img.ID
if options.IIDFile != "" { if options.IIDFile != "" {
if err := ioutil.WriteFile(options.IIDFile, []byte(img.ID), 0644); err != nil { if err := ioutil.WriteFile(options.IIDFile, []byte(img.ID), 0644); err != nil {
return errors.Wrapf(err, "failed to write Image ID File %q", options.IIDFile) return imgID, errors.Wrapf(err, "failed to write Image ID File %q", options.IIDFile)
} }
} else {
fmt.Printf("%s\n", img.ID)
} }
} }
return nil
return imgID, nil
} }
// Push copies the contents of the image to a new location. // Push copies the contents of the image to a new location.

View File

@ -693,7 +693,14 @@ func (b *Executor) Commit(ctx context.Context, ib *imagebuilder.Builder) (err er
PreferredManifestType: b.outputFormat, PreferredManifestType: b.outputFormat,
IIDFile: b.iidfile, IIDFile: b.iidfile,
} }
return b.builder.Commit(ctx, imageRef, options) imgID, err := b.builder.Commit(ctx, imageRef, options)
if err != nil {
return err
}
if options.IIDFile == "" && imgID != "" {
fmt.Printf("%s\n", imgID)
}
return nil
} }
// Build takes care of the details of running Prepare/Execute/Commit/Delete // Build takes care of the details of running Prepare/Execute/Commit/Delete