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 ( import (
"fmt" "fmt"
"os"
"strings" "strings"
"testing" "testing"
@ -600,7 +601,7 @@ b:
func TestDeleteYamlArray(t *testing.T) { func TestDeleteYamlArray(t *testing.T) {
content := `- 1 content := `- 1
- 2 - 2
- 3 - 3
` `
filename := writeTempYamlFile(content) filename := writeTempYamlFile(content)
defer removeTempYamlFile(filename) defer removeTempYamlFile(filename)
@ -846,6 +847,7 @@ c:
func TestMergeCmd_Inplace(t *testing.T) { func TestMergeCmd_Inplace(t *testing.T) {
filename := writeTempYamlFile(readTempYamlFile("examples/data1.yaml")) filename := writeTempYamlFile(readTempYamlFile("examples/data1.yaml"))
os.Chmod(filename, os.FileMode(int(0666)))
defer removeTempYamlFile(filename) defer removeTempYamlFile(filename)
cmd := getRootCommand() cmd := getRootCommand()
@ -853,6 +855,7 @@ func TestMergeCmd_Inplace(t *testing.T) {
if result.Error != nil { if result.Error != nil {
t.Error(result.Error) t.Error(result.Error)
} }
info, _ := os.Stat(filename)
gotOutput := readTempYamlFile(filename) gotOutput := readTempYamlFile(filename)
expectedOutput := `a: simple expectedOutput := `a: simple
b: b:
@ -861,4 +864,5 @@ b:
c: c:
test: 1` test: 1`
assertResult(t, expectedOutput, strings.Trim(gotOutput, "\n ")) 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" "strings"
errors "github.com/pkg/errors" errors "github.com/pkg/errors"
"gopkg.in/spf13/cobra.v0"
yaml "gopkg.in/mikefarah/yaml.v2" yaml "gopkg.in/mikefarah/yaml.v2"
logging "gopkg.in/op/go-logging.v1" logging "gopkg.in/op/go-logging.v1"
cobra "gopkg.in/spf13/cobra.v0"
) )
var trimOutput = true var trimOutput = true
@ -398,7 +398,15 @@ func readAndUpdate(stdOut io.Writer, inputFile string, updateData updateDataFn)
var destination io.Writer var destination io.Writer
var destinationName string var destinationName string
if writeInplace { 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 { if err != nil {
return err return err
} }