internal/handlersettings/handlersettings.go (32 lines of code) (raw):
package handlersettings
import (
"github.com/go-kit/kit/log"
"github.com/pkg/errors"
)
var (
errSourceNotSpecified = errors.New("Either 'source.script' or 'source.scriptUri' has to be specified")
)
// parseAndValidateSettings reads configuration from configFolder, decrypts it,
// runs JSON-schema and logical validation on it and returns it back.
func ParseAndValidateSettings(ctx *log.Context, configFilePath string) (h HandlerSettings, _ error) {
ctx.Log("event", "reading configuration from "+configFilePath)
pubJSON, protJSON, err := readSettings(configFilePath)
if err != nil {
return h, err
}
ctx.Log("event", "read configuration")
ctx.Log("event", "parsing configuration json")
if err := UnmarshalHandlerSettings(pubJSON, protJSON, &h.PublicSettings, &h.ProtectedSettings); err != nil {
return h, errors.Wrap(err, "json parsing error")
}
ctx.Log("event", "parsed configuration json")
ctx.Log("event", "validating configuration logically")
if err := h.validate(); err != nil {
return h, errors.Wrap(err, "invalid configuration")
}
ctx.Log("event", "validated configuration")
return h, nil
}
// readSettings uses specified configFilePath (comes from HandlerEnvironment) to
// decrypt and parse the public/protected settings of the extension handler into
// JSON objects.
func readSettings(configFilePath string) (pubSettingsJSON, protSettingsJSON map[string]interface{}, err error) {
pubSettingsJSON, protSettingsJSON, err = ReadSettings(configFilePath)
err = errors.Wrapf(err, "error reading extension configuration")
return
}