Files
katenary/compose/parser.go
Patrice Ferlet 418a0a8029 Use compose-go + improvements (#9)
Use compose-go https://github.com/compose-spec/compose-go  to make Katenary parsing compose file the official way.
Add labels:
- `volume-from` (with `same-pod`) to avoid volume repetition
- `ignore` to ignore a service
- `mapenv` (replaces the `env-to-service`) to map environment to helm variable (as a template string)
- `secret-vars` declares variables as secret values

More:
- Now, environment (as secret vars) are set in values.yaml
- Ingress has got annotations in values.yaml
- Probes (liveness probe) are improved
- fixed code to optimize
- many others fixes about path, bad volume check, refactorisation, tests...
2022-05-08 09:55:25 +02:00

92 lines
1.8 KiB
Go

package compose
import (
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/compose-spec/compose-go/cli"
"github.com/compose-spec/compose-go/types"
)
const (
ICON_EXCLAMATION = "❕"
)
// Parser is a docker-compose parser.
type Parser struct {
Data *types.Project
temporary *string
}
var (
Appname = ""
CURRENT_DIR, _ = os.Getwd()
)
// 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 {
p := &Parser{}
if len(content) > 0 { // mainly for the tests...
dir := filepath.Dir(filename)
err := os.MkdirAll(dir, 0755)
if err != nil {
log.Fatal(err)
}
p.temporary = &dir
ioutil.WriteFile(filename, []byte(content[0]), 0644)
cli.DefaultFileNames = []string{filename}
}
// if filename is not in cli Default files, add it
if len(filename) > 0 {
found := false
for _, f := range cli.DefaultFileNames {
if f == filename {
found = true
break
}
}
// add the file at first position
if !found {
cli.DefaultFileNames = append([]string{filename}, cli.DefaultFileNames...)
}
}
return p
}
// Parse using compose-go parser, adapt a bit the Project and set Appname.
func (p *Parser) Parse(appname string) {
// Reminder:
// - set Appname
// - loas services
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("Failed to create project", err)
}
Appname = proj.Name
p.Data = proj
CURRENT_DIR = p.Data.WorkingDir
}
func GetCurrentDir() string {
return CURRENT_DIR
}