Remove duplication in loader

Signed-off-by: Daniel Nephin <dnephin@docker.com>
This commit is contained in:
Daniel Nephin 2017-11-06 17:30:10 -05:00
parent 70da09aac2
commit 7bd26ed690
2 changed files with 26 additions and 19 deletions

View File

@ -112,14 +112,14 @@ func loadSections(config map[string]interface{}, configDetails types.ConfigDetai
{ {
key: "secrets", key: "secrets",
fnc: func(config map[string]interface{}) error { fnc: func(config map[string]interface{}) error {
cfg.Secrets, err = LoadSecrets(config, configDetails.WorkingDir) cfg.Secrets, err = LoadSecrets(config, configDetails)
return err return err
}, },
}, },
{ {
key: "configs", key: "configs",
fnc: func(config map[string]interface{}) error { fnc: func(config map[string]interface{}) error {
cfg.Configs, err = LoadConfigObjs(config, configDetails.WorkingDir) cfg.Configs, err = LoadConfigObjs(config, configDetails)
return err return err
}, },
}, },
@ -484,42 +484,48 @@ func LoadVolumes(source map[string]interface{}, version string) (map[string]type
// LoadSecrets produces a SecretConfig map from a compose file Dict // LoadSecrets produces a SecretConfig map from a compose file Dict
// the source Dict is not validated if directly used. Use Load() to enable validation // the source Dict is not validated if directly used. Use Load() to enable validation
func LoadSecrets(source map[string]interface{}, workingDir string) (map[string]types.SecretConfig, error) { func LoadSecrets(source map[string]interface{}, details types.ConfigDetails) (map[string]types.SecretConfig, error) {
secrets := make(map[string]types.SecretConfig) secrets := make(map[string]types.SecretConfig)
if err := transform(source, &secrets); err != nil { if err := transform(source, &secrets); err != nil {
return secrets, err return secrets, err
} }
for name, secret := range secrets { for name, secret := range secrets {
if secret.External.External && secret.External.Name == "" { obj, err := loadFileObjectConfig(name, types.FileObjectConfig(secret), details)
secret.External.Name = name if err != nil {
secrets[name] = secret return nil, err
}
if secret.File != "" {
secret.File = absPath(workingDir, secret.File)
} }
secrets[name] = types.SecretConfig(obj)
} }
return secrets, nil return secrets, nil
} }
// LoadConfigObjs produces a ConfigObjConfig map from a compose file Dict // LoadConfigObjs produces a ConfigObjConfig map from a compose file Dict
// the source Dict is not validated if directly used. Use Load() to enable validation // the source Dict is not validated if directly used. Use Load() to enable validation
func LoadConfigObjs(source map[string]interface{}, workingDir string) (map[string]types.ConfigObjConfig, error) { func LoadConfigObjs(source map[string]interface{}, details types.ConfigDetails) (map[string]types.ConfigObjConfig, error) {
configs := make(map[string]types.ConfigObjConfig) configs := make(map[string]types.ConfigObjConfig)
if err := transform(source, &configs); err != nil { if err := transform(source, &configs); err != nil {
return configs, err return configs, err
} }
for name, config := range configs { for name, config := range configs {
if config.External.External && config.External.Name == "" { obj, err := loadFileObjectConfig(name, types.FileObjectConfig(config), details)
config.External.Name = name if err != nil {
configs[name] = config return nil, err
}
if config.File != "" {
config.File = absPath(workingDir, config.File)
} }
configs[name] = types.ConfigObjConfig(obj)
} }
return configs, nil return configs, nil
} }
func loadFileObjectConfig(name string, obj types.FileObjectConfig, details types.ConfigDetails) (types.FileObjectConfig, error) {
if obj.External.External && obj.External.Name == "" {
obj.External.Name = name
}
if obj.File != "" {
obj.File = absPath(details.WorkingDir, obj.File)
}
return obj, nil
}
func absPath(workingDir string, filePath string) string { func absPath(workingDir string, filePath string) string {
if filepath.IsAbs(filePath) { if filepath.IsAbs(filePath) {
return filePath return filePath

View File

@ -344,14 +344,15 @@ type CredentialSpecConfig struct {
Registry string Registry string
} }
type fileObjectConfig struct { // FileObjectConfig is a config type for a file used by a service
type FileObjectConfig struct {
File string File string
External External External External
Labels Labels Labels Labels
} }
// SecretConfig for a secret // SecretConfig for a secret
type SecretConfig fileObjectConfig type SecretConfig FileObjectConfig
// ConfigObjConfig is the config for the swarm "Config" object // ConfigObjConfig is the config for the swarm "Config" object
type ConfigObjConfig fileObjectConfig type ConfigObjConfig FileObjectConfig