Skip to content

Commit

Permalink
fix: image push
Browse files Browse the repository at this point in the history
  • Loading branch information
whatwewant committed Jul 31, 2024
1 parent 2cb6431 commit 7ac2179
Show file tree
Hide file tree
Showing 13 changed files with 91 additions and 49 deletions.
2 changes: 1 addition & 1 deletion cmd/docker/image/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Build() *cli.Command {
return err
}

return client.Image().Build(ctx.Context, src, func(opt *image.BuildOption) {
return client.Image().Build(ctx.Context, src, func(opt *image.BuildConfig) {
opt.Dockerfile = ctx.String("file")
opt.Tags = ctx.StringSlice("tag")

Expand Down
2 changes: 1 addition & 1 deletion cmd/docker/image/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func List() *cli.Command {
return err
}

images, err := client.Image().List(ctx.Context, func(opt *image.ListOption) {
images, err := client.Image().List(ctx.Context, func(opt *image.ListConfig) {
opt.All = ctx.Bool("all")
})
if err != nil {
Expand Down
28 changes: 28 additions & 0 deletions cmd/docker/image/push.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package image

import (
"github.com/go-zoox/cli"
"github.com/go-zoox/core-utils/fmt"
"github.com/go-zoox/docker"
)

func Push() *cli.Command {
return &cli.Command{
Name: "push",
Usage: "Push an image to a registry",
Flags: []cli.Flag{},
Action: func(ctx *cli.Context) error {
image := ctx.Args().First()
if image == "" {
return fmt.Errorf("image name is required")
}

client, err := docker.New()
if err != nil {
return err
}

return client.Image().Push(ctx.Context, image)
},
}
}
2 changes: 1 addition & 1 deletion cmd/docker/image/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Remove() *cli.Command {
return fmt.Errorf("container id is required")
}

imagesDeleted, err := client.Image().Remove(ctx.Context, imageID, func(opt *image.RemoveOption) {
imagesDeleted, err := client.Image().Remove(ctx.Context, imageID, func(opt *image.RemoveConfig) {
opt.Force = ctx.Bool("force")
opt.PruneChildren = ctx.Bool("no-prune")
})
Expand Down
1 change: 1 addition & 0 deletions cmd/docker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func main() {
image.Remove(),
//
image.Pull(),
image.Push(),
//
image.Build(),
//
Expand Down
12 changes: 6 additions & 6 deletions image/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ import (
"github.com/docker/docker/pkg/jsonmessage"
)

// BuildOption is the configuration for building a image
type BuildOption = types.ImageBuildOptions
// BuildConfig is the configuration for building a image
type BuildConfig = types.ImageBuildOptions

// Build builds a image
func (i *image) Build(ctx context.Context, src string, opts ...func(opt *BuildOption)) error {
opt := &BuildOption{}
func (i *image) Build(ctx context.Context, src string, opts ...func(cfg *BuildConfig)) error {
cfg := &BuildConfig{}
for _, o := range opts {
o(opt)
o(cfg)
}

tar, err := archive.TarWithOptions(src, &archive.TarOptions{})
if err != nil {
return err
}

response, err := i.client.ImageBuild(ctx, tar, *opt)
response, err := i.client.ImageBuild(ctx, tar, *cfg)
if err != nil {
return err
}
Expand Down
16 changes: 9 additions & 7 deletions image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import (

// Image is the docker image client interface
type Image interface {
List(ctx context.Context, opts ...func(opt *ListOption)) (images []entity.Image, err error)
Inspect(ctx context.Context, id string, opts ...func(opt *InspectOption)) (*types.ImageInspect, error)
Remove(ctx context.Context, id string, opts ...func(opt *RemoveOption)) ([]ti.DeleteResponse, error)
List(ctx context.Context, opts ...func(cfg *ListConfig)) (images []entity.Image, err error)
Inspect(ctx context.Context, id string, opts ...func(cfg *InspectConfig)) (*types.ImageInspect, error)
Remove(ctx context.Context, id string, opts ...func(cfg *RemoveConfig)) ([]ti.DeleteResponse, error)
//
Build(ctx context.Context, src string, opts ...func(cfg *BuildConfig)) error
//
Pull(ctx context.Context, name string, opts ...func(cfg *PullConfig)) error
Push(ctx context.Context, name string, opts ...func(cfg *PushConfig)) error
//
Build(ctx context.Context, src string, opts ...func(opt *BuildOption)) error
Pull(ctx context.Context, name string, opts ...func(opt *PullOption)) error
// Push(ctx context.Context, opts ...func(opt *PushOption)) error
Tag(ctx context.Context, source, target string) error
//
Prune(ctx context.Context, opts ...func(opt *PruneOption)) (types.ImagesPruneReport, error)
Prune(ctx context.Context, opts ...func(cfg *PruneConfig)) (types.ImagesPruneReport, error)
}

type image struct {
Expand Down
8 changes: 4 additions & 4 deletions image/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
"github.com/docker/docker/api/types"
)

type InspectOption struct {
type InspectConfig struct {
}

func (i *image) Inspect(ctx context.Context, id string, opts ...func(opt *InspectOption)) (*types.ImageInspect, error) {
opt := &InspectOption{}
func (i *image) Inspect(ctx context.Context, id string, opts ...func(opt *InspectConfig)) (*types.ImageInspect, error) {
cfg := &InspectConfig{}
for _, o := range opts {
o(opt)
o(cfg)
}

inspect, _, err := i.client.ImageInspectWithRaw(ctx, id)
Expand Down
6 changes: 3 additions & 3 deletions image/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/go-zoox/docker/entity"
)

type ListOption struct {
type ListConfig struct {
// All controls whether all images in the graph are filtered, or just
// the heads.
All bool
Expand All @@ -25,8 +25,8 @@ type ListOption struct {
ContainerCount bool
}

func (i *image) List(ctx context.Context, opts ...func(opt *ListOption)) (images []entity.Image, err error) {
opt := &ListOption{}
func (i *image) List(ctx context.Context, opts ...func(opt *ListConfig)) (images []entity.Image, err error) {
opt := &ListConfig{}
for _, o := range opts {
o(opt)
}
Expand Down
6 changes: 3 additions & 3 deletions image/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import (
"github.com/docker/docker/api/types/filters"
)

type PruneOption = filters.Args
type PruneConfig = filters.Args

func (i *image) Prune(ctx context.Context, opts ...func(opt *PruneOption)) (types.ImagesPruneReport, error) {
opt := &PruneOption{}
func (i *image) Prune(ctx context.Context, opts ...func(opt *PruneConfig)) (types.ImagesPruneReport, error) {
opt := &PruneConfig{}
for _, o := range opts {
o(opt)
}
Expand Down
20 changes: 10 additions & 10 deletions image/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/docker/docker/pkg/jsonmessage"
)

// PullOption is the options for pulling an image
type PullOption struct {
// PullConfig is the options for pulling an image
type PullConfig struct {
Auth struct {
Username string
Password string
Expand All @@ -25,19 +25,19 @@ type PullOption struct {
}

// Pull pulls an image
func (i *image) Pull(ctx context.Context, name string, opts ...func(opt *PullOption)) error {
opt := &PullOption{
func (i *image) Pull(ctx context.Context, name string, opts ...func(opt *PullConfig)) error {
cfg := &PullConfig{
Stdout: os.Stdout,
}
for _, o := range opts {
o(opt)
o(cfg)
}

auth := ""
if opt.Auth.Username != "" && opt.Auth.Password != "" {
if cfg.Auth.Username != "" && cfg.Auth.Password != "" {
authConfig := registry.AuthConfig{
Username: opt.Auth.Username,
Password: opt.Auth.Password,
Username: cfg.Auth.Username,
Password: cfg.Auth.Password,
}
encodedJSON, err := json.Marshal(authConfig)
if err != nil {
Expand All @@ -48,14 +48,14 @@ func (i *image) Pull(ctx context.Context, name string, opts ...func(opt *PullOpt

reader, err := i.client.ImagePull(ctx, name, dimage.PullOptions{
RegistryAuth: auth,
Platform: opt.Platform,
Platform: cfg.Platform,
})
if err != nil {
return err
}
defer reader.Close()

if err := jsonmessage.DisplayJSONMessagesToStream(reader, streams.NewOut(opt.Stdout), nil); err != nil {
if err := jsonmessage.DisplayJSONMessagesToStream(reader, streams.NewOut(cfg.Stdout), nil); err != nil {
return err
}

Expand Down
31 changes: 21 additions & 10 deletions image/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,35 @@ import (
"encoding/json"
"io"
"os"
"time"

"github.com/docker/cli/cli/streams"
dimage "github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/registry"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
)

// PushOption is the configuration for pushing an image
type PushOption struct {
Name string
// PushConfig is the configuration for pushing an image
type PushConfig struct {
Auth struct {
Username string
Password string
Server string
}

Stdout io.Writer
}

// Push pushes an image
func Push(cfg *PushOption) error {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Hour)
defer cancel()
func (i *image) Push(ctx context.Context, name string, opts ...func(cfg *PushConfig)) error {
cfg := &PushConfig{
Stdout: os.Stdout,
}
for _, o := range opts {
o(cfg)
}

auth := ""
auth := "no-auth" // @TODO fix: bad parameters and missing X-Registry-Auth: invalid X-Registry-Auth header: EOF
if cfg.Auth.Username != "" && cfg.Auth.Password != "" {
authConfig := registry.AuthConfig{
Username: cfg.Auth.Username,
Expand All @@ -47,13 +53,18 @@ func Push(cfg *PushOption) error {
return err
}

reader, err := cli.ImagePush(ctx, cfg.Name, dimage.PushOptions{
reader, err := cli.ImagePush(ctx, name, dimage.PushOptions{
All: true,
RegistryAuth: auth,
})
if err != nil {
return err
}
io.Copy(os.Stdout, reader)
defer reader.Close()

if err := jsonmessage.DisplayJSONMessagesToStream(reader, streams.NewOut(cfg.Stdout), nil); err != nil {
return err
}

return nil
}
6 changes: 3 additions & 3 deletions image/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import (
ti "github.com/docker/docker/api/types/image"
)

type RemoveOption struct {
type RemoveConfig struct {
Force bool
PruneChildren bool
}

func (i *image) Remove(ctx context.Context, id string, opts ...func(opt *RemoveOption)) ([]ti.DeleteResponse, error) {
opt := &RemoveOption{}
func (i *image) Remove(ctx context.Context, id string, opts ...func(opt *RemoveConfig)) ([]ti.DeleteResponse, error) {
opt := &RemoveConfig{}
for _, o := range opts {
o(opt)
}
Expand Down

0 comments on commit 7ac2179

Please sign in to comment.