compose.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "bytes"
  16. "context"
  17. "encoding/json"
  18. "fmt"
  19. "strings"
  20. "github.com/docker/compose/v2/pkg/api"
  21. "github.com/compose-spec/compose-go/types"
  22. "github.com/docker/cli/cli/config/configfile"
  23. moby "github.com/docker/docker/api/types"
  24. "github.com/docker/docker/client"
  25. "github.com/sanathkr/go-yaml"
  26. )
  27. var Separator = "-"
  28. // NewComposeService create a local implementation of the compose.Service API
  29. func NewComposeService(apiClient client.APIClient, configFile *configfile.ConfigFile) api.Service {
  30. return &composeService{
  31. apiClient: apiClient,
  32. configFile: configFile,
  33. }
  34. }
  35. type composeService struct {
  36. apiClient client.APIClient
  37. configFile *configfile.ConfigFile
  38. }
  39. func getCanonicalContainerName(c moby.Container) string {
  40. // Names return container canonical name /foo + link aliases /linked_by/foo
  41. for _, name := range c.Names {
  42. if strings.LastIndex(name, "/") == 0 {
  43. return name[1:]
  44. }
  45. }
  46. return c.Names[0][1:]
  47. }
  48. func getContainerNameWithoutProject(c moby.Container) string {
  49. name := getCanonicalContainerName(c)
  50. project := c.Labels[api.ProjectLabel]
  51. prefix := fmt.Sprintf("%s_%s_", project, c.Labels[api.ServiceLabel])
  52. if strings.HasPrefix(name, prefix) {
  53. return name[len(project)+1:]
  54. }
  55. return name
  56. }
  57. func (s *composeService) Convert(ctx context.Context, project *types.Project, options api.ConvertOptions) ([]byte, error) {
  58. switch options.Format {
  59. case "json":
  60. marshal, err := json.MarshalIndent(project, "", " ")
  61. if err != nil {
  62. return nil, err
  63. }
  64. return escapeDollarSign(marshal), nil
  65. case "yaml":
  66. marshal, err := yaml.Marshal(project)
  67. if err != nil {
  68. return nil, err
  69. }
  70. return escapeDollarSign(marshal), nil
  71. default:
  72. return nil, fmt.Errorf("unsupported format %q", options)
  73. }
  74. }
  75. func escapeDollarSign(marshal []byte) []byte {
  76. dollar := []byte{'$'}
  77. escDollar := []byte{'$', '$'}
  78. return bytes.ReplaceAll(marshal, dollar, escDollar)
  79. }