Skip to content

Commit

Permalink
implement publish
Browse files Browse the repository at this point in the history
Signed-off-by: Nicolas De Loof <[email protected]>
  • Loading branch information
ndeloof committed Sep 17, 2023
1 parent a697a06 commit ead0ce4
Showing 1 changed file with 116 additions and 3 deletions.
119 changes: 116 additions & 3 deletions pkg/compose/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,137 @@ package compose

import (
"context"
"encoding/json"
"os"

"github.com/compose-spec/compose-go/types"
"github.com/distribution/reference"
"github.com/docker/buildx/util/imagetools"
"github.com/docker/compose/v2/pkg/api"
"github.com/docker/compose/v2/pkg/progress"
"github.com/opencontainers/go-digest"
"github.com/opencontainers/image-spec/specs-go"
v1 "github.com/opencontainers/image-spec/specs-go/v1"
)

func (s *composeService) Publish(ctx context.Context, project *types.Project, repository string, options api.PublishOptions) error {
return progress.RunWithTitle(ctx, func(ctx context.Context) error {
return s.publish(ctx, project, repository, options)
}, s.stdinfo(), "Publishing")

Check warning on line 37 in pkg/compose/publish.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/publish.go#L35-L37

Added lines #L35 - L37 were not covered by tests
}

func (s *composeService) publish(ctx context.Context, project *types.Project, repository string, options api.PublishOptions) error {

Check warning on line 40 in pkg/compose/publish.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/publish.go#L40

Added line #L40 was not covered by tests
err := s.Push(ctx, project, api.PushOptions{})
if err != nil {
return err
}

_, err = reference.ParseDockerRef(repository)
w := progress.ContextWriter(ctx)

named, err := reference.ParseDockerRef(repository)
if err != nil {
return err
}

Check warning on line 51 in pkg/compose/publish.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/publish.go#L46-L51

Added lines #L46 - L51 were not covered by tests

resolver := imagetools.New(imagetools.Opt{
Auth: s.configFile(),
})

var layers []v1.Descriptor
for _, file := range project.ComposeFiles {
f, err := os.ReadFile(file)
if err != nil {
return err
}

Check warning on line 62 in pkg/compose/publish.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/publish.go#L53-L62

Added lines #L53 - L62 were not covered by tests

w.Event(progress.Event{
ID: file,
Text: "publishing",
Status: progress.Working,
})
layer := v1.Descriptor{
MediaType: v1.MediaTypeImageLayer,
Digest: digest.FromString(string(f)),
Size: int64(len(f)),
Annotations: map[string]string{
"com.docker.compose": api.ComposeVersion,
},
ArtifactType: "application/vnd.docker.compose.file+yaml",
}
layers = append(layers, layer)
err = resolver.Push(ctx, named, layer, f)
if err != nil {
w.Event(progress.Event{
ID: file,
Text: "publishing",
Status: progress.Error,
})

return err
}

Check warning on line 88 in pkg/compose/publish.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/publish.go#L64-L88

Added lines #L64 - L88 were not covered by tests

w.Event(progress.Event{
ID: file,
Text: "published",
Status: progress.Done,
})

Check warning on line 94 in pkg/compose/publish.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/publish.go#L90-L94

Added lines #L90 - L94 were not covered by tests
}

emptyConfig, err := json.Marshal(v1.ImageConfig{})
if err != nil {
return err
}
configDescriptor := v1.Descriptor{
MediaType: v1.MediaTypeImageConfig,
Digest: digest.FromBytes(emptyConfig),
Size: int64(len(emptyConfig)),
Annotations: map[string]string{
"com.docker.compose.version": api.ComposeVersion,
},
}
err = resolver.Push(ctx, named, configDescriptor, emptyConfig)
if err != nil {
return err
}

Check warning on line 112 in pkg/compose/publish.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/publish.go#L97-L112

Added lines #L97 - L112 were not covered by tests

imageManifest, err := json.Marshal(v1.Manifest{
Versioned: specs.Versioned{SchemaVersion: 2},
MediaType: v1.MediaTypeImageManifest,
ArtifactType: "application/vnd.docker.compose.project",
Config: configDescriptor,
Layers: layers,
})

Check warning on line 120 in pkg/compose/publish.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/publish.go#L114-L120

Added lines #L114 - L120 were not covered by tests
if err != nil {
return err
}

// TODO publish project.ComposeFiles
w.Event(progress.Event{
ID: repository,
Text: "publishing",
Status: progress.Working,
})

Check warning on line 129 in pkg/compose/publish.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/publish.go#L125-L129

Added lines #L125 - L129 were not covered by tests

return api.ErrNotImplemented
err = resolver.Push(ctx, named, v1.Descriptor{
MediaType: v1.MediaTypeImageManifest,
Digest: digest.FromString(string(imageManifest)),
Size: int64(len(imageManifest)),
Annotations: map[string]string{
"com.docker.compose.version": api.ComposeVersion,
},
ArtifactType: "application/vnd.docker.compose.project",
}, imageManifest)
if err != nil {
w.Event(progress.Event{
ID: repository,
Text: "publishing",
Status: progress.Error,
})
return err
}
w.Event(progress.Event{
ID: repository,
Text: "published",
Status: progress.Done,
})
return nil

Check warning on line 153 in pkg/compose/publish.go

View check run for this annotation

Codecov / codecov/patch

pkg/compose/publish.go#L131-L153

Added lines #L131 - L153 were not covered by tests
}

0 comments on commit ead0ce4

Please sign in to comment.