gomplate
repository·main·Indexed 25 days ago
https://github.com/hairyhenderson/gomplateA powerful template renderer that uses Go's templating syntax to inject data from various sources, including JSON, YAML, AWS EC2 metadata, Hashicorp Consul, and Hashicorp Vault. It supports a wide range of datasources via URLs, files, environment variables, and standard input, and allows for custom function extension through a plugin system.
What's inside gomplate
- gomplate is a template renderer that supports a wide variety of datasources. It allows you to inject data from sources like JSON (including encrypted EJSON), YAML, AWS EC2 metadata, Hashicorp Consul, and Hashicorp Vault into your templates. It uses Go's templating syntax for logic and transformations.
Explore gomplate namespaces
mainIn addition to standard Go template functions, gomplate provides over 200 specialized functions organized into namespaces (e.g.,base64). These namespaces allow for advanced data manipulation, file handling, and more, specific to the gomplate environment.Use the `path` namespace for slash-based paths
maingomplate provides two path namespaces. Use thepathnamespace for manipulating slash-based (/) paths, such as in URLs. For local filesystem paths (especially those involving Windows-style backslashes), use thefilepathnamespace instead.Use the filepath namespace for local filesystem paths
maingomplate provides two path namespaces:pathfor slash-based (/) paths (like URLs) andfilepathfor local filesystem paths. Use thefilepathnamespace when dealing with paths that may involve platform-specific separators, such as Windows backslashes (\). Allfilepathfunctions are wrappers for Go'spath/filepathpackage.Install gomplate via go install
mainGo developers can install the binary directly using
go install. Note that this method produces a binary that isn't versioned and may not necessarily work correctly.$ go install github.com/hairyhenderson/gomplate/v5/cmd/gomplate@latestInstall gomplate via Homebrew (macOS/Linux)
mainThe simplest method for macOS and Linux is to use Homebrew.
$ brew install gomplateBasic Template Syntax
mainGomplate uses Go's
text/templatesyntax. Actions are delimited by{{and}}markers and are replaced with their output during rendering.Hello, {{ print "World" }}!Manage Template Context
mainThe context is referenced using the
.(period) character.- Use the
withaction to change the current context for a block. - The initial (root) context is always accessible via the
$variable, even if shadowed byrangeorwithblocks. - You can populate the default context from data sources using the
--contextor-cflag.
- Use the
Use S3 as a datasource
mainRetrieve objects from Amazon S3 or S3-compatible storage using the
s3://scheme.URL Structure:
s3://<bucket>/<path>?<query>Query Parameters:
region: AWS region (defaults toAWS_REGION,AWS_DEFAULT_REGION, or EC2 region).endpoint: The endpoint hostname/URI (useful for Minio or local testing). Can also be set viaAWS_S3_ENDPOINT.s3ForcePathStyle: Set totrueto force path-style access (required for some S3-compatible servers).disableSSL: Set totrueto disable SSL (use for testing only).type: Overrides the MIME type for parsing.
Output: The object contents, parsed based on the discovered MIME type or the
typequery parameter.Use `git` datasources
mainThe
gitdatasource provides access to files in local or remote git repositories. Remote repositories can be accessed viagit,git+file,git+http,git+https, andgit+sshschemes.URL Structure:
scheme://[userinfo@]authority//path/to/repo//path/to/file#fragment- The
//sequence separates the repository path from the file/directory path. If//is missing, the datasource points to the repository root. - The
#fragmentspecifies the branch or tag (e.g.,#mainor#refs/tags/v1).
Note: For local repositories, only committed files are visible; 'dirty' or uncommitted files are ignored.
- The
Process directories with --input-dir and --output-dir
mainTo process multiple templates in a directory recursively, use
--input-dirand--output-dir. All files in the input directory are treated as templates, and the resulting files are stored in the output directory while preserving the original directory structure. The output directory is created automatically if it does not exist.You can filter files using
--excludeand--includeor a.gomplateignorefile.# Process all files in directory "templates" with the datasource given # and store the files with the same directory structure in "config" gomplate --input-dir=templates --output-dir=config --datasource config=config.yamlFormat time using reference layouts
maingomplate uses a reference time for formatting and parsing:Mon Jan 2 15:04:05 -0700 MST 2006. Instead of using format codes like%Y-%m-%d, you provide an example of the layout you want. You can use pre-defined constant layouts for convenience.