Skip to content

Commit

Permalink
address comments from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
tgross committed Mar 4, 2024
1 parent 9eb6576 commit d0656c4
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
7 changes: 7 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions manager/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion renderer/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 7 additions & 0 deletions template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit d0656c4

Please sign in to comment.