From d0656c4561ba98642e5ed7cf47b4001f491b5cd7 Mon Sep 17 00:00:00 2001 From: Tim Gross Date: Mon, 4 Mar 2024 16:06:41 -0500 Subject: [PATCH] address comments from code review --- config/config.go | 7 +++++++ manager/runner.go | 6 ++++++ renderer/renderer.go | 1 - template/template.go | 7 +++++++ 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 627a9ecf9..c8109a7da 100644 --- a/config/config.go +++ b/config/config.go @@ -118,9 +118,16 @@ type Config struct { // Consul Template in another application RendererFunc renderer.Renderer `mapstructure:"-" json:"-"` + // ReaderFunc is called whenever the template source is read, and will + // default to os.ReadFile. This is intended for use when embedding Consul + // Template in another application. ReaderFunc Reader `mapstructure:"-" json:"-"` } +// Reader is an interface that is implemented by os.OpenFile. The +// Config.ReaderFunc requires this interface so that applications that embed +// Consul Template can have an alternative implementation of os.OpenFile +// (ex. virtual file, sandboxed reads) type Reader func(src string) ([]byte, error) // Copy returns a deep copy of the current configuration. This is useful because diff --git a/manager/runner.go b/manager/runner.go index a884a273a..856822b4e 100644 --- a/manager/runner.go +++ b/manager/runner.go @@ -114,8 +114,14 @@ type Runner struct { // stopped is a boolean of whether the runner is stopped stopped bool + // rendererFn is called whenever the template needs to be written, and will + // default to renderer.Render. This is intended for use when embedding + // Consul Template in another application rendererFn renderer.Renderer + // readerFn is called whenever the template source is read, and will default + // to os.ReadFile. This is intended for use when embedding Consul Template + // in another application. readerFn config.Reader // finalConfigCopy provides access to a static copy of the finalized diff --git a/renderer/renderer.go b/renderer/renderer.go index ccc5fc1a8..5abe45ecf 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -64,7 +64,6 @@ type Renderer func(*RenderInput) (*RenderResult, error) // Render atomically renders a file contents to disk, returning a result of // whether it would have rendered and actually did render. func Render(i *RenderInput) (*RenderResult, error) { - existing, err := os.ReadFile(i.Path) fileExists := !os.IsNotExist(err) if err != nil && fileExists { diff --git a/template/template.go b/template/template.go index 7beb4d729..28b5265d1 100644 --- a/template/template.go +++ b/template/template.go @@ -27,6 +27,10 @@ var ( // does not specify either a "source" or "content" argument, which is not // valid. ErrTemplateMissingContentsAndSource = errors.New("template: must specify exactly one of 'source' or 'contents'") + + // ErrMissingReaderFunction is the error returned when the template + // configuration is missing a reader function. + ErrMissingReaderFunction = errors.New("template: missing a reader function") ) // Template is the internal representation of an individual template to process. @@ -156,6 +160,9 @@ func NewTemplate(i *NewTemplateInput) (*Template, error) { } if i.Source != "" { + if i.ReaderFunc == nil { + return nil, ErrMissingReaderFunction + } contents, err := i.ReaderFunc(i.Source) if err != nil { return nil, errors.Wrap(err, "failed to read template")