Fixed write-inplace with no expression

This commit is contained in:
Mike Farah 2021-02-10 17:06:16 +11:00
parent 26742b2597
commit bd0818c481
2 changed files with 18 additions and 4 deletions

View File

@ -42,14 +42,21 @@ func evaluateAll(cmd *cobra.Command, args []string) error {
colorsEnabled = true
}
if writeInplace && len(args) < 2 {
firstFileIndex := -1
if !nullInput && len(args) == 1 {
firstFileIndex = 0
} else if len(args) > 1 {
firstFileIndex = 1
}
if writeInplace && (firstFileIndex == -1) {
return fmt.Errorf("Write inplace flag only applicable when giving an expression and at least one file")
}
if writeInplace {
// only use colors if its forced
colorsEnabled = forceColor
writeInPlaceHandler := yqlib.NewWriteInPlaceHandler(args[1])
writeInPlaceHandler := yqlib.NewWriteInPlaceHandler(args[firstFileIndex])
out, err = writeInPlaceHandler.CreateTempFile()
if err != nil {
return err

View File

@ -62,14 +62,21 @@ func evaluateSequence(cmd *cobra.Command, args []string) error {
colorsEnabled = true
}
if writeInplace && len(args) < 2 {
firstFileIndex := -1
if !nullInput && len(args) == 1 {
firstFileIndex = 0
} else if len(args) > 1 {
firstFileIndex = 1
}
if writeInplace && (firstFileIndex == -1) {
return fmt.Errorf("Write inplace flag only applicable when giving an expression and at least one file")
}
if writeInplace {
// only use colors if its forced
colorsEnabled = forceColor
writeInPlaceHandler := yqlib.NewWriteInPlaceHandler(args[1])
writeInPlaceHandler := yqlib.NewWriteInPlaceHandler(args[firstFileIndex])
out, err = writeInPlaceHandler.CreateTempFile()
if err != nil {
return err