Skip to content

Commit

Permalink
Eliminate cremote.UploadFile. (#797)
Browse files Browse the repository at this point in the history
This can now be done in terms of `static.NewFile` and `remote.Write`, which is simply enough to just inline.

Signed-off-by: Matt Moore <mattomata@gmail.com>
  • Loading branch information
mattmoor authored Sep 25, 2021
1 parent d77d120 commit 52faaca
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
10 changes: 7 additions & 3 deletions cmd/cosign/cli/attach/sbom.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ import (
"path/filepath"

"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/types"
"github.com/peterbourgon/ff/v3/ffcli"

"github.com/sigstore/cosign/cmd/cosign/cli/options"
cremote "github.com/sigstore/cosign/pkg/cosign/remote"
ociremote "github.com/sigstore/cosign/pkg/oci/remote"
"github.com/sigstore/cosign/pkg/oci/static"
ctypes "github.com/sigstore/cosign/pkg/types"
)

Expand Down Expand Up @@ -86,8 +87,11 @@ func SBOMCmd(ctx context.Context, regOpts options.RegistryOpts, sbomRef string,
}

fmt.Fprintf(os.Stderr, "Uploading SBOM file for [%s] to [%s] with mediaType [%s].\n", ref.Name(), dstRef.Name(), sbomType)
_, err = cremote.UploadFile(b, dstRef, sbomType, types.OCIConfigJSON, remoteOpts...)
return err
img, err := static.NewFile(b, static.WithLayerMediaType(sbomType))
if err != nil {
return err
}
return remote.Write(dstRef, img, remoteOpts...)
}

func sbomBytes(sbomRef string) ([]byte, error) {
Expand Down
10 changes: 7 additions & 3 deletions cmd/cosign/cli/upload/wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ import (
"os"

"github.com/google/go-containerregistry/pkg/name"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/peterbourgon/ff/v3/ffcli"

"github.com/sigstore/cosign/cmd/cosign/cli/options"
cremote "github.com/sigstore/cosign/pkg/cosign/remote"
"github.com/sigstore/cosign/pkg/oci/static"
"github.com/sigstore/cosign/pkg/types"
)

Expand Down Expand Up @@ -63,6 +64,9 @@ func WasmCmd(ctx context.Context, regOpts options.RegistryOpts, wasmPath, imageR
return err
}
fmt.Fprintf(os.Stderr, "Uploading wasm file from [%s] to [%s].\n", wasmPath, ref.Name())
_, err = cremote.UploadFile(b, ref, types.WasmLayerMediaType, types.WasmConfigMediaType, regOpts.GetRegistryClientOpts(ctx)...)
return err
img, err := static.NewFile(b, static.WithLayerMediaType(types.WasmLayerMediaType), static.WithConfigMediaType(types.WasmConfigMediaType))
if err != nil {
return err
}
return remote.Write(ref, img, regOpts.GetRegistryClientOpts(ctx)...)
}
18 changes: 5 additions & 13 deletions pkg/cosign/remote/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,14 @@ func UploadFiles(ref name.Reference, files []File, getMt MediaTypeGetter, remote
}
mt := getMt(b)
fmt.Fprintf(os.Stderr, "Uploading file from [%s] to [%s] with media type [%s]\n", f.Path(), ref.Name(), mt)
img, err := UploadFile(b, ref, mt, types.OCIConfigJSON, remoteOpts...)

img, err := static.NewFile(b, static.WithLayerMediaType(mt))
if err != nil {
return name.Digest{}, err
}
if err := remote.Write(ref, img, remoteOpts...); err != nil {
return name.Digest{}, err
}
l, err := img.Layers()
if err != nil {
return name.Digest{}, err
Expand Down Expand Up @@ -137,15 +141,3 @@ func UploadFiles(ref name.Reference, files []File, getMt MediaTypeGetter, remote
}
return ref.Context().Digest(lastHash.String()), nil
}

func UploadFile(b []byte, ref name.Reference, layerMT, configMT types.MediaType, remoteOpts ...remote.Option) (v1.Image, error) {
img, err := static.NewFile(b, static.WithLayerMediaType(layerMT), static.WithConfigMediaType(configMT))
if err != nil {
return nil, err
}

if err := remote.Write(ref, img, remoteOpts...); err != nil {
return nil, err
}
return img, nil
}

0 comments on commit 52faaca

Please sign in to comment.