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

Example usage documentation for getting started?

Open decentral1se opened this issue 4 years ago • 2 comments

It would be great to have some few lines in the README or wherever showing how to parse/load a compose file.

I am not quite sure how to get started using this library :thinking:

decentral1se avatar Aug 02 '21 20:08 decentral1se

Oh, think I found it in compose-ref:

func load(file string) (*compose.Config, error) {
	b, err := ioutil.ReadFile(file)
	if err != nil {
		return nil, err
	}
	config, err := loader.ParseYAML(b)
	if err != nil {
		return nil, err
	}
	var files []compose.ConfigFile
	files = append(files, compose.ConfigFile{Filename: file, Config: config})
	return loader.Load(compose.ConfigDetails{
		WorkingDir:  ".",
		ConfigFiles: files,
	})
}

I can't quite get it to run right now but it seems like a good start.

decentral1se avatar Aug 02 '21 21:08 decentral1se

Just stumbled upon this as well, would like some section in README to try out but I guess the developers for this assumed golang familiarity. For what it's worth I think you can just modify some of the tests to get going.

hiimdoublej-swag avatar Jan 07 '22 08:01 hiimdoublej-swag

@decentral1se did you get something working? If so, could you plase paste it here?

qknight avatar Jun 22 '23 13:06 qknight

I got this here working:

import (
	"fmt"
	"github.com/compose-spec/compose-go/loader"
	"log"
)
import "github.com/compose-spec/compose-go/types"

func main() {
	file := "docker-compose.yaml"

	var configFiles []types.ConfigFile = []types.ConfigFile{{Filename: file}}
	var configDetails types.ConfigDetails = types.ConfigDetails{WorkingDir: ".", ConfigFiles: configFiles}

	p, e := loader.Load(configDetails)
	if e != nil {
		log.Fatalf("Failed to load file: %v", e)
	}
	fmt.Println("xxxxxxxx Name xxxxxxxxxx")
	fmt.Println(p.Name)
	fmt.Println("xxxxxxxx Services xxxxxxxxxx")
	for i := range p.Services {
		fmt.Println(p.Services[i].Name)
	}
	fmt.Println("xxxxxxxxxxxxxxxxxx")
}

qknight avatar Jun 22 '23 14:06 qknight

Ended up not using it @qknight, glad you got something going!

decentral1se avatar Jun 22 '23 14:06 decentral1se

The recommended approach is tu rely on cli.ProjectFromOptions see https://github.com/docker/compose/blob/main/cmd/compose/compose.go for a concrete example

ndeloof avatar Aug 29 '23 13:08 ndeloof