From bb098187e73e94541ac0e53dd9e28b96a476f053 Mon Sep 17 00:00:00 2001 From: James Geddes Date: Tue, 4 Feb 2025 17:32:36 +0000 Subject: [PATCH] add support for .tofu files Signed-off-by: James Geddes --- docs/user-guide/configuration/footer-from.md | 6 +++--- docs/user-guide/configuration/header-from.md | 6 +++--- terraform/load.go | 7 ++++--- terraform/load_test.go | 14 +++++++++++--- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/docs/user-guide/configuration/footer-from.md b/docs/user-guide/configuration/footer-from.md index 6a425fa8..960141b7 100644 --- a/docs/user-guide/configuration/footer-from.md +++ b/docs/user-guide/configuration/footer-from.md @@ -11,14 +11,14 @@ toc: true Since `v0.12.0` Relative path to a file to extract footer for the generated output from. Supported -file formats are `.adoc`, `.md`, `.tf`, and `.txt`. +file formats are `.adoc`, `.md`, `.tf`, `.tofu`, and `.txt`. {{< alert type="info" >}} The whole file content is being extracted as module footer when extracting from `.adoc`, `.md`, or `.txt`. {{< /alert >}} -To extract footer from `.tf` file you need to use following javascript, c, or java +To extract footer from `.tf` or `.tofu` file you need to use following javascript, c, or java like multi-line comment. ```tf @@ -37,7 +37,7 @@ resource "foo" "bar" { ... } ``` {{< alert type="info" >}} -This comment must start at the immediate first line of the `.tf` file +This comment must start at the immediate first line of the `.tf` or `.tofu` file before any `resource`, `variable`, `module`, etc. {{< /alert >}} diff --git a/docs/user-guide/configuration/header-from.md b/docs/user-guide/configuration/header-from.md index b5f016ef..2ab4a0b6 100644 --- a/docs/user-guide/configuration/header-from.md +++ b/docs/user-guide/configuration/header-from.md @@ -11,14 +11,14 @@ toc: true Since `v0.10.0` Relative path to a file to extract header for the generated output from. Supported -file formats are `.adoc`, `.md`, `.tf`, and `.txt`. +file formats are `.adoc`, `.md`, `.tf`, `.tofu`, and `.txt`. {{< alert type="info" >}} The whole file content is being extracted as module header when extracting from `.adoc`, `.md`, or `.txt`. {{< /alert >}} -To extract header from `.tf` file you need to use following javascript, c, or java +To extract header from `.tf` or `.tofu` file you need to use following javascript, c, or java like multi-line comment. ```tf @@ -37,7 +37,7 @@ resource "foo" "bar" { ... } ``` {{< alert type="info" >}} -This comment must start at the immediate first line of the `.tf` file +This comment must start at the immediate first line of the `.tf` or `.tofu` file before any `resource`, `variable`, `module`, etc. {{< /alert >}} diff --git a/terraform/load.go b/terraform/load.go index fea188ba..55dfc72e 100644 --- a/terraform/load.go +++ b/terraform/load.go @@ -108,10 +108,10 @@ func isFileFormatSupported(filename string, section string) (bool, error) { return false, fmt.Errorf("--%s-from value is missing", section) } switch getFileFormat(filename) { - case ".adoc", ".md", ".tf", ".txt": + case ".adoc", ".md", ".tf", ".tofu", ".txt": return true, nil } - return false, fmt.Errorf("only .adoc, .md, .tf, and .txt formats are supported to read %s from", section) + return false, fmt.Errorf("only .adoc, .md, .tf, .tofu and .txt formats are supported to read %s from", section) } func loadHeader(config *print.Config) (string, error) { @@ -146,7 +146,8 @@ func loadSection(config *print.Config, file string, section string) (string, err } return "", err // user explicitly asked for a file which doesn't exist } - if getFileFormat(file) != ".tf" { + format := getFileFormat(file) + if format != ".tf" && format != ".tofu" { content, err := os.ReadFile(filepath.Clean(filename)) if err != nil { return "", err diff --git a/terraform/load_test.go b/terraform/load_test.go index 89a87eb1..563770f2 100644 --- a/terraform/load_test.go +++ b/terraform/load_test.go @@ -161,6 +161,14 @@ func TestIsFileFormatSupported(t *testing.T) { errText: "", section: "header", }, + { + name: "is file format supported", + filename: "main.tofu", + expected: true, + wantErr: false, + errText: "", + section: "header", + }, { name: "is file format supported", filename: "main.txt", @@ -174,7 +182,7 @@ func TestIsFileFormatSupported(t *testing.T) { filename: "main.doc", expected: false, wantErr: true, - errText: "only .adoc, .md, .tf, and .txt formats are supported to read header from", + errText: "only .adoc, .md, .tf, .tofu and .txt formats are supported to read header from", section: "header", }, { @@ -189,7 +197,7 @@ func TestIsFileFormatSupported(t *testing.T) { filename: "main.doc", expected: false, wantErr: true, - errText: "only .adoc, .md, .tf, and .txt formats are supported to read footer from", + errText: "only .adoc, .md, .tf, .tofu and .txt formats are supported to read footer from", section: "footer", }, { @@ -405,7 +413,7 @@ func TestLoadSections(t *testing.T) { file: "wrong-formate.docx", expected: "", wantErr: true, - errText: "only .adoc, .md, .tf, and .txt formats are supported to read footer from", + errText: "only .adoc, .md, .tf, .tofu and .txt formats are supported to read footer from", section: "footer", }, {