From 0a9db2d3d3bd0d4d335397e9ce6bd83c6553f333 Mon Sep 17 00:00:00 2001 From: RPRX <63339210+rprx@users.noreply.github.com> Date: Fri, 4 Dec 2020 14:03:07 +0000 Subject: [PATCH] Apply JSON format to `xray api` commands' output --- main/commands/all/api/shared.go | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/main/commands/all/api/shared.go b/main/commands/all/api/shared.go index 71ae6247..88902d3c 100644 --- a/main/commands/all/api/shared.go +++ b/main/commands/all/api/shared.go @@ -104,16 +104,30 @@ func fetchHTTPContent(target string) ([]byte, error) { } func showResponese(m proto.Message) { + if isNil(m) { + return + } + b := new(bytes.Buffer) + e := json.NewEncoder(b) + e.SetIndent("", " ") + e.SetEscapeHTML(false) + err := e.Encode(m) msg := "" - bs, err := proto.Marshal(m) if err != nil { - msg = err.Error() + msg = fmt.Sprintf("error: %s\n\n%v", err, m) } else { - msg = string(bs) - msg = strings.TrimSpace(msg) + msg = strings.TrimSpace(b.String()) } if msg == "" { return } fmt.Println(msg) } + +func isNil(i interface{}) bool { + vi := reflect.ValueOf(i) + if vi.Kind() == reflect.Ptr { + return vi.IsNil() + } + return i == nil +}