agola/internal/services/common/variables.go

62 lines
1.7 KiB
Go
Raw Normal View History

2019-03-14 13:36:18 +00:00
// Copyright 2019 Sorint.lab
//
// 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 common
import (
2019-07-01 09:40:20 +00:00
csapi "agola.io/agola/internal/services/configstore/api"
"agola.io/agola/internal/services/types"
"agola.io/agola/internal/util"
2019-03-14 13:36:18 +00:00
)
func FilterOverriddenVariables(variables []*csapi.Variable) []*csapi.Variable {
variablesMap := map[string]*csapi.Variable{}
2019-03-14 13:36:18 +00:00
for _, v := range variables {
if _, ok := variablesMap[v.Name]; !ok {
variablesMap[v.Name] = v
}
}
filteredVariables := make([]*csapi.Variable, len(variablesMap))
2019-03-14 13:36:18 +00:00
i := 0
// keep the original order
for _, v := range variables {
if _, ok := variablesMap[v.Name]; !ok {
continue
}
filteredVariables[i] = v
delete(variablesMap, v.Name)
i++
}
return filteredVariables
}
func GetVarValueMatchingSecret(varval types.VariableValue, varParentPath string, secrets []*csapi.Secret) *csapi.Secret {
2019-03-14 13:36:18 +00:00
// get the secret value referenced by the variable, it must be a secret at the same level or a lower level
var secret *csapi.Secret
2019-03-14 13:36:18 +00:00
for _, s := range secrets {
// we assume the root path will be the same
if s.Name != varval.SecretName {
continue
}
if util.IsSameOrParentPath(s.ParentPath, varParentPath) {
2019-03-14 13:36:18 +00:00
secret = s
break
}
}
return secret
}