mirror of
https://github.com/taigrr/yq
synced 2025-01-18 04:53:17 -08:00
Fixed inplace errors clearing out original file
This commit is contained in:
parent
55511de9af
commit
6f0a329331
@ -1927,6 +1927,22 @@ func TestWriteCmd_Inplace(t *testing.T) {
|
|||||||
test.AssertResult(t, expectedOutput, strings.Trim(gotOutput, "\n "))
|
test.AssertResult(t, expectedOutput, strings.Trim(gotOutput, "\n "))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWriteCmd_InplaceError(t *testing.T) {
|
||||||
|
content := `b: cat
|
||||||
|
c: 3
|
||||||
|
`
|
||||||
|
filename := test.WriteTempYamlFile(content)
|
||||||
|
defer test.RemoveTempYamlFile(filename)
|
||||||
|
|
||||||
|
cmd := getRootCommand()
|
||||||
|
result := test.RunCmd(cmd, fmt.Sprintf("write -i %s b.c 7", filename))
|
||||||
|
if result.Error == nil {
|
||||||
|
t.Error("Expected Error to occur!")
|
||||||
|
}
|
||||||
|
gotOutput := test.ReadTempYamlFile(filename)
|
||||||
|
test.AssertResult(t, content, gotOutput)
|
||||||
|
}
|
||||||
|
|
||||||
func TestWriteCmd_Append(t *testing.T) {
|
func TestWriteCmd_Append(t *testing.T) {
|
||||||
content := `b:
|
content := `b:
|
||||||
- foo
|
- foo
|
||||||
|
@ -351,6 +351,7 @@ func updateDoc(inputFile string, updateCommands []yqlib.UpdateCommand, writer io
|
|||||||
func readAndUpdate(stdOut io.Writer, inputFile string, updateData updateDataFn) error {
|
func readAndUpdate(stdOut io.Writer, inputFile string, updateData updateDataFn) error {
|
||||||
var destination io.Writer
|
var destination io.Writer
|
||||||
var destinationName string
|
var destinationName string
|
||||||
|
var completedSuccessfully = false
|
||||||
if writeInplace {
|
if writeInplace {
|
||||||
info, err := os.Stat(inputFile)
|
info, err := os.Stat(inputFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -368,7 +369,9 @@ func readAndUpdate(stdOut io.Writer, inputFile string, updateData updateDataFn)
|
|||||||
destination = tempFile
|
destination = tempFile
|
||||||
defer func() {
|
defer func() {
|
||||||
safelyCloseFile(tempFile)
|
safelyCloseFile(tempFile)
|
||||||
safelyRenameFile(tempFile.Name(), inputFile)
|
if completedSuccessfully {
|
||||||
|
safelyRenameFile(tempFile.Name(), inputFile)
|
||||||
|
}
|
||||||
}()
|
}()
|
||||||
} else {
|
} else {
|
||||||
destination = stdOut
|
destination = stdOut
|
||||||
@ -387,7 +390,9 @@ func readAndUpdate(stdOut io.Writer, inputFile string, updateData updateDataFn)
|
|||||||
encoder = yqlib.NewYamlEncoder(bufferedWriter, indent, colorsEnabled)
|
encoder = yqlib.NewYamlEncoder(bufferedWriter, indent, colorsEnabled)
|
||||||
}
|
}
|
||||||
|
|
||||||
return readStream(inputFile, mapYamlDecoder(updateData, encoder))
|
var errorProcessing = readStream(inputFile, mapYamlDecoder(updateData, encoder))
|
||||||
|
completedSuccessfully = errorProcessing == nil
|
||||||
|
return errorProcessing
|
||||||
}
|
}
|
||||||
|
|
||||||
type updateCommandParsed struct {
|
type updateCommandParsed struct {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user