forked from toolshed/abra
59 lines
1.7 KiB
Go
59 lines
1.7 KiB
Go
package stack // https://github.com/docker/cli/blob/master/cli/command/stack/loader/loader.go
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
|
|
"coopcloud.tech/abra/pkg/i18n"
|
|
composeGoCli "github.com/compose-spec/compose-go/v2/cli"
|
|
composeGoTypes "github.com/compose-spec/compose-go/v2/types"
|
|
"github.com/pkg/errors"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type LoadConf struct {
|
|
ComposeFiles []string
|
|
AppEnv map[string]string
|
|
DisableConsistencyCheck bool
|
|
}
|
|
|
|
func LoadCompose(conf LoadConf) (*composeGoTypes.Project, error) {
|
|
// NOTE(d1): silence compose-go internal logger
|
|
logrus.SetOutput(io.Discard)
|
|
if len(conf.ComposeFiles) == 0 {
|
|
return nil, errors.New(i18n.G("LoadCompose: provide compose files"))
|
|
}
|
|
|
|
hasEnvVariables := len(conf.AppEnv) > 0
|
|
newProjectOptions := []composeGoCli.ProjectOptionsFn{
|
|
composeGoCli.WithInterpolation(hasEnvVariables),
|
|
}
|
|
|
|
if hasEnvVariables {
|
|
var env []string
|
|
for k, v := range conf.AppEnv {
|
|
env = append(env, fmt.Sprintf("%s=%s", k, v))
|
|
}
|
|
newProjectOptions = append(newProjectOptions, composeGoCli.WithEnv(env))
|
|
|
|
// prefixes secrets and volumes with stack name
|
|
stackName, ok := conf.AppEnv["STACK_NAME"]
|
|
if ok && stackName != "" {
|
|
newProjectOptions = append(newProjectOptions, composeGoCli.WithName(stackName))
|
|
}
|
|
}
|
|
|
|
// this is needed for updating tags and labels, because each file is loaded one by one
|
|
// and the consistency check requires every loaded file to have an image with tag
|
|
if conf.DisableConsistencyCheck {
|
|
newProjectOptions = append(newProjectOptions, composeGoCli.WithConsistency(false))
|
|
}
|
|
|
|
projectOptions, err := composeGoCli.NewProjectOptions(conf.ComposeFiles, newProjectOptions...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return projectOptions.LoadProject(context.Background())
|
|
}
|