2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-09-29 18:11:46 -04:00
|
|
|
|
|
|
|
package quick
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
2016-06-12 22:35:28 -04:00
|
|
|
"github.com/cheggaaa/pb"
|
2015-09-29 18:11:46 -04:00
|
|
|
)
|
|
|
|
|
2017-05-06 13:16:59 -04:00
|
|
|
const errorFmt = "%5d: %s <<<<"
|
2015-09-29 18:11:46 -04:00
|
|
|
|
|
|
|
// FormatJSONSyntaxError generates a pretty printed json syntax error since
|
|
|
|
// golang doesn't provide an easy way to report the location of the error
|
2017-05-06 13:16:59 -04:00
|
|
|
func FormatJSONSyntaxError(data io.Reader, offset int64) (highlight string) {
|
2015-09-29 18:11:46 -04:00
|
|
|
var readLine bytes.Buffer
|
2017-05-06 13:16:59 -04:00
|
|
|
var errLine = 1
|
|
|
|
var readBytes int64
|
2015-09-29 18:11:46 -04:00
|
|
|
|
|
|
|
bio := bufio.NewReader(data)
|
|
|
|
|
|
|
|
// termWidth is set to a default one to use when we are
|
|
|
|
// not able to calculate terminal width via OS syscalls
|
|
|
|
termWidth := 25
|
|
|
|
|
|
|
|
// errorShift is the length of the minimum needed place for
|
2016-08-15 05:44:48 -04:00
|
|
|
// error msg accessories, like <--, etc.. We calculate it
|
2015-09-29 18:11:46 -04:00
|
|
|
// dynamically to avoid an eventual bug after modifying errorFmt
|
|
|
|
errorShift := len(fmt.Sprintf(errorFmt, 1, ""))
|
|
|
|
|
2016-06-12 22:35:28 -04:00
|
|
|
if width, err := pb.GetTerminalWidth(); err == nil {
|
|
|
|
termWidth = width
|
2015-09-29 18:11:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
b, err := bio.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
readBytes++
|
2017-05-06 13:16:59 -04:00
|
|
|
if readBytes > offset {
|
2015-09-29 18:11:46 -04:00
|
|
|
break
|
|
|
|
}
|
2019-02-13 07:59:36 -05:00
|
|
|
if b == '\n' {
|
2015-09-29 18:11:46 -04:00
|
|
|
readLine.Reset()
|
|
|
|
errLine++
|
2019-02-13 07:59:36 -05:00
|
|
|
continue
|
|
|
|
} else if b == '\t' {
|
2015-09-29 18:11:46 -04:00
|
|
|
readLine.WriteByte(' ')
|
2019-02-13 07:59:36 -05:00
|
|
|
} else if b == '\r' {
|
2015-09-29 18:11:46 -04:00
|
|
|
break
|
|
|
|
}
|
2019-02-13 07:59:36 -05:00
|
|
|
readLine.WriteByte(b)
|
2015-09-29 18:11:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
lineLen := readLine.Len()
|
|
|
|
idx := lineLen - termWidth + errorShift
|
|
|
|
if idx < 0 || idx > lineLen-1 {
|
|
|
|
idx = 0
|
|
|
|
}
|
|
|
|
|
2017-05-06 13:16:59 -04:00
|
|
|
return fmt.Sprintf(errorFmt, errLine, readLine.String()[idx:])
|
2015-09-29 18:11:46 -04:00
|
|
|
}
|