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

writeToFile Function Improvements #1549

Merged
merged 3 commits into from
Mar 9, 2022
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
8 changes: 6 additions & 2 deletions docs/templating-language.md
Original file line number Diff line number Diff line change
Expand Up @@ -1601,12 +1601,16 @@ for more information.

### `writeToFile`

Writes the content to a file with username, group name, permissions. There are optional flags to
select appending mode or add a newline.
Writes the content to a file with permissions, username (or UID), group name (or GID),
and optional flags to select appending mode or add a newline.

The username and group name fields can be left blank to default to the current user and group.

For example:

```golang
{{ key "my/key/path" | writeToFile "/my/file/path.txt" "" "" "0644" }}
{{ key "my/key/path" | writeToFile "/my/file/path.txt" "100" "1000" "0644" }}
{{ 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" }}
Expand Down
66 changes: 55 additions & 11 deletions template/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1611,10 +1611,14 @@ func md5sum(item string) (string, error) {
return fmt.Sprintf("%x", md5.Sum([]byte(item))), nil
}

// writeToFile writes the content to a file with username, group name, permissions and optional
// flags to select appending mode or add a newline.
// writeToFile writes the content to a file with permissions, username (or UID), group name (or GID),
// and optional flags to select appending mode or add a newline.
//
// The username and group name fields can be left blank to default to the current user and group.
//
// For example:
// key "my/key/path" | writeToFile "/my/file/path.txt" "" "" "0644"
// key "my/key/path" | writeToFile "/my/file/path.txt" "100" "1000" "0644"
// 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"
Expand Down Expand Up @@ -1642,6 +1646,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
Expand All @@ -1659,19 +1672,50 @@ func writeToFile(path, username, groupName, permissions string, args ...string)
}

// Change ownership and permissions
u, err := user.Lookup(username)
var uid int
var gid int
cu, err := user.Current()
if err != nil {
return "", err
}
g, err := user.LookupGroup(groupName)
if err != nil {
return "", err

if username == "" {
uid, _ = strconv.Atoi(cu.Uid)
} else {
var convErr error
u, err := user.Lookup(username)
if err != nil {
// Check if username string is already a UID
uid, convErr = strconv.Atoi(username)
if convErr != nil {
return "", err
}
} else {
uid, _ = strconv.Atoi(u.Uid)
}
}
uid, _ := strconv.Atoi(u.Uid)
gid, _ := strconv.Atoi(g.Gid)
err = os.Chown(path, uid, gid)
if err != nil {
return "", err

if groupName == "" {
gid, _ = strconv.Atoi(cu.Gid)
} else {
var convErr error
g, err := user.LookupGroup(groupName)
if err != nil {
gid, convErr = strconv.Atoi(groupName)
if convErr != nil {
return "", err
}
} else {
gid, _ = strconv.Atoi(g.Gid)
}
}

// Avoid the chown call altogether if using current user and group.
if username != "" || groupName != "" {
err = os.Chown(path, uid, gid)
if err != nil {
return "", err
}
}

err = os.Chmod(path, perm)
Expand Down
127 changes: 109 additions & 18 deletions template/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os/user"
"reflect"
"strconv"
"syscall"
"testing"
"time"

Expand Down Expand Up @@ -1978,54 +1979,139 @@ func TestTemplate_Execute(t *testing.T) {
}

func Test_writeToFile(t *testing.T) {
// Use current user and its primary group for input
currentUser, err := user.Current()
if err != nil {
t.Fatal(err)
}
currentUsername := currentUser.Username
currentGroup, err := user.LookupGroupId(currentUser.Gid)
if err != nil {
t.Fatal(err)
}
currentGroupName := currentGroup.Name

cases := []struct {
name string
filePath string
content string
username string
groupName string
permissions string
flags string
expectation string
wantErr bool
}{
{
"writeToFile_without_flags",
"",
"after",
currentUsername,
currentGroupName,
"0644",
"",
"after",
false,
},
{
"writeToFile_with_different_file_permissions",
"",
"after",
currentUsername,
currentGroupName,
"0666",
"",
"after",
false,
},
{
"writeToFile_with_append",
"",
"after",
currentUsername,
currentGroupName,
"0644",
`"append"`,
"beforeafter",
false,
},
{
"writeToFile_with_newline",
"",
"after",
currentUsername,
currentGroupName,
"0644",
`"newline"`,
"after\n",
false,
},
{
"writeToFile_with_append_and_newline",
"",
"after",
currentUsername,
currentGroupName,
"0644",
`"append,newline"`,
"beforeafter\n",
false,
},
{
"writeToFile_default_owner",
"",
"after",
"",
"",
"0644",
"",
"after",
false,
},
{
"writeToFile_provide_uid_gid",
"",
"after",
currentUser.Uid,
currentUser.Gid,
"0644",
"",
"after",
false,
},
{
"writeToFile_provide_just_gid",
"",
"after",
"",
currentUser.Gid,
"0644",
"",
"after",
false,
},
{
"writeToFile_provide_just_uid",
"",
"after",
currentUser.Uid,
"",
"0644",
"",
"after",
false,
},
{
"writeToFile_create_directory",
"demo/testing.tmp",
"after",
currentUsername,
currentGroupName,
"0644",
"",
"after",
false,
},
}

for _, tc := range cases {
Expand All @@ -2035,27 +2121,25 @@ func Test_writeToFile(t *testing.T) {
t.Fatal(err)
}
defer os.RemoveAll(outDir)
outputFile, err := ioutil.TempFile(outDir, "")
if err != nil {
t.Fatal(err)
}
outputFile.WriteString("before")

// Use current user and its primary group for input
currentUser, err := user.Current()
if err != nil {
t.Fatal(err)
}
currentUsername := currentUser.Username
currentGroup, err := user.LookupGroupId(currentUser.Gid)
if err != nil {
t.Fatal(err)
var outputFilePath string
if tc.filePath == "" {
outputFile, err := ioutil.TempFile(outDir, "")
if err != nil {
t.Fatal(err)
}
_, err = outputFile.WriteString("before")
if err != nil {
t.Fatal(err)
}
outputFilePath = outputFile.Name()
} else {
outputFilePath = outDir + "/" + tc.filePath
}
currentGroupName := currentGroup.Name

templateContent := fmt.Sprintf(
"{{ \"%s\" | writeToFile \"%s\" \"%s\" \"%s\" \"%s\" %s}}",
tc.content, outputFile.Name(), currentUsername, currentGroupName, tc.permissions, tc.flags)
tc.content, outputFilePath, tc.username, tc.groupName, tc.permissions, tc.flags)
ti := &NewTemplateInput{
Contents: templateContent,
}
Expand All @@ -2072,7 +2156,7 @@ func Test_writeToFile(t *testing.T) {

// Compare generated file content with the expectation.
// The function should generate an empty string to the output.
_generatedFileContent, err := ioutil.ReadFile(outputFile.Name())
_generatedFileContent, err := ioutil.ReadFile(outputFilePath)
generatedFileContent := string(_generatedFileContent)
if err != nil {
t.Fatal(err)
Expand All @@ -2084,7 +2168,7 @@ func Test_writeToFile(t *testing.T) {
t.Errorf("writeToFile() got = %v, want %v", generatedFileContent, tc.expectation)
}
// Assert output file permissions
sts, err := outputFile.Stat()
sts, err := os.Stat(outputFilePath)
if err != nil {
t.Fatal(err)
}
Expand All @@ -2096,6 +2180,13 @@ func Test_writeToFile(t *testing.T) {
if sts.Mode() != perm {
t.Errorf("writeToFile() wrong permissions got = %v, want %v", perm, tc.permissions)
}

stat := sts.Sys().(*syscall.Stat_t)
u := strconv.FormatUint(uint64(stat.Uid), 10)
g := strconv.FormatUint(uint64(stat.Gid), 10)
if u != currentUser.Uid || g != currentUser.Gid {
t.Errorf("writeToFile() owner = %v:%v, wanted %v:%v", u, g, currentUser.Uid, currentUser.Gid)
}
})
}
}