gotemplate

command
v0.0.0-...-8b69172 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 27, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

README

gotemplate

Bazel rule for Go's text/template package.

Setup and usage via Bazel

WORKSPACE file
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

# gotemplate is written in Go and hence needs rules_go and gazelle to be built.
# See https://github.com/bazelbuild/bazel-gazelle for the up to date setup instructions.
http_archive(
    name = "io_bazel_rules_go",
)

http_archive(
    name = "bazel_gazelle",
)

git_repository(
    name = "com_github_atlassian_bazel_tools",
    commit = "<commit>",
    remote = "https://github.com/atlassian/bazel-tools.git",
    shallow_since = "<bla>",
)

load("@com_github_atlassian_bazel_tools//gotemplate:deps.bzl", "gotemplate_dependencies")

gotemplate_dependencies()
Simple templating

BUILD.bazel file:

load("@com_github_atlassian_bazel_tools//gotemplate:def.bzl", "gotemplate")

gotemplate(
    name = "something",
    yaml_contexts = {
        "some.ctx1.yaml": "one",
        "some.ctx2.yaml": "two",
    },
    template = "some-template.txt",
)

some-template.txt:

bla bla bla
{{ .one.a }} {{ .two }}

some.ctx1.yaml:

a: 1

some.ctx2.yaml:

b: 2

To run this example execute:

bazel build :something

Output file will contain:

bla bla bla
1 map[b:2]
Executable files

You can build executable files using gotemplate_exec rule.

BUILD.bazel file:

load("@com_github_atlassian_bazel_tools//gotemplate:def.bzl", "gotemplate_exec")

gotemplate_exec(
    name = "something-else.bash",
    yaml_contexts = {
        "some.ctx1.yaml": "one",
        "some.ctx2.yaml": "two",
    },
    template = "some-executable-template.bash",
)

some-executable-template.bash:

#!/usr/bin/env bash

echo '{{ .one.a }} {{ .two }}'

To run this example execute:

bazel run :something-else.bash

Output file will contain:

1 map[b:2]

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL