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

Add writeToFile function #1495

Merged
merged 5 commits into from
Jul 29, 2021
Merged

Conversation

anhdat
Copy link
Contributor

@anhdat anhdat commented Jul 28, 2021

Fixes #1077
Based on the implementation of savetofile plugin.
writeToFile writes the content to a file with username, group name, permissions. There are optional flags to select appending mode or add a newline.

For example:

{{ key "my/key/path" | writeToFile "/my/file/path.txt" "my-user" "my-group" "0644" }}
{{ key "my/key/path" | writeToFile "/my/file/path.txt" "my-user" "my-group" "0644" "append" }}
{{ key "my/key/path" | writeToFile "/my/file/path.txt" "my-user" "my-group" "0644" "append,newline" }}

This is my first PR in this repository, please guide me on how to finish this PR. Thank you in advance!

@anhdat anhdat requested a review from a team July 28, 2021 12:52
@eikenb
Copy link
Contributor

eikenb commented Jul 28, 2021

Thankjs for the PR @anhdat,

I know this is frequently requested and will definitely look at this for the next feature release. I'm currently debating about doing a bugfix 0.26.1 release or just jumping to 0.27.0... if the latter I'll definitely include this.

@eikenb
Copy link
Contributor

eikenb commented Jul 28, 2021

One thing... I'm not going to consider it a blocker as I'd like to get this feature added, but if you get a chance I'd love to see a test added for this. If you decide to add test(s) there are 2 possible places, in template_tests.go as an entry in the test table in TestTemplate_Execute or you can test it directly in funcs_test.go. Either one would be great and, if you do decide to write the test, I'll leave it to you to pick which you method you prefer.

Thanks.

@anhdat
Copy link
Contributor Author

anhdat commented Jul 29, 2021

Hi @eikenb, thanks for the guidance. I've added the tests to the template_tests.go file. The writeToFile function requires a different testing setup, so I created a function Test_writeToFile alongside TestTemplate_Execute. Please let me know if this works. Thanks.

@anhdat
Copy link
Contributor Author

anhdat commented Jul 29, 2021

The build is failing in consul-template/manager:

foo--- FAIL: TestDedup_IsLeader (2.00s)
    dedup_test.go:46: timeout
--- FAIL: TestDedup_UpdateDeps (2.00s)
    dedup_test.go:76: timeout
--- FAIL: TestDedup_FollowerUpdate (2.00s)
    dedup_test.go:135: timeout
FAIL
FAIL	github.com/hashicorp/consul-template/manager	5.094s

Would a retry fix this?

@eikenb eikenb added the hashicat-update-required Changes that need to be ported to hashicat label Jul 29, 2021
@eikenb
Copy link
Contributor

eikenb commented Jul 29, 2021

Thanks for the tests!

A re-run did fix that test failure. I've been working to eliminate them, but there are still a number of race conditions lurking in the tests. That dedup one has been particularly annoying.

The github-actions CI is still failing due to it finding a data race in a test that passes on every other platform (???)... I'm going to look into that, but I'm only testing github-actions CI at this point so I'm not going to consider it a blocker. I think it really just comes down to the use of t.Parallel() and I plan on removing all of those very soon anyways (they make debugging tests a PITA) and then, maybe, add some back if they actually help.

@eikenb eikenb merged commit 0b1d208 into hashicorp:master Jul 29, 2021
@anhdat anhdat deleted the 1077/write-to-file-function branch July 29, 2021 19:21
@eikenb eikenb added this to the 0.26.1 milestone Jul 29, 2021
@wavedocs
Copy link

Guys, I propose to supplement the code that will function to create nested directories if they are missing.

diff --git a/template/funcs.go b/template/funcs.go
index 66990b1c..376e6afe 100644
--- a/template/funcs.go
+++ b/template/funcs.go
@@ -1628,6 +1628,15 @@ func writeToFile(path, username, groupName, permissions string, args ...string)
                        return "", err
                }
        } else {
+               dirPath := filepath.Dir(path)
+
+               if _, err := os.Stat(dirPath); err != nil {
+                       err := os.MkdirAll(dirPath, os.ModePerm)
+                       if err != nil {
+                               return "", err
+                       }
+               }
+
                f, err = os.Create(path)
                if err != nil {
                        return "", err

@eikenb
Copy link
Contributor

eikenb commented Aug 11, 2021

Hey @wavedocs,

Thanks for raising your concern. I suggest submitting a PR with this change and linking it back here. Having a PR will make it easy to merge and for others to take a more active part of the discussion. If you do submit a PR with this please add a test for the added functionality in addition to the feature.

Thanks.

@eikenb eikenb added the hashicat-update-complete Completed porting changes to hashicat label Nov 20, 2021
@mbillow
Copy link
Contributor

mbillow commented Mar 7, 2022

@wavedocs @eikenb I included the directory creation and subsequent tests in #1549, FYI.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement hashicat-update-complete Completed porting changes to hashicat hashicat-update-required Changes that need to be ported to hashicat
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Generating multiple similar files from the same template
4 participants