2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2016-07-30 10:20:43 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"text/template"
|
2017-02-15 23:14:47 +00:00
|
|
|
|
2023-02-07 19:07:20 +00:00
|
|
|
"github.com/Masterminds/sprig/v3"
|
2023-05-01 21:18:34 +00:00
|
|
|
"github.com/hashicorp/go-msgpack/codec"
|
2017-02-15 23:14:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
jsonHandlePretty = &codec.JsonHandle{
|
|
|
|
HTMLCharsAsIs: true,
|
|
|
|
Indent: 4,
|
|
|
|
}
|
2016-07-30 10:20:43 +00:00
|
|
|
)
|
|
|
|
|
2022-08-16 14:06:30 +00:00
|
|
|
// DataFormatter is a transformer of the data.
|
2016-07-30 10:20:43 +00:00
|
|
|
type DataFormatter interface {
|
|
|
|
// TransformData should return transformed string data.
|
|
|
|
TransformData(interface{}) (string, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DataFormat returns the data formatter specified format.
|
|
|
|
func DataFormat(format, tmpl string) (DataFormatter, error) {
|
|
|
|
switch format {
|
|
|
|
case "json":
|
2016-08-04 10:19:31 +00:00
|
|
|
if len(tmpl) > 0 {
|
|
|
|
return nil, fmt.Errorf("json format does not support template option.")
|
|
|
|
}
|
2016-07-30 10:20:43 +00:00
|
|
|
return &JSONFormat{}, nil
|
|
|
|
case "template":
|
|
|
|
return &TemplateFormat{tmpl}, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("Unsupported format is specified.")
|
|
|
|
}
|
|
|
|
|
2023-02-07 19:07:20 +00:00
|
|
|
type JSONFormat struct{}
|
2016-07-30 10:20:43 +00:00
|
|
|
|
|
|
|
// TransformData returns JSON format string data.
|
|
|
|
func (p *JSONFormat) TransformData(data interface{}) (string, error) {
|
2017-02-15 23:14:47 +00:00
|
|
|
var buf bytes.Buffer
|
2023-02-07 19:07:20 +00:00
|
|
|
err := codec.NewEncoder(&buf, jsonHandlePretty).Encode(data)
|
2016-07-30 10:20:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2017-02-15 23:14:47 +00:00
|
|
|
return buf.String(), nil
|
2016-07-30 10:20:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type TemplateFormat struct {
|
|
|
|
tmpl string
|
|
|
|
}
|
|
|
|
|
|
|
|
// TransformData returns template format string data.
|
|
|
|
func (p *TemplateFormat) TransformData(data interface{}) (string, error) {
|
2023-02-07 19:07:20 +00:00
|
|
|
var out bytes.Buffer
|
2016-07-30 10:20:43 +00:00
|
|
|
if len(p.tmpl) == 0 {
|
2023-02-07 19:07:20 +00:00
|
|
|
return "", fmt.Errorf("template needs to be specified in golang's text/template format.")
|
2016-07-30 10:20:43 +00:00
|
|
|
}
|
|
|
|
|
2023-02-07 19:07:20 +00:00
|
|
|
t, err := template.New("").Funcs(makeFuncMap()).Parse(p.tmpl)
|
2016-08-04 09:42:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2023-02-07 19:07:20 +00:00
|
|
|
err = t.Execute(&out, data)
|
2016-07-30 10:20:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-02-07 19:07:20 +00:00
|
|
|
return out.String(), nil
|
2016-07-30 10:20:43 +00:00
|
|
|
}
|
2017-07-01 01:10:19 +00:00
|
|
|
|
|
|
|
func Format(json bool, template string, data interface{}) (string, error) {
|
|
|
|
var format string
|
|
|
|
if json && len(template) > 0 {
|
|
|
|
return "", fmt.Errorf("Both json and template formatting are not allowed")
|
|
|
|
} else if json {
|
|
|
|
format = "json"
|
|
|
|
} else if len(template) > 0 {
|
|
|
|
format = "template"
|
|
|
|
} else {
|
|
|
|
return "", fmt.Errorf("no formatting option given")
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := DataFormat(format, template)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := f.TransformData(data)
|
|
|
|
if err != nil {
|
2023-02-07 19:07:20 +00:00
|
|
|
return "", fmt.Errorf("Error formatting the data: %w", err)
|
2017-07-01 01:10:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
2023-02-07 19:07:20 +00:00
|
|
|
|
|
|
|
func makeFuncMap() template.FuncMap {
|
|
|
|
fm := template.FuncMap{}
|
|
|
|
|
|
|
|
// Add the Sprig functions to the funcmap. These functions are decorated
|
|
|
|
// with `sprig_` to match how they are treated in consul-template
|
|
|
|
for k, v := range sprig.FuncMap() {
|
|
|
|
target := "sprig_" + k
|
|
|
|
fm[target] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return fm
|
|
|
|
}
|