copying file permissions from original file when inline merging - Closes #180

This commit is contained in:
Thad Craft 2018-10-24 15:00:43 -05:00 committed by Mike Farah
parent 7a6689eb40
commit 8d6e3a6a75
2 changed files with 15 additions and 3 deletions

View File

@ -2,6 +2,7 @@ package main
import (
"fmt"
"os"
"strings"
"testing"
@ -846,6 +847,7 @@ c:
func TestMergeCmd_Inplace(t *testing.T) {
filename := writeTempYamlFile(readTempYamlFile("examples/data1.yaml"))
os.Chmod(filename, os.FileMode(int(0666)))
defer removeTempYamlFile(filename)
cmd := getRootCommand()
@ -853,6 +855,7 @@ func TestMergeCmd_Inplace(t *testing.T) {
if result.Error != nil {
t.Error(result.Error)
}
info, _ := os.Stat(filename)
gotOutput := readTempYamlFile(filename)
expectedOutput := `a: simple
b:
@ -861,4 +864,5 @@ b:
c:
test: 1`
assertResult(t, expectedOutput, strings.Trim(gotOutput, "\n "))
assertResult(t, os.FileMode(int(0666)), info.Mode())
}

12
yq.go
View File

@ -11,10 +11,10 @@ import (
"strings"
errors "github.com/pkg/errors"
"gopkg.in/spf13/cobra.v0"
yaml "gopkg.in/mikefarah/yaml.v2"
logging "gopkg.in/op/go-logging.v1"
cobra "gopkg.in/spf13/cobra.v0"
)
var trimOutput = true
@ -398,7 +398,15 @@ func readAndUpdate(stdOut io.Writer, inputFile string, updateData updateDataFn)
var destination io.Writer
var destinationName string
if writeInplace {
var tempFile, err = ioutil.TempFile("", "temp")
info, err := os.Stat(inputFile)
if err != nil {
return err
}
tempFile, err := ioutil.TempFile("", "temp")
if err != nil {
return err
}
err = tempFile.Chmod(info.Mode())
if err != nil {
return err
}