Skip to content
/ tbd Public

"to be defined" - a really simple way to create text templates with placeholders

License

Notifications You must be signed in to change notification settings

lucasepe/tbd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tbd

Go Report Card     Release     codecov

"to be defined"

A really simple way to create text templates with placeholders.

This tool is deliberately simple and trivial, no advanced features.

If you need advanced templates rendering which supports complex syntax and a huge list of datasources (JSON, YAML, AWS EC2 metadata, BoltDB, Hashicorp > Consul and Hashicorp Vault secrets), I recommend you use one of these:

Built-in Variables

When executed inside a Git repository, tbd automatically exports some variables related to the Git repository which may be useful in the build phase.

These variables are: ARCH, OS, REPO_COMMIT, REPO_HOST, REPO_NAME, REPO_ROOT, REPO_TAG, REPO_TAG_CLEAN, REPO_URL, TIMESTAMP.

Try it! With tbd in your PATH, go in a Git folder and type:

$ tbd vars
+----------------+------------------------------------------+
| ARCH           | amd64                                    |
| OS             | linux                                    |
| REPO_COMMIT    | a3193274112d3a6f5c2a0277e2ca07ec238d622f |
| REPO_HOST      | github.com                               |
| REPO_NAME      | tbd                                      |
| REPO_ROOT      | lucasepe                                 |
| REPO_TAG       | v0.1.1                                   |
| REPO_TAG_CLEAN | 0.1.1                                    |
| REPO_URL       | https://github.com/lucasepe/tbd          |
| TIMESTAMP      | 2021-07-26T14:22:36Z                     |
+----------------+------------------------------------------+

Obviously in your case the values ​​will be different.

How does a template looks like ?

A template is a text document in which you can insert placeholders for the text you want to make dynamic.

  • a placeholder is delimited by {{ and }} - (i.e. {{ FULL_NAME }})
  • all text outside placeholders is copied to the output unchanged

Example:

apiVersion: v1
kind: Pod
metadata:
  name: {{ metadata.name }}
  labels:
    app: {{ metadata.labels.app }}
 spec:
  containers:
    - name: {{ container.1.name }}
      image: {{ container.1.image }}
      ports:
        - containerPort: {{ container.1.port }}
    - name: {{ container.2.name }}
      image: {{ container.2.image }}
      ports:
        - containerPort: {{ container.2.port }}

Another example:

{{ greeting }}

I will be out of the office from {{ start.date }} until {{ return.date }}. 
If you need immediate assistance while I’m away, please email {{ contact.email }}.

Best,
{{ name }}

How can I define placeholders values?

Create a text file in which you enter the values for the placeholders.

  • define a placeholder value using KEY = value (or KEY: value)
  • empty lines are skipped
  • lines beginning with # are treated as comments

Example:

# metadata values
metadata.name = rss-site
metadata.labels.app = web

# containers values
container.1.name = front-end
container.1.image = nginx
container.1.port = 80

container.2.name = rss-reader
container.2.image: nickchase/rss-php-nginx:v1
container.2.port: 88

Another example...

greeting: Greetings
start.date: August, 9 
return.date: August 23
contact.email: pinco.pallo@gmail.com
name: Pinco Pallo 

How fill in the template?

Use the merge command

$ tbd merge /path/to/your/template /path/to/your/envfile

Example:

$ tbd merge testdata/sample.tbd testdata/sample.vars

👉 you can also specify an HTTP url to fetch your template and/or placeholders values.

Example:

$ tbd merge https://raw.githubusercontent.com/lucasepe/tbd/main/testdata/sample.tbd \
    https://raw.githubusercontent.com/lucasepe/tbd/main/testdata/sample.vars

and the output is...

Greetings

I will be out of the office from August, 9 until August 23. 
If you need immediate assistance while I’m away, please email pinco.pallo@gmail.com.

Best,
Pinco Pallo

How to list all template placeholders?

Use the marks command.

$ tbd marks /path/to/your/template

Example:

$ tbd marks testdata/sample.tbd
greeting
start.date
return.date
contact.email
name

How to list all variables?

Use the vars command.

$ tbd vars /path/to/your/envfile

Example:

$ tbd vars testdata/sample.vars
+----------------+------------------------------------------+
| Label          | Value                                    |
+----------------+------------------------------------------+
| ARCH           | amd64                                    |
| OS             | linux                                    |
| REPO_COMMIT    | a3193274112d3a6f5c2a0277e2ca07ec238d622f |
| REPO_HOST      | github.com                               |
| REPO_NAME      | tbd                                      |
| REPO_ROOT      | lucasepe                                 |
| REPO_TAG       | v0.1.1                                   |
| REPO_TAG_CLEAN | 0.1.1                                    |
| REPO_URL       | https://github.com/lucasepe/tbd          |
| TIMESTAMP      | 2021-07-26T14:17:49Z                     |
| contact.email  | pinco.pallo@gmail.com                    |
| greeting       | Greetings                                |
| name           | Pinco Pallo                              |
| return.date    | August 23                                |
| start.date     | August, 9                                |
+----------------+------------------------------------------+

As you can see, since I ran the command in a Git repository, there are also relative variables.

How to install?

If you have golang installed:

$ go install github.com/lucasepe/tbd@latest

This will create the executable under your $GOPATH/bin directory.

Ready-To-Use Releases

If you don't want to compile the sourcecode yourself, here you can find the tool already compiled for:

  • MacOS
  • Linux
  • Windows

Credits

Thanks to @valyala for the fasttemplate library - which I have modified by adding and removing some functions for the tbd purpose.

About

"to be defined" - a really simple way to create text templates with placeholders

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published