Skip to content

Commit

Permalink
Update for adding context.Context to containers/image API
Browse files Browse the repository at this point in the history
In addition to the minimum necessary to update the API, also rename some
parameters/variables for consistency:

c	*cli.Context
ctx	context.Context
sys	*types.SystemContext

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
  • Loading branch information
mtrmac committed Apr 9, 2018
1 parent 3788217 commit 8f92a52
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 56 deletions.
29 changes: 15 additions & 14 deletions cmd/skopeo/copy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"errors"
"fmt"
"os"
Expand Down Expand Up @@ -30,48 +31,48 @@ func contextsFromGlobalOptions(c *cli.Context) (*types.SystemContext, *types.Sys
return sourceCtx, destinationCtx, nil
}

func copyHandler(context *cli.Context) error {
if len(context.Args()) != 2 {
func copyHandler(c *cli.Context) error {
if len(c.Args()) != 2 {
return errors.New("Usage: copy source destination")
}

policyContext, err := getPolicyContext(context)
policyContext, err := getPolicyContext(c)
if err != nil {
return fmt.Errorf("Error loading trust policy: %v", err)
}
defer policyContext.Destroy()

srcRef, err := alltransports.ParseImageName(context.Args()[0])
srcRef, err := alltransports.ParseImageName(c.Args()[0])
if err != nil {
return fmt.Errorf("Invalid source name %s: %v", context.Args()[0], err)
return fmt.Errorf("Invalid source name %s: %v", c.Args()[0], err)
}
destRef, err := alltransports.ParseImageName(context.Args()[1])
destRef, err := alltransports.ParseImageName(c.Args()[1])
if err != nil {
return fmt.Errorf("Invalid destination name %s: %v", context.Args()[1], err)
return fmt.Errorf("Invalid destination name %s: %v", c.Args()[1], err)
}
signBy := context.String("sign-by")
removeSignatures := context.Bool("remove-signatures")
signBy := c.String("sign-by")
removeSignatures := c.Bool("remove-signatures")

sourceCtx, destinationCtx, err := contextsFromGlobalOptions(context)
sourceCtx, destinationCtx, err := contextsFromGlobalOptions(c)
if err != nil {
return err
}

var manifestType string
if context.IsSet("format") {
switch context.String("format") {
if c.IsSet("format") {
switch c.String("format") {
case "oci":
manifestType = imgspecv1.MediaTypeImageManifest
case "v2s1":
manifestType = manifest.DockerV2Schema1SignedMediaType
case "v2s2":
manifestType = manifest.DockerV2Schema2MediaType
default:
return fmt.Errorf("unknown format %q. Choose on of the supported formats: 'oci', 'v2s1', or 'v2s2'", context.String("format"))
return fmt.Errorf("unknown format %q. Choose on of the supported formats: 'oci', 'v2s1', or 'v2s2'", c.String("format"))
}
}

return copy.Image(policyContext, destRef, srcRef, &copy.Options{
return copy.Image(context.Background(), policyContext, destRef, srcRef, &copy.Options{
RemoveSignatures: removeSignatures,
SignBy: signBy,
ReportWriter: os.Stdout,
Expand Down
13 changes: 7 additions & 6 deletions cmd/skopeo/delete.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"errors"
"fmt"
"strings"
Expand All @@ -10,21 +11,21 @@ import (
"github.com/urfave/cli"
)

func deleteHandler(context *cli.Context) error {
if len(context.Args()) != 1 {
func deleteHandler(c *cli.Context) error {
if len(c.Args()) != 1 {
return errors.New("Usage: delete imageReference")
}

ref, err := alltransports.ParseImageName(context.Args()[0])
ref, err := alltransports.ParseImageName(c.Args()[0])
if err != nil {
return fmt.Errorf("Invalid source name %s: %v", context.Args()[0], err)
return fmt.Errorf("Invalid source name %s: %v", c.Args()[0], err)
}

ctx, err := contextFromGlobalOptions(context, "")
sys, err := contextFromGlobalOptions(c, "")
if err != nil {
return err
}
return ref.DeleteImage(ctx)
return ref.DeleteImage(context.Background(), sys)
}

var deleteCmd = cli.Command{
Expand Down
11 changes: 7 additions & 4 deletions cmd/skopeo/inspect.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"encoding/json"
"fmt"
"strings"
Expand Down Expand Up @@ -66,7 +67,9 @@ var inspectCmd = cli.Command{
},
},
Action: func(c *cli.Context) (retErr error) {
img, err := parseImage(c)
ctx := context.Background()

img, err := parseImage(ctx, c)
if err != nil {
return err
}
Expand All @@ -77,7 +80,7 @@ var inspectCmd = cli.Command{
}
}()

rawManifest, _, err := img.Manifest()
rawManifest, _, err := img.Manifest(ctx)
if err != nil {
return err
}
Expand All @@ -88,7 +91,7 @@ var inspectCmd = cli.Command{
}
return nil
}
imgInspect, err := img.Inspect()
imgInspect, err := img.Inspect(ctx)
if err != nil {
return err
}
Expand All @@ -110,7 +113,7 @@ var inspectCmd = cli.Command{
}
if dockerImg, ok := img.(*docker.Image); ok {
outputData.Name = dockerImg.SourceRefFullName()
outputData.RepoTags, err = dockerImg.GetRepositoryTags()
outputData.RepoTags, err = dockerImg.GetRepositoryTags(ctx)
if err != nil {
// some registries may decide to block the "list all tags" endpoint
// gracefully allow the inspect to continue in this case. Currently
Expand Down
22 changes: 13 additions & 9 deletions cmd/skopeo/layers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"io/ioutil"
"os"
Expand All @@ -24,15 +25,18 @@ var layersCmd = cli.Command{
if c.NArg() == 0 {
return errors.New("Usage: layers imageReference [layer...]")
}
ctx, err := contextFromGlobalOptions(c, "")

ctx := context.Background()

sys, err := contextFromGlobalOptions(c, "")
if err != nil {
return err
}
rawSource, err := parseImageSource(c, c.Args()[0])
rawSource, err := parseImageSource(ctx, c, c.Args()[0])
if err != nil {
return err
}
src, err := image.FromSource(ctx, rawSource)
src, err := image.FromSource(ctx, sys, rawSource)
if err != nil {
if closeErr := rawSource.Close(); closeErr != nil {
return errors.Wrapf(err, " (close error: %v)", closeErr)
Expand Down Expand Up @@ -85,7 +89,7 @@ var layersCmd = cli.Command{
if err != nil {
return err
}
dest, err := tmpDirRef.NewImageDestination(nil)
dest, err := tmpDirRef.NewImageDestination(ctx, nil)
if err != nil {
return err
}
Expand All @@ -97,26 +101,26 @@ var layersCmd = cli.Command{
}()

for _, bd := range blobDigests {
r, blobSize, err := rawSource.GetBlob(types.BlobInfo{Digest: bd.digest, Size: -1})
r, blobSize, err := rawSource.GetBlob(ctx, types.BlobInfo{Digest: bd.digest, Size: -1})
if err != nil {
return err
}
if _, err := dest.PutBlob(r, types.BlobInfo{Digest: bd.digest, Size: blobSize}, bd.isConfig); err != nil {
if _, err := dest.PutBlob(ctx, r, types.BlobInfo{Digest: bd.digest, Size: blobSize}, bd.isConfig); err != nil {
if closeErr := r.Close(); closeErr != nil {
return errors.Wrapf(err, " (close error: %v)", closeErr)
}
return err
}
}

manifest, _, err := src.Manifest()
manifest, _, err := src.Manifest(ctx)
if err != nil {
return err
}
if err := dest.PutManifest(manifest); err != nil {
if err := dest.PutManifest(ctx, manifest); err != nil {
return err
}

return dest.Commit()
return dest.Commit(ctx)
},
}
34 changes: 17 additions & 17 deletions cmd/skopeo/signing.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
"github.com/urfave/cli"
)

func standaloneSign(context *cli.Context) error {
outputFile := context.String("output")
if len(context.Args()) != 3 || outputFile == "" {
func standaloneSign(c *cli.Context) error {
outputFile := c.String("output")
if len(c.Args()) != 3 || outputFile == "" {
return errors.New("Usage: skopeo standalone-sign manifest docker-reference key-fingerprint -o signature")
}
manifestPath := context.Args()[0]
dockerReference := context.Args()[1]
fingerprint := context.Args()[2]
manifestPath := c.Args()[0]
dockerReference := c.Args()[1]
fingerprint := c.Args()[2]

manifest, err := ioutil.ReadFile(manifestPath)
if err != nil {
Expand Down Expand Up @@ -53,14 +53,14 @@ var standaloneSignCmd = cli.Command{
},
}

func standaloneVerify(context *cli.Context) error {
if len(context.Args()) != 4 {
func standaloneVerify(c *cli.Context) error {
if len(c.Args()) != 4 {
return errors.New("Usage: skopeo standalone-verify manifest docker-reference key-fingerprint signature")
}
manifestPath := context.Args()[0]
expectedDockerReference := context.Args()[1]
expectedFingerprint := context.Args()[2]
signaturePath := context.Args()[3]
manifestPath := c.Args()[0]
expectedDockerReference := c.Args()[1]
expectedFingerprint := c.Args()[2]
signaturePath := c.Args()[3]

unverifiedManifest, err := ioutil.ReadFile(manifestPath)
if err != nil {
Expand All @@ -81,7 +81,7 @@ func standaloneVerify(context *cli.Context) error {
return fmt.Errorf("Error verifying signature: %v", err)
}

fmt.Fprintf(context.App.Writer, "Signature verified, digest %s\n", sig.DockerManifestDigest)
fmt.Fprintf(c.App.Writer, "Signature verified, digest %s\n", sig.DockerManifestDigest)
return nil
}

Expand All @@ -92,11 +92,11 @@ var standaloneVerifyCmd = cli.Command{
Action: standaloneVerify,
}

func untrustedSignatureDump(context *cli.Context) error {
if len(context.Args()) != 1 {
func untrustedSignatureDump(c *cli.Context) error {
if len(c.Args()) != 1 {
return errors.New("Usage: skopeo untrusted-signature-dump-without-verification signature")
}
untrustedSignaturePath := context.Args()[0]
untrustedSignaturePath := c.Args()[0]

untrustedSignature, err := ioutil.ReadFile(untrustedSignaturePath)
if err != nil {
Expand All @@ -111,7 +111,7 @@ func untrustedSignatureDump(context *cli.Context) error {
if err != nil {
return err
}
fmt.Fprintln(context.App.Writer, string(untrustedOut))
fmt.Fprintln(c.App.Writer, string(untrustedOut))
return nil
}

Expand Down
13 changes: 7 additions & 6 deletions cmd/skopeo/utils.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"errors"
"strings"

Expand Down Expand Up @@ -66,29 +67,29 @@ func getDockerAuth(creds string) (*types.DockerAuthConfig, error) {

// parseImage converts image URL-like string to an initialized handler for that image.
// The caller must call .Close() on the returned ImageCloser.
func parseImage(c *cli.Context) (types.ImageCloser, error) {
func parseImage(ctx context.Context, c *cli.Context) (types.ImageCloser, error) {
imgName := c.Args().First()
ref, err := alltransports.ParseImageName(imgName)
if err != nil {
return nil, err
}
ctx, err := contextFromGlobalOptions(c, "")
sys, err := contextFromGlobalOptions(c, "")
if err != nil {
return nil, err
}
return ref.NewImage(ctx)
return ref.NewImage(ctx, sys)
}

// parseImageSource converts image URL-like string to an ImageSource.
// The caller must call .Close() on the returned ImageSource.
func parseImageSource(c *cli.Context, name string) (types.ImageSource, error) {
func parseImageSource(ctx context.Context, c *cli.Context, name string) (types.ImageSource, error) {
ref, err := alltransports.ParseImageName(name)
if err != nil {
return nil, err
}
ctx, err := contextFromGlobalOptions(c, "")
sys, err := contextFromGlobalOptions(c, "")
if err != nil {
return nil, err
}
return ref.NewImageSource(ctx)
return ref.NewImageSource(ctx, sys)
}

0 comments on commit 8f92a52

Please sign in to comment.