Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce config --built-images #2

Merged
merged 1 commit into from
Dec 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cmd/compose/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)")

Expand Down Expand Up @@ -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))
}
}
Expand Down
80 changes: 80 additions & 0 deletions cmd/compose/convert_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}