Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: add filebase64 function #11791

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/11791.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
hcl: added support for using the `filebase64` function in jobspecs
```
1 change: 1 addition & 0 deletions jobspec2/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func Functions(basedir string, allowFS bool) map[string]function.Function {
"basename": guardFS(allowFS, filesystem.BasenameFunc),
"dirname": guardFS(allowFS, filesystem.DirnameFunc),
"file": guardFS(allowFS, filesystem.MakeFileFunc(basedir, false)),
"filebase64": guardFS(allowFS, filesystem.MakeFileFunc(basedir, true)),
"fileexists": guardFS(allowFS, filesystem.MakeFileExistsFunc(basedir)),
"fileset": guardFS(allowFS, filesystem.MakeFileSetFunc(basedir)),
"pathexpand": guardFS(allowFS, filesystem.PathExpandFunc),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
layout: docs
page_title: filebase64 - Functions - Configuration Language
description: |-
The filebase64 function reads the contents of the file at the given path and
returns them as a base64-encoded string.
---

# `filebase64` Function

`filebase64` reads the contents of a file at the given path and returns them as
a base64-encoded string.

```hcl
filebase64(path)
```

The result is a Base64 representation of the raw bytes in the given file.

Nomad uses the "standard" Base64 alphabet as defined in
[RFC 4648 section 4](https://tools.ietf.org/html/rfc4648#section-4).

## Examples

```
> filebase64("${path.module}/hello.txt")
SGVsbG8gV29ybGQ=
```

## Related Functions

* [`file`](/docs/job-specification/hcl2/functions/file/file) also reads the contents of a given file,
but interprets the data as UTF-8 text and returns the result directly
as a string, without any further encoding.
* [`base64decode`](/docs/job-specification/hcl2/functions/encoding/base64decode) can decode a
Base64 string representing bytes in UTF-8, but in practice `base64decode(filebase64(...))`
is equivalent to the shorter expression `file(...)`.
4 changes: 4 additions & 0 deletions website/data/docs-nav-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,10 @@
"title": "file",
"path": "job-specification/hcl2/functions/file/file"
},
{
"title": "filebase64",
"path": "job-specification/hcl2/functions/file/filebase64"
},
{
"title": "fileexists",
"path": "job-specification/hcl2/functions/file/fileexists"
Expand Down