Merge pull request #25 from tonistiigi/imagetools
imagetools: initial partial implementationpull/28/head
commit
fc2a28b639
@ -0,0 +1,41 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"github.com/docker/cli/cli/command"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
type createOptions struct {
|
||||
files []string
|
||||
tags []string
|
||||
dryrun bool
|
||||
append bool
|
||||
}
|
||||
|
||||
func runCreate(dockerCli command.Cli, in createOptions, args []string) error {
|
||||
return errors.Errorf("not-implemented")
|
||||
}
|
||||
|
||||
func createCmd(dockerCli command.Cli) *cobra.Command {
|
||||
var options createOptions
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "create [OPTIONS] [SOURCE...]",
|
||||
Short: "Create a new image based on source images",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return runCreate(dockerCli, options, args)
|
||||
},
|
||||
}
|
||||
|
||||
flags := cmd.Flags()
|
||||
|
||||
flags.StringArrayVarP(&options.files, "file", "f", []string{}, "Read source descriptor from file")
|
||||
flags.StringArrayVarP(&options.tags, "tag", "t", []string{}, "Set reference for new image")
|
||||
flags.BoolVar(&options.dryrun, "dry-run", false, "Show final image instead of pushing")
|
||||
flags.BoolVar(&options.append, "append", false, "Append to existing manifest")
|
||||
|
||||
_ = flags
|
||||
|
||||
return cmd
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/containerd/containerd/images"
|
||||
"github.com/docker/cli/cli"
|
||||
"github.com/docker/cli/cli/command"
|
||||
"github.com/moby/buildkit/util/appcontext"
|
||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/tonistiigi/buildx/util/imagetools"
|
||||
)
|
||||
|
||||
type inspectOptions struct {
|
||||
raw bool
|
||||
}
|
||||
|
||||
func runInspect(dockerCli command.Cli, in inspectOptions, name string) error {
|
||||
ctx := appcontext.Context()
|
||||
|
||||
r := imagetools.New(imagetools.Opt{
|
||||
Auth: dockerCli.ConfigFile(),
|
||||
})
|
||||
|
||||
dt, desc, err := r.Get(ctx, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if in.raw {
|
||||
fmt.Printf("%s\n", dt)
|
||||
return nil
|
||||
}
|
||||
|
||||
switch desc.MediaType {
|
||||
// case images.MediaTypeDockerSchema2Manifest, specs.MediaTypeImageManifest:
|
||||
// TODO: handle distribution manifest and schema1
|
||||
case images.MediaTypeDockerSchema2ManifestList, ocispec.MediaTypeImageIndex:
|
||||
imagetools.PrintManifestList(dt, desc, name, os.Stdout)
|
||||
default:
|
||||
fmt.Printf("%s\n", dt)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func inspectCmd(dockerCli command.Cli) *cobra.Command {
|
||||
var options inspectOptions
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "inspect [OPTIONS] NAME",
|
||||
Short: "Show details of image in the registry",
|
||||
Args: cli.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
return runInspect(dockerCli, options, args[0])
|
||||
},
|
||||
}
|
||||
|
||||
flags := cmd.Flags()
|
||||
|
||||
flags.BoolVar(&options.raw, "raw", false, "Show original JSON manifest")
|
||||
|
||||
_ = flags
|
||||
|
||||
return cmd
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"github.com/docker/cli/cli/command"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func RootCmd(dockerCli command.Cli) *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "imagetools",
|
||||
Short: "Commands to work on images in registry",
|
||||
}
|
||||
|
||||
cmd.AddCommand(
|
||||
inspectCmd(dockerCli),
|
||||
createCmd(dockerCli),
|
||||
)
|
||||
|
||||
return cmd
|
||||
}
|
@ -0,0 +1,92 @@
|
||||
package imagetools
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/containerd/containerd/remotes"
|
||||
"github.com/containerd/containerd/remotes/docker"
|
||||
clitypes "github.com/docker/cli/cli/config/types"
|
||||
"github.com/docker/distribution/reference"
|
||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
)
|
||||
|
||||
type Auth interface {
|
||||
GetAuthConfig(registryHostname string) (clitypes.AuthConfig, error)
|
||||
}
|
||||
|
||||
type Opt struct {
|
||||
Auth Auth
|
||||
}
|
||||
|
||||
type Resolver struct {
|
||||
r remotes.Resolver
|
||||
}
|
||||
|
||||
func New(opt Opt) *Resolver {
|
||||
resolver := docker.NewResolver(docker.ResolverOptions{
|
||||
Client: http.DefaultClient,
|
||||
Credentials: toCredentialsFunc(opt.Auth),
|
||||
})
|
||||
return &Resolver{
|
||||
r: resolver,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Resolver) Get(ctx context.Context, in string) ([]byte, ocispec.Descriptor, error) {
|
||||
ref, err := parseRef(in)
|
||||
if err != nil {
|
||||
return nil, ocispec.Descriptor{}, err
|
||||
}
|
||||
|
||||
in, desc, err := r.r.Resolve(ctx, ref.String())
|
||||
if err != nil {
|
||||
return nil, ocispec.Descriptor{}, err
|
||||
}
|
||||
|
||||
fetcher, err := r.r.Fetcher(ctx, in)
|
||||
if err != nil {
|
||||
return nil, ocispec.Descriptor{}, err
|
||||
}
|
||||
|
||||
rc, err := fetcher.Fetch(ctx, desc)
|
||||
if err != nil {
|
||||
return nil, ocispec.Descriptor{}, err
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
_, err = io.Copy(buf, rc)
|
||||
rc.Close()
|
||||
if err != nil {
|
||||
return nil, ocispec.Descriptor{}, err
|
||||
}
|
||||
|
||||
return buf.Bytes(), desc, nil
|
||||
}
|
||||
|
||||
func parseRef(s string) (reference.Named, error) {
|
||||
ref, err := reference.ParseNormalizedNamed(s)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ref = reference.TagNameOnly(ref)
|
||||
return ref, nil
|
||||
}
|
||||
|
||||
func toCredentialsFunc(a Auth) func(string) (string, string, error) {
|
||||
return func(host string) (string, string, error) {
|
||||
if host == "registry-1.docker.io" {
|
||||
host = "https://index.docker.io/v1/"
|
||||
}
|
||||
ac, err := a.GetAuthConfig(host)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
if ac.IdentityToken != "" {
|
||||
return "", ac.IdentityToken, nil
|
||||
}
|
||||
return ac.Username, ac.Password, nil
|
||||
}
|
||||
}
|
@ -0,0 +1,74 @@
|
||||
package imagetools
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
|
||||
"github.com/containerd/containerd/platforms"
|
||||
"github.com/docker/distribution/reference"
|
||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
)
|
||||
|
||||
func PrintManifestList(dt []byte, desc ocispec.Descriptor, refstr string, out io.Writer) error {
|
||||
ref, err := parseRef(refstr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var mfst ocispec.Index
|
||||
if err := json.Unmarshal(dt, &mfst); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w := tabwriter.NewWriter(out, 0, 0, 1, ' ', 0)
|
||||
|
||||
fmt.Fprintf(w, "Name:\t%s\n", ref.String())
|
||||
fmt.Fprintf(w, "MediaType:\t%s\n", desc.MediaType)
|
||||
fmt.Fprintf(w, "Digest:\t%s\n", desc.Digest)
|
||||
fmt.Fprintf(w, "\t\n")
|
||||
|
||||
fmt.Fprintf(w, "Manifests:\t\n")
|
||||
w.Flush()
|
||||
|
||||
pfx := " "
|
||||
|
||||
w = tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
|
||||
for i, m := range mfst.Manifests {
|
||||
if i != 0 {
|
||||
fmt.Fprintf(w, "\t\n")
|
||||
}
|
||||
cr, err := reference.WithDigest(ref, m.Digest)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Fprintf(w, "%sName:\t%s\n", pfx, cr.String())
|
||||
fmt.Fprintf(w, "%sMediaType:\t%s\n", pfx, m.MediaType)
|
||||
if p := m.Platform; p != nil {
|
||||
fmt.Fprintf(w, "%sPlatform:\t%s\n", pfx, platforms.Format(*p))
|
||||
if p.OSVersion != "" {
|
||||
fmt.Fprintf(w, "%sOSVersion:\t%s\n", pfx, p.OSVersion)
|
||||
}
|
||||
if len(p.OSFeatures) > 0 {
|
||||
fmt.Fprintf(w, "%sOSFeatures:\t%s\n", pfx, strings.Join(p.OSFeatures, ", "))
|
||||
}
|
||||
if len(m.URLs) > 0 {
|
||||
fmt.Fprintf(w, "%sURLs:\t%s\n", pfx, strings.Join(m.URLs, ", "))
|
||||
}
|
||||
if len(m.Annotations) > 0 {
|
||||
w.Flush()
|
||||
w2 := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
|
||||
pfx2 := pfx + " "
|
||||
for k, v := range m.Annotations {
|
||||
fmt.Fprintf(w2, "%s%s:\t%s\n", pfx2, k, v)
|
||||
}
|
||||
w2.Flush()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return w.Flush()
|
||||
}
|
Loading…
Reference in New Issue