Skip to content
This repository was archived by the owner on Nov 27, 2023. It is now read-only.

Commit 6562ec8

Browse files
authored
Merge pull request #1444 from docker/config_flags
introduce config --services, --volumes, --hash for backward compatibility
2 parents 9ace15c + ec5489a commit 6562ec8

File tree

7 files changed

+87
-27
lines changed

7 files changed

+87
-27
lines changed

cli/cmd/compose/convert.go

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"fmt"
2323
"io"
2424
"os"
25+
"strings"
2526

2627
"github.com/cnabio/cnab-to-oci/remotes"
2728
"github.com/distribution/distribution/v3/reference"
@@ -32,14 +33,18 @@ import (
3233
"github.com/docker/compose-cli/api/client"
3334
"github.com/docker/compose-cli/api/compose"
3435
"github.com/docker/compose-cli/api/config"
36+
"github.com/docker/compose-cli/utils"
3537
)
3638

3739
type convertOptions struct {
3840
*projectOptions
39-
Format string
40-
Output string
41-
quiet bool
42-
resolve bool
41+
Format string
42+
Output string
43+
quiet bool
44+
resolve bool
45+
services bool
46+
volumes bool
47+
hash string
4348
}
4449

4550
var addFlagsFuncs []func(cmd *cobra.Command, opts *convertOptions)
@@ -60,6 +65,16 @@ func convertCommand(p *projectOptions) *cobra.Command {
6065
}
6166
os.Stdout = devnull
6267
}
68+
if opts.services {
69+
return runServices(opts)
70+
}
71+
if opts.volumes {
72+
return runVolumes(opts)
73+
}
74+
if opts.hash != "" {
75+
return runHash(opts)
76+
}
77+
6378
return runConvert(cmd.Context(), opts, args)
6479
},
6580
}
@@ -68,6 +83,10 @@ func convertCommand(p *projectOptions) *cobra.Command {
6883
flags.BoolVar(&opts.resolve, "resolve-image-digests", false, "Pin image tags to digests.")
6984
flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only validate the configuration, don't print anything.")
7085

86+
flags.BoolVar(&opts.services, "services", false, "Print the service names, one per line.")
87+
flags.BoolVar(&opts.volumes, "volumes", false, "Print the volume names, one per line.")
88+
flags.StringVar(&opts.hash, "hash", "", "Print the service config hash, one per line.")
89+
7190
// add flags for hidden backends
7291
for _, f := range addFlagsFuncs {
7392
f(cmd, &opts)
@@ -126,3 +145,44 @@ func runConvert(ctx context.Context, opts convertOptions, services []string) err
126145
_, err = fmt.Fprint(out, string(json))
127146
return err
128147
}
148+
149+
func runServices(opts convertOptions) error {
150+
project, err := opts.toProject(nil)
151+
if err != nil {
152+
return err
153+
}
154+
for _, s := range project.Services {
155+
fmt.Println(s.Name)
156+
}
157+
return nil
158+
}
159+
160+
func runVolumes(opts convertOptions) error {
161+
project, err := opts.toProject(nil)
162+
if err != nil {
163+
return err
164+
}
165+
for _, v := range project.Volumes {
166+
fmt.Println(v.Name)
167+
}
168+
return nil
169+
}
170+
171+
func runHash(opts convertOptions) error {
172+
var services []string
173+
if opts.hash != "*" {
174+
services = append(services, strings.Split(opts.hash, ",")...)
175+
}
176+
project, err := opts.toProject(services)
177+
if err != nil {
178+
return err
179+
}
180+
for _, s := range project.Services {
181+
hash, err := utils.ServiceHash(s)
182+
if err != nil {
183+
return err
184+
}
185+
fmt.Printf("%s %s\n", s.Name, hash)
186+
}
187+
return nil
188+
}

local/compose/containers.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"github.com/compose-spec/compose-go/types"
2525
moby "github.com/docker/docker/api/types"
2626
"github.com/docker/docker/api/types/filters"
27+
28+
"github.com/docker/compose-cli/utils"
2729
)
2830

2931
// Containers is a set of moby Container
@@ -69,14 +71,14 @@ type containerPredicate func(c moby.Container) bool
6971
func isService(services ...string) containerPredicate {
7072
return func(c moby.Container) bool {
7173
service := c.Labels[serviceLabel]
72-
return contains(services, service)
74+
return utils.StringContains(services, service)
7375
}
7476
}
7577

7678
func isNotService(services ...string) containerPredicate {
7779
return func(c moby.Container) bool {
7880
service := c.Labels[serviceLabel]
79-
return !contains(services, service)
81+
return !utils.StringContains(services, service)
8082
}
8183
}
8284

local/compose/convergence.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333
"github.com/docker/compose-cli/api/compose"
3434
"github.com/docker/compose-cli/api/progress"
3535
status "github.com/docker/compose-cli/local/moby"
36+
"github.com/docker/compose-cli/utils"
3637
)
3738

3839
const (
@@ -97,7 +98,7 @@ func (s *composeService) ensureService(ctx context.Context, project *types.Proje
9798
return nil
9899
}
99100

100-
expected, err := jsonHash(service)
101+
expected, err := utils.ServiceHash(service)
101102
if err != nil {
102103
return err
103104
}
@@ -249,7 +250,7 @@ func (s *composeService) recreateContainer(ctx context.Context, project *types.P
249250
// setDependentLifecycle define the Lifecycle strategy for all services to depend on specified service
250251
func setDependentLifecycle(project *types.Project, service string, strategy string) {
251252
for i, s := range project.Services {
252-
if contains(s.GetDependencies(), service) {
253+
if utils.StringContains(s.GetDependencies(), service) {
253254
if s.Extensions == nil {
254255
s.Extensions = map[string]interface{}{}
255256
}

local/compose/create.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ import (
4040
"github.com/docker/compose-cli/api/compose"
4141
"github.com/docker/compose-cli/api/progress"
4242
convert "github.com/docker/compose-cli/local/moby"
43+
"github.com/docker/compose-cli/utils"
4344
)
4445

4546
func (s *composeService) Create(ctx context.Context, project *types.Project, opts compose.CreateOptions) error {
@@ -102,7 +103,7 @@ func (s *composeService) Create(ctx context.Context, project *types.Project, opt
102103
prepareNetworkMode(project)
103104

104105
return InDependencyOrder(ctx, project, func(c context.Context, service types.ServiceConfig) error {
105-
if contains(opts.Services, service.Name) {
106+
if utils.StringContains(opts.Services, service.Name) {
106107
return s.ensureService(c, project, service, opts.Recreate, opts.Inherit, opts.Timeout)
107108
}
108109
return s.ensureService(c, project, service, opts.RecreateDependencies, opts.Inherit, opts.Timeout)
@@ -121,7 +122,7 @@ func prepareVolumes(p *types.Project) error {
121122
p.Services[i].DependsOn = make(types.DependsOnConfig, len(dependServices))
122123
}
123124
for _, service := range p.Services {
124-
if contains(dependServices, service.Name) {
125+
if utils.StringContains(dependServices, service.Name) {
125126
p.Services[i].DependsOn[service.Name] = types.ServiceDependency{
126127
Condition: types.ServiceConditionStarted,
127128
}
@@ -196,7 +197,7 @@ func getImageName(service types.ServiceConfig, projectName string) string {
196197
func (s *composeService) getCreateOptions(ctx context.Context, p *types.Project, service types.ServiceConfig, number int, inherit *moby.Container,
197198
autoRemove bool) (*container.Config, *container.HostConfig, *network.NetworkingConfig, error) {
198199

199-
hash, err := jsonHash(service)
200+
hash, err := utils.ServiceHash(service)
200201
if err != nil {
201202
return nil, nil, nil, err
202203
}

local/compose/dependencies.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424

2525
"github.com/compose-spec/compose-go/types"
2626
"golang.org/x/sync/errgroup"
27+
28+
"github.com/docker/compose-cli/utils"
2729
)
2830

2931
// ServiceStatus indicates the status of a service
@@ -313,7 +315,7 @@ func (g *Graph) HasCycles() (bool, error) {
313315
path := []string{
314316
vertex.Key,
315317
}
316-
if !contains(discovered, vertex.Key) && !contains(finished, vertex.Key) {
318+
if !utils.StringContains(discovered, vertex.Key) && !utils.StringContains(finished, vertex.Key) {
317319
var err error
318320
discovered, finished, err = g.visit(vertex.Key, path, discovered, finished)
319321

@@ -331,11 +333,11 @@ func (g *Graph) visit(key string, path []string, discovered []string, finished [
331333

332334
for _, v := range g.Vertices[key].Children {
333335
path := append(path, v.Key)
334-
if contains(discovered, v.Key) {
336+
if utils.StringContains(discovered, v.Key) {
335337
return nil, nil, fmt.Errorf("cycle found: %s", strings.Join(path, " -> "))
336338
}
337339

338-
if !contains(finished, v.Key) {
340+
if !utils.StringContains(finished, v.Key) {
339341
if _, _, err := g.visit(v.Key, path, discovered, finished); err != nil {
340342
return nil, nil, err
341343
}

local/compose/logs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (s *composeService) Logs(ctx context.Context, projectName string, consumer
4343
}
4444
if len(options.Services) > 0 {
4545
ignore = func(s string) bool {
46-
return !contains(options.Services, s)
46+
return !utils.StringContains(options.Services, s)
4747
}
4848
}
4949

local/compose/util.go renamed to utils/hash.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,21 @@
1414
limitations under the License.
1515
*/
1616

17-
package compose
17+
package utils
1818

1919
import (
2020
"encoding/json"
2121

22+
"github.com/compose-spec/compose-go/types"
2223
"github.com/opencontainers/go-digest"
2324
)
2425

25-
func jsonHash(o interface{}) (string, error) {
26+
// ServiceHash compute configuration has for a service
27+
// TODO move this to compose-go
28+
func ServiceHash(o types.ServiceConfig) (string, error) {
2629
bytes, err := json.Marshal(o)
2730
if err != nil {
2831
return "", err
2932
}
30-
return digest.SHA256.FromBytes(bytes).String(), nil
31-
}
32-
33-
func contains(slice []string, item string) bool {
34-
for _, v := range slice {
35-
if v == item {
36-
return true
37-
}
38-
}
39-
return false
33+
return digest.SHA256.FromBytes(bytes).Encoded(), nil
4034
}

0 commit comments

Comments
 (0)