2019-09-03 06:05:30 +00:00
|
|
|
// Copyright 2019 Drone.IO Inc. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Drone Non-Commercial License
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// +build !oss
|
|
|
|
|
|
|
|
package converter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/drone/drone/core"
|
2021-06-03 13:06:57 +00:00
|
|
|
"github.com/drone/drone/plugin/converter/jsonnet"
|
2019-09-03 06:05:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO(bradrydzewski) handle jsonnet imports
|
|
|
|
// TODO(bradrydzewski) handle jsonnet object vs array output
|
|
|
|
|
|
|
|
// Jsonnet returns a conversion service that converts the
|
|
|
|
// jsonnet file to a yaml file.
|
|
|
|
func Jsonnet(enabled bool) core.ConvertService {
|
|
|
|
return &jsonnetPlugin{
|
|
|
|
enabled: enabled,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonnetPlugin struct {
|
|
|
|
enabled bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *jsonnetPlugin) Convert(ctx context.Context, req *core.ConvertArgs) (*core.Config, error) {
|
|
|
|
if p.enabled == false {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the file extension is not jsonnet we can
|
|
|
|
// skip this plugin by returning zero values.
|
|
|
|
if strings.HasSuffix(req.Repo.Config, ".jsonnet") == false {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2021-06-03 13:06:57 +00:00
|
|
|
file, err := jsonnet.Parse(req, nil, nil)
|
2019-09-03 06:05:30 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2021-06-03 13:06:57 +00:00
|
|
|
return nil, err
|
2019-09-03 06:05:30 +00:00
|
|
|
}
|
|
|
|
return &core.Config{
|
2021-06-03 13:06:57 +00:00
|
|
|
Data: file,
|
2019-09-03 06:05:30 +00:00
|
|
|
}, nil
|
|
|
|
}
|