compose.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. // Separator is used for naming components
  28. var Separator = "-"
  29. // NewComposeService create a local implementation of the compose.Service API
  30. func NewComposeService(apiClient client.APIClient, configFile *configfile.ConfigFile) api.Service {
  31. return &composeService{
  32. apiClient: apiClient,
  33. configFile: configFile,
  34. }
  35. }
  36. type composeService struct {
  37. apiClient client.APIClient
  38. configFile *configfile.ConfigFile
  39. }
  40. func getCanonicalContainerName(c moby.Container) string {
  41. // Names return container canonical name /foo + link aliases /linked_by/foo
  42. for _, name := range c.Names {
  43. if strings.LastIndex(name, "/") == 0 {
  44. return name[1:]
  45. }
  46. }
  47. return c.Names[0][1:]
  48. }
  49. func getContainerNameWithoutProject(c moby.Container) string {
  50. name := getCanonicalContainerName(c)
  51. project := c.Labels[api.ProjectLabel]
  52. prefix := fmt.Sprintf("%s_%s_", project, c.Labels[api.ServiceLabel])
  53. if strings.HasPrefix(name, prefix) {
  54. return name[len(project)+1:]
  55. }
  56. return name
  57. }
  58. func (s *composeService) Convert(ctx context.Context, project *types.Project, options api.ConvertOptions) ([]byte, error) {
  59. switch options.Format {
  60. case "json":
  61. marshal, err := json.MarshalIndent(project, "", " ")
  62. if err != nil {
  63. return nil, err
  64. }
  65. return escapeDollarSign(marshal), nil
  66. case "yaml":
  67. marshal, err := yaml.Marshal(project)
  68. if err != nil {
  69. return nil, err
  70. }
  71. return escapeDollarSign(marshal), nil
  72. default:
  73. return nil, fmt.Errorf("unsupported format %q", options)
  74. }
  75. }
  76. func escapeDollarSign(marshal []byte) []byte {
  77. dollar := []byte{'$'}
  78. escDollar := []byte{'$', '$'}
  79. return bytes.ReplaceAll(marshal, dollar, escDollar)
  80. }