forked from palantir/distgo
/
configdist.go
163 lines (143 loc) · 5.66 KB
/
configdist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2016 Palantir Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package config
import (
"path"
"github.com/palantir/pkg/matcher"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
"github.com/palantir/distgo/distgo"
"github.com/palantir/distgo/distgo/config/internal/v0"
)
type DistConfig v0.DistConfig
func ToDistConfig(in *DistConfig) *v0.DistConfig {
return (*v0.DistConfig)(in)
}
// ToParam returns the DistParam represented by the receiver *DisterConfig and the provided default DisterConfig. If a
// config value is specified (non-nil) in the receiver config, it is used. If a config value is not specified in the
// receiver config but is specified in the default config, the default config value is used. If a value is not specified
// in either configuration, the program-specified default value (if any) is used.
func (cfg *DistConfig) ToParam(scriptIncludes string, defaultCfg DistConfig, disterFactory distgo.DisterFactory) (distgo.DistParam, error) {
outputDir := getConfigStringValue(cfg.OutputDir, defaultCfg.OutputDir, "out/dist")
if path.IsAbs(outputDir) {
return distgo.DistParam{}, errors.Errorf("output-dir cannot be specified as an absolute path")
}
disters, err := (*DistersConfig)(cfg.Disters).ToParam((*DistersConfig)(defaultCfg.Disters), scriptIncludes, disterFactory)
if err != nil {
return distgo.DistParam{}, err
}
return distgo.DistParam{
OutputDir: outputDir,
DistParams: disters,
}, nil
}
type DisterConfig v0.DisterConfig
func ToDisterConfig(in DisterConfig) v0.DisterConfig {
return (v0.DisterConfig)(in)
}
func (cfg *DisterConfig) ToParam(defaultCfg DisterConfig, scriptIncludes string, disterFactory distgo.DisterFactory) (distgo.DisterParam, error) {
disterType := getConfigStringValue(cfg.Type, defaultCfg.Type, "")
if disterType == "" {
return distgo.DisterParam{}, errors.Errorf("dister type must be specified for DisterConfig")
}
dister, err := newDister(disterType, getConfigValue(cfg.Config, defaultCfg.Config, nil).(yaml.MapSlice), disterFactory)
if err != nil {
return distgo.DisterParam{}, err
}
inputDirCfg := getConfigValue((*InputDirConfig)(cfg.InputDir), (*InputDirConfig)(defaultCfg.InputDir), InputDirConfig{}).(InputDirConfig)
return distgo.DisterParam{
NameTemplate: getConfigStringValue(cfg.NameTemplate, defaultCfg.NameTemplate, "{{Product}}-{{Version}}"),
InputDir: inputDirCfg.ToParam(),
Script: distgo.CreateScriptContent(getConfigStringValue(cfg.Script, defaultCfg.Script, ""), scriptIncludes),
Dister: dister,
}, nil
}
type InputDirConfig v0.InputDirConfig
func ToInputDirConfig(in *InputDirConfig) *v0.InputDirConfig {
return (*v0.InputDirConfig)(in)
}
func (cfg *InputDirConfig) ToParam() distgo.InputDirParam {
var excludeMatcher matcher.Matcher
if !cfg.Exclude.Empty() {
excludeMatcher = cfg.Exclude.Matcher()
}
return distgo.InputDirParam{
Path: cfg.Path,
Exclude: excludeMatcher,
}
}
func newDister(disterType string, cfgYML yaml.MapSlice, disterFactory distgo.DisterFactory) (distgo.Dister, error) {
if disterType == "" {
return nil, errors.Errorf("dister type must be non-empty")
}
if disterFactory == nil {
return nil, errors.Errorf("disterFactory must be provided")
}
cfgYMLBytes, err := yaml.Marshal(cfgYML)
if err != nil {
return nil, errors.Wrapf(err, "failed to marshal configuration")
}
return disterFactory.NewDister(disterType, cfgYMLBytes)
}
type DistersConfig v0.DistersConfig
func ToDistersConfig(in *DistersConfig) *v0.DistersConfig {
return (*v0.DistersConfig)(in)
}
func (cfgs *DistersConfig) ToParam(defaultCfg *DistersConfig, scriptIncludes string, disterFactory distgo.DisterFactory) (map[distgo.DistID]distgo.DisterParam, error) {
// keys that exist either only in cfgs or only in defaultCfg
distinctCfgs := make(map[distgo.DistID]DisterConfig)
// keys that appear in both cfgs and defaultCfg
commonCfgIDs := make(map[distgo.DistID]struct{})
if cfgs != nil {
for distID, distCfg := range *cfgs {
if defaultCfg != nil {
if _, ok := (*defaultCfg)[distID]; ok {
commonCfgIDs[distID] = struct{}{}
continue
}
}
distinctCfgs[distID] = DisterConfig(distCfg)
}
}
if defaultCfg != nil {
for distID, distCfg := range *defaultCfg {
if cfgs != nil {
if _, ok := (*cfgs)[distID]; ok {
commonCfgIDs[distID] = struct{}{}
continue
}
}
distinctCfgs[distID] = DisterConfig(distCfg)
}
}
distParamsMap := make(map[distgo.DistID]distgo.DisterParam)
// generate parameters for all of the distinct elements
for distID, distCfg := range distinctCfgs {
currParam, err := distCfg.ToParam(DisterConfig{}, scriptIncludes, disterFactory)
if err != nil {
return nil, errors.Wrapf(err, "failed to generate parameter for dist configuration %s", distID)
}
distParamsMap[distID] = currParam
}
// merge keys that appear in both maps
for distID := range commonCfgIDs {
currCfg := (*cfgs)[distID]
currParam, err := (*DisterConfig)(&currCfg).ToParam(DisterConfig((*defaultCfg)[distID]), scriptIncludes, disterFactory)
if err != nil {
return nil, errors.Wrapf(err, "failed to generate parameter for dist configuration %s", distID)
}
distParamsMap[distID] = currParam
}
return distParamsMap, nil
}