Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
Nicolas De Loof 2024-05-16 17:14:42 +02:00 коммит произвёл Nicolas De loof
Родитель 9e8c8caa2b
Коммит 5c6924ec6f
4 изменённых файлов: 62 добавлений и 40 удалений

Просмотреть файл

@ -143,47 +143,15 @@ func configCommand(p *ProjectOptions, dockerCli command.Cli) *cobra.Command {
return cmd
}
func runConfig(ctx context.Context, dockerCli command.Cli, opts configOptions, services []string) error {
func runConfig(ctx context.Context, dockerCli command.Cli, opts configOptions, services []string) (err error) {
var content []byte
if opts.noInterpolate {
// we can't use ToProject, so the model we render here is only partially resolved
model, err := opts.ToModel(ctx, dockerCli, services)
if err != nil {
return err
}
if opts.resolveImageDigests {
err = resolveImageDigests(ctx, dockerCli, model)
if err != nil {
return err
}
}
content, err = formatModel(model, opts.Format)
content, err = runConfigNoInterpolate(ctx, dockerCli, opts, services)
if err != nil {
return err
}
} else {
project, err := opts.ToProject(ctx, dockerCli, services)
if err != nil {
return err
}
if !opts.noConsistency {
err := project.CheckContainerNameUnicity()
if err != nil {
return err
}
}
switch opts.Format {
case "json":
content, err = project.MarshalJSON()
case "yaml":
content, err = project.MarshalYAML()
default:
return fmt.Errorf("unsupported format %q", opts.Format)
}
content, err = runConfigInterpolate(ctx, dockerCli, opts, services)
if err != nil {
return err
}
@ -200,10 +168,62 @@ func runConfig(ctx context.Context, dockerCli command.Cli, opts configOptions, s
if opts.Output != "" && len(content) > 0 {
return os.WriteFile(opts.Output, content, 0o666)
}
_, err := fmt.Fprint(dockerCli.Out(), string(content))
_, err = fmt.Fprint(dockerCli.Out(), string(content))
return err
}
func runConfigInterpolate(ctx context.Context, dockerCli command.Cli, opts configOptions, services []string) ([]byte, error) {
project, err := opts.ToProject(ctx, dockerCli, services)
if err != nil {
return nil, err
}
if opts.resolveImageDigests {
project, err = project.WithImagesResolved(compose.ImageDigestResolver(ctx, dockerCli.ConfigFile(), dockerCli.Client()))
if err != nil {
return nil, err
}
}
if !opts.noConsistency {
err := project.CheckContainerNameUnicity()
if err != nil {
return nil, err
}
}
var content []byte
switch opts.Format {
case "json":
content, err = project.MarshalJSON()
case "yaml":
content, err = project.MarshalYAML()
default:
return nil, fmt.Errorf("unsupported format %q", opts.Format)
}
if err != nil {
return nil, err
}
return content, nil
}
func runConfigNoInterpolate(ctx context.Context, dockerCli command.Cli, opts configOptions, services []string) ([]byte, error) {
// we can't use ToProject, so the model we render here is only partially resolved
model, err := opts.ToModel(ctx, dockerCli, services)
if err != nil {
return nil, err
}
if opts.resolveImageDigests {
err = resolveImageDigests(ctx, dockerCli, model)
if err != nil {
return nil, err
}
}
return formatModel(model, opts.Format)
}
func resolveImageDigests(ctx context.Context, dockerCli command.Cli, model map[string]any) (err error) {
// create a pseudo-project so we can rely on WithImagesResolved to resolve images
p := &types.Project{
@ -342,6 +362,7 @@ func runConfigImages(ctx context.Context, dockerCli command.Cli, opts configOpti
if err != nil {
return err
}
for _, s := range project.Services {
fmt.Fprintln(dockerCli.Out(), api.GetImageNameOrDefault(s, project.Name))
}

2
go.mod
Просмотреть файл

@ -7,7 +7,7 @@ require (
github.com/Microsoft/go-winio v0.6.2
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
github.com/buger/goterm v1.0.4
github.com/compose-spec/compose-go/v2 v2.1.0
github.com/compose-spec/compose-go/v2 v2.1.1-0.20240516145816-197e6352c64a
github.com/containerd/console v1.0.4
github.com/containerd/containerd v1.7.16
github.com/davecgh/go-spew v1.1.1

4
go.sum
Просмотреть файл

@ -90,8 +90,8 @@ github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4 h1:/inchEIKaYC1Akx+H+g
github.com/cncf/xds/go v0.0.0-20230607035331-e9ce68804cb4/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb h1:EDmT6Q9Zs+SbUoc7Ik9EfrFqcylYqgPZ9ANSbTAntnE=
github.com/codahale/rfc6979 v0.0.0-20141003034818-6a90f24967eb/go.mod h1:ZjrT6AXHbDs86ZSdt/osfBi5qfexBrKUdONk989Wnk4=
github.com/compose-spec/compose-go/v2 v2.1.0 h1:qdW2qISQlCQG8v1O2TChcdxgAWTUGgUX/CPSO+ES9+E=
github.com/compose-spec/compose-go/v2 v2.1.0/go.mod h1:bEPizBkIojlQ20pi2vNluBa58tevvj0Y18oUSHPyfdc=
github.com/compose-spec/compose-go/v2 v2.1.1-0.20240516145816-197e6352c64a h1:R0ufvPulvRvFa8JaFEWzSJmkhjfeArXD9vKk0jpmpM4=
github.com/compose-spec/compose-go/v2 v2.1.1-0.20240516145816-197e6352c64a/go.mod h1:bEPizBkIojlQ20pi2vNluBa58tevvj0Y18oUSHPyfdc=
github.com/containerd/cgroups v1.1.0 h1:v8rEWFl6EoqHB+swVNjVoCJE8o3jX7e8nqBGPLaDFBM=
github.com/containerd/cgroups v1.1.0/go.mod h1:6ppBcbh/NOOUU+dMKrykgaBnK9lCIBxHqJDGwsa1mIw=
github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro=

Просмотреть файл

@ -264,7 +264,8 @@ func ImageDigestResolver(ctx context.Context, file *configfile.ConfigFile, apiCl
}
inspect, err := apiClient.DistributionInspect(ctx, named.String(), auth)
if err != nil {
return "", err
return "",
fmt.Errorf("failed ot resolve digest for %s: %w", named.String(), err)
}
return inspect.Descriptor.Digest, nil
}