tty-config icon indicating copy to clipboard operation
tty-config copied to clipboard

add support for modifying content after reading

Open colstrom opened this issue 2 years ago • 0 comments

Describe the change

This is a pretty straightforward one, it just adds the ability to modify the contents of a config file after reading it, but before it gets parsed and merged.

Why are we doing this?

The boring way to use this it to append content to the config or something like that. But there are more fun things you can do with it, like scanning the content to check for any secrets that shouldn't be stored in plaintext, or... this thing that I'm doing:

TTY::Config.new do |config|
  if config.exist?
    config.read do |content|
      if %w[<% %>].all? { |tag| content.include? tag }
        content.replace ERB.new(content).result
      end
    end
  end
end

With a config file like this:

---
github:
  access_token: %x{vault kv get -mount secret -field access-token github}

This allows the config file itself to be an integration point for things like secrets management.

Benefits

The main use case I'm using this for is templating config files, and this small change is enough to allow that.

Drawbacks

This does make use of the implicit block, which would prevent it from being used for something else. An alternate (more complicated) approach would be to add some sort of hooks that could be configured, perhaps passing a proc to those that would be called at the appropriate time. That's... probably more complexity than is needed.

Requirements

  • [x] Tests written & passing locally?
  • [x] Code style checked?
  • [x] Rebased with master branch?
  • [x] Documentation updated?
  • [ ] Changelog updated?

colstrom avatar Oct 27 '22 22:10 colstrom