github.com/containerd/nerdctl@v1.7.7/pkg/composer/config.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 /* 18 Portions from https://github.com/docker/compose/blob/v2.2.2/pkg/compose/hash.go 19 Copyright (C) Docker Compose authors. 20 Licensed under the Apache License, Version 2.0 21 NOTICE: https://github.com/docker/compose/blob/v2.2.2/NOTICE 22 */ 23 24 package composer 25 26 import ( 27 "context" 28 "encoding/json" 29 "fmt" 30 "io" 31 "strings" 32 33 "github.com/compose-spec/compose-go/types" 34 "github.com/opencontainers/go-digest" 35 "gopkg.in/yaml.v3" 36 ) 37 38 type ConfigOptions struct { 39 Services bool 40 Volumes bool 41 Hash string 42 } 43 44 func (c *Composer) Config(ctx context.Context, w io.Writer, co ConfigOptions) error { 45 if co.Services { 46 for _, service := range c.project.Services { 47 fmt.Fprintln(w, service.Name) 48 } 49 return nil 50 } 51 if co.Volumes { 52 for volume := range c.project.Volumes { 53 fmt.Fprintln(w, volume) 54 } 55 return nil 56 } 57 if co.Hash != "" { 58 var services []string 59 if co.Hash != "*" { 60 services = strings.Split(co.Hash, ",") 61 } 62 return c.project.WithServices(services, func(svc types.ServiceConfig) error { 63 hash, err := ServiceHash(svc) 64 if err != nil { 65 return err 66 } 67 fmt.Fprintf(w, "%s %s\n", svc.Name, hash) 68 return nil 69 }) 70 } 71 projectYAML, err := yaml.Marshal(c.project) 72 if err != nil { 73 return err 74 } 75 fmt.Fprintf(w, "%s", projectYAML) 76 return nil 77 } 78 79 // ServiceHash is from https://github.com/docker/compose/blob/v2.2.2/pkg/compose/hash.go#L28-L38 80 func ServiceHash(o types.ServiceConfig) (string, error) { 81 // remove the Build config when generating the service hash 82 o.Build = nil 83 o.PullPolicy = "" 84 o.Scale = 1 85 bytes, err := json.Marshal(o) 86 if err != nil { 87 return "", err 88 } 89 return digest.SHA256.FromBytes(bytes).Encoded(), nil 90 }