Skip to content

Commit

Permalink
Merge pull request #90 from numtide/fix-sudo-for-text-file
Browse files Browse the repository at this point in the history
proper sudo for text_file resource
  • Loading branch information
draganm authored Jan 11, 2025
2 parents bd8a957 + cccf163 commit e3fb25e
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions resource/textfile/text_file_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func resourceUpdateAndCreate(d *schema.ResourceData, m interface{}) error {
)

if d.Get("sudo").(bool) {
cmd = fmt.Sprintf("sudo %s", cmd)
cmd = fmt.Sprintf("sudo sh -c %s", shellescape.Quote(cmd))
}

stdout, stderr, err := sshsession.Run(d, cmd)
Expand All @@ -126,6 +126,10 @@ func resourceRead(d *schema.ResourceData, m interface{}) error {
{
cmd := fmt.Sprintf("stat -c '%%u %%g %%a' %s", shellescape.Quote(path))

if d.Get("sudo").(bool) {
cmd = fmt.Sprintf("sudo sh -c %s", shellescape.Quote(cmd))
}

stdout, _, err := sshsession.Run(d, cmd)
if err != nil {
d.SetId("")
Expand Down Expand Up @@ -181,9 +185,13 @@ func resourceDelete(d *schema.ResourceData, m interface{}) error {

cmd := fmt.Sprintf("rm -f %s", shellescape.Quote(path))

if d.Get("sudo").(bool) {
cmd = fmt.Sprintf("sudo sh -c %s", shellescape.Quote(cmd))
}

stdout, stderr, err := sshsession.Run(d, cmd)
if err != nil {
return errors.Wrapf(err, "error while deletin file %s:\nSTDOUT:\n%s\nSTDERR:\n%s\n", path, string(stdout), string(stderr))
return errors.Wrapf(err, "error while deleting file %s:\nSTDOUT:\n%s\nSTDERR:\n%s\n", path, string(stdout), string(stderr))
}

return nil
Expand Down

0 comments on commit e3fb25e

Please sign in to comment.