mirror of
https://github.com/status-im/consul.git
synced 2025-01-09 13:26:07 +00:00
5fb9df1640
* Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
99 lines
1.8 KiB
Go
99 lines
1.8 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package tfgen
|
|
|
|
import (
|
|
"bytes"
|
|
"text/template"
|
|
|
|
"github.com/hashicorp/go-hclog"
|
|
"github.com/hashicorp/hcl/v2/hclwrite"
|
|
)
|
|
|
|
type FileResource struct {
|
|
name string
|
|
res Resource
|
|
}
|
|
|
|
func (r *FileResource) Name() string { return r.name }
|
|
|
|
func (r *FileResource) Commit(logger hclog.Logger) error {
|
|
val, err := r.res.Render()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = UpdateFileIfDifferent(logger, []byte(val), r.name, 0644)
|
|
return err
|
|
}
|
|
|
|
func File(name string, res Resource) *FileResource {
|
|
return &FileResource{name: name, res: res}
|
|
}
|
|
|
|
func Text(s string) Resource {
|
|
return &textResource{text: s}
|
|
}
|
|
|
|
func Embed(name string) Resource {
|
|
return &embedResource{name: name}
|
|
}
|
|
|
|
func Eval(t *template.Template, data any) Resource {
|
|
return &evalResource{template: t, data: data, hcl: false}
|
|
}
|
|
|
|
func HCL(t *template.Template, data any) Resource {
|
|
return &evalResource{template: t, data: data, hcl: true}
|
|
}
|
|
|
|
type Resource interface {
|
|
Render() (string, error)
|
|
}
|
|
|
|
type embedResource struct {
|
|
name string
|
|
}
|
|
|
|
func (r *embedResource) Render() (string, error) {
|
|
val, err := content.ReadFile(r.name)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(val), nil
|
|
}
|
|
|
|
type textResource struct {
|
|
text string
|
|
}
|
|
|
|
func (r *textResource) Render() (string, error) {
|
|
return r.text, nil
|
|
}
|
|
|
|
type evalResource struct {
|
|
template *template.Template
|
|
data any
|
|
hcl bool
|
|
}
|
|
|
|
func (r *evalResource) Render() (string, error) {
|
|
out, err := StringTemplate(r.template, r.data)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
if r.hcl {
|
|
return string(hclwrite.Format([]byte(out))), nil
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
func StringTemplate(t *template.Template, data any) (string, error) {
|
|
var res bytes.Buffer
|
|
if err := t.Execute(&res, data); err != nil {
|
|
return "", err
|
|
}
|
|
return res.String(), nil
|
|
}
|