swirl/docker/compose/parse.go

89 lines
2.1 KiB
Go
Raw Normal View History

2017-09-26 12:50:09 +00:00
package compose
import (
"fmt"
"os"
"sort"
"strings"
2019-04-24 09:10:18 +00:00
2021-12-06 12:24:22 +00:00
composetypes "github.com/cuigh/swirl/docker/compose/types"
2017-09-26 12:50:09 +00:00
)
2019-04-24 09:10:18 +00:00
func Parse(name, content string) (*composetypes.Config, error) {
2017-09-26 12:50:09 +00:00
//absPath, err := filepath.Abs(composefile)
//if err != nil {
// return details, err
//}
//details.WorkingDir = filepath.Dir(absPath)
configFile, err := getConfigFile(name, content)
if err != nil {
return nil, err
}
env, err := buildEnvironment(os.Environ())
if err != nil {
return nil, err
}
2019-04-24 09:10:18 +00:00
details := composetypes.ConfigDetails{
ConfigFiles: []composetypes.ConfigFile{*configFile},
2017-09-26 12:50:09 +00:00
Environment: env,
}
cfg, err := Load(details)
if err != nil {
if fpe, ok := err.(*ForbiddenPropertiesError); ok {
err = fmt.Errorf("Compose file contains unsupported options:\n\n%s\n",
propertyWarnings(fpe.Properties))
}
}
return cfg, err
}
func propertyWarnings(properties map[string]string) string {
var msgs []string
for name, description := range properties {
msgs = append(msgs, fmt.Sprintf("%s: %s", name, description))
}
sort.Strings(msgs)
return strings.Join(msgs, "\n\n")
}
2019-04-24 09:10:18 +00:00
func getConfigFile(name, content string) (*composetypes.ConfigFile, error) {
2017-09-26 12:50:09 +00:00
config, err := ParseYAML([]byte(content))
if err != nil {
return nil, err
}
2019-04-24 09:10:18 +00:00
return &composetypes.ConfigFile{
2017-09-26 12:50:09 +00:00
Filename: name,
Config: config,
}, nil
}
func buildEnvironment(env []string) (map[string]string, error) {
result := make(map[string]string, len(env))
for _, s := range env {
// if value is empty, s is like "K=", not "K".
if !strings.Contains(s, "=") {
return result, fmt.Errorf("unexpected environment %q", s)
}
kv := strings.SplitN(s, "=", 2)
result[kv[0]] = kv[1]
}
return result, nil
}
2019-04-24 09:10:18 +00:00
func GetServicesDeclaredNetworks(serviceConfigs []composetypes.ServiceConfig) map[string]struct{} {
2017-09-26 12:50:09 +00:00
serviceNetworks := map[string]struct{}{}
for _, serviceConfig := range serviceConfigs {
if len(serviceConfig.Networks) == 0 {
serviceNetworks["default"] = struct{}{}
continue
}
for network := range serviceConfig.Networks {
serviceNetworks[network] = struct{}{}
}
}
return serviceNetworks
}