Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avoid printing control characters from goal #1585

Merged
merged 5 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 0 additions & 166 deletions cmd/goal/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"os"
"strconv"
"strings"
"unicode"
"unicode/utf8"

"github.com/spf13/cobra"

Expand Down Expand Up @@ -920,170 +918,6 @@ var deleteAppCmd = &cobra.Command{
},
}

func unicodePrintable(str string) bool {
for _, r := range str {
if !unicode.IsPrint(r) {
return false
}
}
return true
}

func jsonPrintable(str string) bool {
// htmlSafeSet holds the value true if the ASCII character with the given
// array position can be safely represented inside a JSON string, embedded
// inside of HTML <script> tags, without any additional escaping.
//
// All values are true except for the ASCII control characters (0-31), the
// double quote ("), the backslash character ("\"), HTML opening and closing
// tags ("<" and ">"), and the ampersand ("&").
var htmlSafeSet = [utf8.RuneSelf]bool{
' ': true,
'!': true,
'"': false,
'#': true,
'$': true,
'%': true,
'&': false,
'\'': true,
'(': true,
')': true,
'*': true,
'+': true,
',': true,
'-': true,
'.': true,
'/': true,
'0': true,
'1': true,
'2': true,
'3': true,
'4': true,
'5': true,
'6': true,
'7': true,
'8': true,
'9': true,
':': true,
';': true,
'<': false,
'=': true,
'>': false,
'?': true,
'@': true,
'A': true,
'B': true,
'C': true,
'D': true,
'E': true,
'F': true,
'G': true,
'H': true,
'I': true,
'J': true,
'K': true,
'L': true,
'M': true,
'N': true,
'O': true,
'P': true,
'Q': true,
'R': true,
'S': true,
'T': true,
'U': true,
'V': true,
'W': true,
'X': true,
'Y': true,
'Z': true,
'[': true,
'\\': false,
']': true,
'^': true,
'_': true,
'`': true,
'a': true,
'b': true,
'c': true,
'd': true,
'e': true,
'f': true,
'g': true,
'h': true,
'i': true,
'j': true,
'k': true,
'l': true,
'm': true,
'n': true,
'o': true,
'p': true,
'q': true,
'r': true,
's': true,
't': true,
'u': true,
'v': true,
'w': true,
'x': true,
'y': true,
'z': true,
'{': true,
'|': true,
'}': true,
'~': true,
'\u007f': true,
}

for _, r := range str {
if r >= utf8.RuneSelf {
return false
}
if htmlSafeSet[r] == false {
return false
}
}
return true
}

func heuristicFormatStr(str string) string {
// See if we can print it as a json output
if jsonPrintable(str) {
return str
}

// otherwise, see if it's a 32 byte string that could be printed as an address
if len(str) == 32 {
var addr basics.Address
copy(addr[:], []byte(str))
return addr.String()
}

// otherwise, use the default json formatter to output the byte array.
return str
}

func heuristicFormatKey(key string) string {
return heuristicFormatStr(key)
}

func heuristicFormatVal(val basics.TealValue) basics.TealValue {
if val.Type == basics.TealUintType {
return val
}
val.Bytes = heuristicFormatStr(val.Bytes)
return val
}

func heuristicFormat(state map[string]basics.TealValue) map[string]basics.TealValue {
result := make(map[string]basics.TealValue)
for k, v := range state {
result[heuristicFormatKey(k)] = heuristicFormatVal(v)
}
return result
}

var readStateAppCmd = &cobra.Command{
Use: "read",
Short: "Read local or global state for an application",
Expand Down
40 changes: 27 additions & 13 deletions cmd/goal/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,36 +492,50 @@ func ensurePassword() []byte {
}

func reportInfoln(args ...interface{}) {
fmt.Println(args...)
// log.Infoln(args...)
for _, line := range strings.Split(fmt.Sprint(args...), "\n") {
printable, line := unicodePrintable(line)
if !printable {
fmt.Println(infoNonPrintableCharacters)
}
fmt.Println(line)
}
}

func reportInfof(format string, args ...interface{}) {
fmt.Printf(format+"\n", args...)
// log.Infof(format, args...)
reportInfoln(fmt.Sprintf(format, args...))
}

func reportWarnln(args ...interface{}) {
fmt.Print("Warning: ")
fmt.Println(args...)
// log.Warnln(args...)

for _, line := range strings.Split(fmt.Sprint(args...), "\n") {
printable, line := unicodePrintable(line)
if !printable {
fmt.Println(infoNonPrintableCharacters)
}

fmt.Println(line)
}
}

func reportWarnf(format string, args ...interface{}) {
fmt.Printf("Warning: "+format+"\n", args...)
// log.Warnf(format, args...)
reportWarnln(fmt.Sprintf(format, args...))
}

func reportErrorln(args ...interface{}) {
fmt.Fprintln(os.Stderr, args...)
// log.Warnln(args...)
outStr := fmt.Sprint(args...)
for _, line := range strings.Split(outStr, "\n") {
printable, line := unicodePrintable(line)
if !printable {
fmt.Fprintln(os.Stderr, errorNonPrintableCharacters)
}
fmt.Fprintln(os.Stderr, line)
}
os.Exit(1)
}

func reportErrorf(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format+"\n", args...)
// log.Warnf(format, args...)
os.Exit(1)
reportErrorln(fmt.Sprintf(format, args...))
}

// writeFile is a wrapper of ioutil.WriteFile which considers the special
Expand Down
Loading