compose-go icon indicating copy to clipboard operation
compose-go copied to clipboard

Label not added when lables field is not defined in `docker-compose.yml`

Open anzeha opened this issue 9 months ago • 0 comments

When docker compose file does not have labels field defined, adding labels does not work, here is the example of the code:

package main

import (
	"context"
	"fmt"
	"log"

	"github.com/compose-spec/compose-go/v2/cli"
)

func main() {
	composeFilePath := "./docker-compose-error.yml"
	projectName := "my_project"
	ctx := context.Background()

	options, err := cli.NewProjectOptions(
		[]string{composeFilePath},
		cli.WithOsEnv,
		cli.WithDotEnv,
		cli.WithName(projectName),
	)
	if err != nil {
		log.Fatal(err)
	}

	project, err := cli.ProjectFromOptions(ctx, options)
	if err != nil {
		log.Fatal(err)
	}

	// Add test: "true" label to services
	for _, svc := range project.Services {
		svc.Labels = svc.Labels.Add("test", "true")
	}

	// Print all labels of postgres service
	fmt.Println(project.Services["postgres"].Labels)
}

With the follwing docker-compose-error.yml:

services:
  postgres:
    environment:
      - POSTGRES_DB=goby_test
      - POSTGRES_USER=postgres
    image: 'postgres:9.3.17'
    ports:
      - '5432:5432'

The ouptut of this program is map[], while it should be map[test:true] as we added the test=true label.

anzeha avatar May 02 '24 16:05 anzeha