Skip to content

Commit

Permalink
util_linux: Add AppendToFileAsRoot utility function
Browse files Browse the repository at this point in the history
Add a utility function similar to WriteToFileAsRoot(), that appends to the
specified path instead of overwriting the file. We use this in a following
patch to append to Qemu bridge access configuration file.
  • Loading branch information
zeenix committed Oct 14, 2019
1 parent bece74c commit ff31d80
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion pkg/os/util_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,22 @@ import (
)

func WriteToFileAsRoot(reason, content, filepath string) error {
return writeToFileAsRoot(reason, content, filepath, false)
}

func AppendToFileAsRoot(reason, content, filepath string) error {
return writeToFileAsRoot(reason, content, filepath, true)
}

func writeToFileAsRoot(reason, content, filepath string, append bool) error {
logging.Infof("Will use root access: %s", reason)
cmd := exec.Command("sudo", "tee", filepath) // #nosec G204
var append_option string
if append {
append_option = "-a"
} else {
append_option = ""
}
cmd := exec.Command("sudo", "tee", append_option, filepath) // #nosec G204
cmd.Stdin = strings.NewReader(content)
buf := new(bytes.Buffer)
cmd.Stderr = buf
Expand Down

0 comments on commit ff31d80

Please sign in to comment.