compose-go
compose-go copied to clipboard
Example usage documentation for getting started?
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:
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.
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.
@decentral1se did you get something working? If so, could you plase paste it here?
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")
}
Ended up not using it @qknight, glad you got something going!
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