Files
katenary/compose/parser.go

77 lines
1.3 KiB
Go
Raw Normal View History

2021-11-30 12:04:28 +01:00
package compose
import (
"log"
"os"
2021-12-03 11:49:32 +01:00
"strings"
2021-11-30 12:04:28 +01:00
"github.com/compose-spec/compose-go/cli"
"github.com/compose-spec/compose-go/types"
2021-11-30 12:04:28 +01:00
"gopkg.in/yaml.v3"
)
const (
ICON_EXCLAMATION = "❕"
)
2021-11-30 15:45:36 +01:00
// Parser is a docker-compose parser.
2021-11-30 12:04:28 +01:00
type Parser struct {
Data *types.Project
2021-11-30 12:04:28 +01:00
}
var Appname = ""
// NewParser create a Parser and parse the file given in filename. If filename is empty, we try to parse the content[0] argument that should be a valid YAML content.
func NewParser(filename string, content ...string) *Parser {
2021-11-30 12:04:28 +01:00
c := NewCompose()
if filename != "" {
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
dec := yaml.NewDecoder(f)
err = dec.Decode(c)
if err != nil {
log.Fatal(err)
}
} else {
dec := yaml.NewDecoder(strings.NewReader(content[0]))
err := dec.Decode(c)
if err != nil {
log.Fatal(err)
}
}
2021-11-30 12:04:28 +01:00
p := &Parser{}
2021-11-30 12:04:28 +01:00
return p
}
func (p *Parser) Parse(appname string) {
// Reminder:
// - set Appname
// - loas services
2021-12-03 11:49:32 +01:00
options, err := cli.NewProjectOptions(nil,
cli.WithDefaultConfigPath,
cli.WithNormalization(true),
cli.WithInterpolation(true),
cli.WithResolvedPaths(true),
)
if err != nil {
log.Fatal(err)
}
proj, err := cli.ProjectFromOptions(options)
if err != nil {
log.Fatal(err)
2021-12-03 11:49:32 +01:00
}
Appname = proj.Name
p.Data = proj
}