From 9ef54530c9019d63656a4568a94db90ddee84a49 Mon Sep 17 00:00:00 2001 From: abdennour Date: Sat, 4 Dec 2021 20:54:15 +0300 Subject: [PATCH] introduce config --built-images It solves #8994 Signed-off-by: abdennour --- cmd/compose/convert.go | 8 ++-- cmd/compose/convert_test.go | 80 +++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 cmd/compose/convert_test.go diff --git a/cmd/compose/convert.go b/cmd/compose/convert.go index bdeab2f326f..7c597783237 100644 --- a/cmd/compose/convert.go +++ b/cmd/compose/convert.go @@ -50,7 +50,7 @@ type convertOptions struct { profiles bool images bool hash string - onlyBuilt bool + builtImages bool } func convertCommand(p *projectOptions, backend api.Service) *cobra.Command { @@ -87,7 +87,7 @@ func convertCommand(p *projectOptions, backend api.Service) *cobra.Command { if opts.profiles { return runProfiles(opts, args) } - if opts.images { + if opts.images || opts.builtImages { return runConfigImages(opts, args) } @@ -106,7 +106,7 @@ func convertCommand(p *projectOptions, backend api.Service) *cobra.Command { flags.BoolVar(&opts.volumes, "volumes", false, "Print the volume names, one per line.") flags.BoolVar(&opts.profiles, "profiles", false, "Print the profile names, one per line.") flags.BoolVar(&opts.images, "images", false, "Print the image names, one per line.") - flags.BoolVar(&opts.onlyBuilt, "only-built", false, "filter only services which uses built images") + flags.BoolVar(&opts.builtImages, "built-images", false, "Print only built image names, one per line") flags.StringVar(&opts.hash, "hash", "", "Print the service config hash, one per line.") flags.StringVarP(&opts.Output, "output", "o", "", "Save to file (default to stdout)") @@ -229,7 +229,7 @@ func runConfigImages(opts convertOptions, services []string) error { return err } for _, s := range project.Services { - if !opts.onlyBuilt || (opts.onlyBuilt && s.Build != nil) { + if !opts.builtImages || (opts.builtImages && s.Build != nil) { fmt.Println(getImageName(s, *project)) } } diff --git a/cmd/compose/convert_test.go b/cmd/compose/convert_test.go new file mode 100644 index 00000000000..463b9fd3a84 --- /dev/null +++ b/cmd/compose/convert_test.go @@ -0,0 +1,80 @@ +/* + Copyright 2020 Docker Compose CLI authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package compose + +import ( + "testing" + + "github.com/compose-spec/compose-go/types" +) + +func Test_getImageName(t *testing.T) { + type args struct { + s types.ServiceConfig + p types.Project + } + tests := []struct { + name string + args args + want string + }{ + { + name: "image not mentioned explicitly", + args: args{ + s: types.ServiceConfig{ + Name: "s1", + }, + p: types.Project{ + Name: "p1", + }, + }, + want: "p1_s1", + }, + { + name: "another image not mentioned explicitly", + args: args{ + s: types.ServiceConfig{ + Name: "frontend", + }, + p: types.Project{ + Name: "devops", + }, + }, + want: "devops_frontend", + }, + { + name: "image mentioned explicitly", + args: args{ + s: types.ServiceConfig{ + Name: "s1", + Image: "myimage:mytag", + }, + p: types.Project{ + Name: "p1", + }, + }, + want: "myimage:mytag", + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := getImageName(tt.args.s, tt.args.p); got != tt.want { + t.Errorf("getImageName() = %v, want %v", got, tt.want) + } + }) + } +}