mirror of
https://github.com/rancher/os.git
synced 2025-05-02 13:23:23 +00:00
56 lines
1.2 KiB
Go
56 lines
1.2 KiB
Go
|
package docker
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"strings"
|
||
|
|
||
|
"github.com/docker/libcompose/project"
|
||
|
"github.com/rancherio/os/config"
|
||
|
)
|
||
|
|
||
|
type ConfigEnvironment struct {
|
||
|
cfg *config.CloudConfig
|
||
|
}
|
||
|
|
||
|
func NewConfigEnvironment(cfg *config.CloudConfig) *ConfigEnvironment {
|
||
|
return &ConfigEnvironment{
|
||
|
cfg: cfg,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func appendEnv(array []string, key, value string) []string {
|
||
|
parts := strings.SplitN(key, "/", 2)
|
||
|
if len(parts) == 2 {
|
||
|
key = parts[1]
|
||
|
}
|
||
|
|
||
|
return append(array, fmt.Sprintf("%s=%s", key, value))
|
||
|
}
|
||
|
|
||
|
func lookupKeys(cfg *config.CloudConfig, keys ...string) []string {
|
||
|
for _, key := range keys {
|
||
|
if strings.HasSuffix(key, "*") {
|
||
|
result := []string{}
|
||
|
for envKey, envValue := range cfg.Rancher.Environment {
|
||
|
keyPrefix := key[:len(key)-1]
|
||
|
if strings.HasPrefix(envKey, keyPrefix) {
|
||
|
result = appendEnv(result, envKey, envValue)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if len(result) > 0 {
|
||
|
return result
|
||
|
}
|
||
|
} else if value, ok := cfg.Rancher.Environment[key]; ok {
|
||
|
return appendEnv([]string{}, key, value)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return []string{}
|
||
|
}
|
||
|
|
||
|
func (c *ConfigEnvironment) Lookup(key, serviceName string, serviceConfig *project.ServiceConfig) []string {
|
||
|
fullKey := fmt.Sprintf("%s/%s", serviceName, key)
|
||
|
return lookupKeys(c.cfg, fullKey, key)
|
||
|
}
|