Skip to content

Commit c8c3548

Browse files
committed
feat: check duplicate tools and pipeline templates
Signed-off-by: Bird <[email protected]>
1 parent 372c153 commit c8c3548

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

internal/pkg/configmanager/rawconfig.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"unicode"
88

99
"gopkg.in/yaml.v3"
10+
11+
"github.com/devstream-io/devstream/pkg/util/log"
1012
)
1113

1214
// rawConfig respent every valid config block for devstream
@@ -46,9 +48,15 @@ func newRawConfigFromConfigBytes(fileText []byte) (*rawConfig, error) {
4648
case "pipelineTemplates":
4749
rawConfigData.pipelineTemplates = append(rawConfigData.pipelineTemplates, matchStr...)
4850
case "config":
51+
if len(rawConfigData.config) != 0 {
52+
return nil, fmt.Errorf("<config> key can only be defined once")
53+
}
4954
rawConfigData.config = append(rawConfigData.config, matchStr...)
5055
case "vars":
5156
rawConfigData.vars = append(rawConfigData.vars, matchStr...)
57+
default:
58+
const errHint = "you may have filled in the wrong key or imported a yaml file that is not related to dtm"
59+
return nil, fmt.Errorf("invalid config key <%s>, %s", string(configKey), errHint)
5260
}
5361
}
5462
if err := rawConfigData.validate(); err != nil {
@@ -127,6 +135,10 @@ func (c *rawConfig) getTemplatePipelineMap() (map[string]string, error) {
127135
if err != nil {
128136
return nil, err
129137
}
138+
if _, ok := pipelineTemplateMap[t.Name]; ok {
139+
return nil, fmt.Errorf("pipelineTemplate <%s> is duplicated", t.Name)
140+
}
141+
log.Infof("pipelineTemplate %s is loaded", t.Name)
130142
pipelineTemplateMap[t.Name] = string(rawPipeline)
131143
}
132144
return pipelineTemplateMap, nil

internal/pkg/configmanager/tool.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func (tools Tools) validate() (errs []error) {
6666
for _, tool := range tools {
6767
errs = append(errs, tool.validate()...)
6868
}
69+
errs = append(errs, tools.DuplicatedCheck()...)
6970
return
7071
}
7172

@@ -116,6 +117,22 @@ func (t *Tool) GetPluginMD5FileNameWithOSAndArch(os, arch string) string {
116117
return t.GetPluginNameWithOSAndArch(os, arch) + ".md5"
117118
}
118119

120+
func (t *Tool) EqualNameAndInstanceID(other *Tool) bool {
121+
return t.Name == other.Name && t.InstanceID == other.InstanceID
122+
}
123+
124+
func (tools Tools) DuplicatedCheck() (errs []error) {
125+
list := make(map[string]struct{})
126+
for _, t := range tools {
127+
key := t.KeyWithNameAndInstanceID()
128+
if _, ok := list[key]; ok {
129+
errs = append(errs, fmt.Errorf("tool or app <%s> is duplicated", key))
130+
}
131+
list[key] = struct{}{}
132+
}
133+
return errs
134+
}
135+
119136
// validateDependsOnConfig is used to validate all tools' DependsOn config
120137
func (tools Tools) validateDependsOnConfig() (retErrs []error) {
121138
retErrs = make([]error, 0)
@@ -137,7 +154,7 @@ func (tools Tools) validateDependsOnConfig() (retErrs []error) {
137154
}
138155

139156
if _, ok := toolKeySet[d]; !ok {
140-
errs = append(errs, fmt.Errorf("t %s's DependsOn %s doesn't exist", tool.InstanceID, d))
157+
errs = append(errs, fmt.Errorf("tool %s's DependsOn %s doesn't exist", tool.InstanceID, d))
141158
}
142159
}
143160
return

0 commit comments

Comments
 (0)