2020-03-04 12:08:56 +00:00
|
|
|
// Copyright 2020 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 config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
|
2022-02-22 14:01:29 +00:00
|
|
|
"agola.io/agola/internal/errors"
|
2020-03-04 12:08:56 +00:00
|
|
|
"github.com/google/go-jsonnet"
|
|
|
|
)
|
|
|
|
|
|
|
|
func execJsonnet(configData []byte, configContext *ConfigContext) ([]byte, error) {
|
|
|
|
vm := jsonnet.MakeVM()
|
|
|
|
cj, err := json.Marshal(configContext)
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to marshal config context")
|
2020-03-04 12:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
vm.TLACode("ctx", string(cj))
|
|
|
|
out, err := vm.EvaluateSnippet("", string(configData))
|
|
|
|
if err != nil {
|
2022-02-22 14:01:29 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to evaluate jsonnet config")
|
2020-03-04 12:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return []byte(out), nil
|
|
|
|
}
|