diff --git a/internal/cmd/coinset/root.go b/internal/cmd/coinset/root.go index 98ad5e6..02d7010 100644 --- a/internal/cmd/coinset/root.go +++ b/internal/cmd/coinset/root.go @@ -33,6 +33,7 @@ func SetVersion(v string) { var jq string var mainnet bool var testnet bool +var raw bool var api string var version = "dev" @@ -41,5 +42,6 @@ func init() { rootCmd.PersistentFlags().BoolVar(&mainnet, "mainnet", false, "Use mainnet as the network") rootCmd.PersistentFlags().BoolVar(&testnet, "testnet", false, "Use the latest testnet as the network") rootCmd.PersistentFlags().StringVarP(&api, "api", "a", "", "api host to use") + rootCmd.PersistentFlags().BoolVarP(&raw, "raw", "r", false, "display output in raw json") rootCmd.MarkFlagsMutuallyExclusive("mainnet", "testnet", "api") } diff --git a/internal/cmd/coinset/util.go b/internal/cmd/coinset/util.go index 26aff7a..3bc220b 100644 --- a/internal/cmd/coinset/util.go +++ b/internal/cmd/coinset/util.go @@ -42,12 +42,12 @@ func makeRequest(rpc string, jsonData map[string]interface{}) { jsonString, _ := json.Marshal(jsonData) buf = bytes.NewBuffer([]byte(string(jsonString))) } - req, err := http.NewRequest("POST", apiRoot()+"/"+rpc, buf) + req, err := http.NewRequest("POST", apiRoot()+"/"+rpc, buf) if err != nil { fmt.Println(err) return } - req.Header.Add("Content-Type", `application/json"`) + req.Header.Add("Content-Type", `application/json"`) resp, err := client.Do(req) if err != nil { @@ -55,31 +55,36 @@ func makeRequest(rpc string, jsonData map[string]interface{}) { return } - var result map[string]interface{} - byteResult, _ := ioutil.ReadAll(resp.Body) - json.Unmarshal(byteResult, &result) + var result map[string]interface{} + byteResult, _ := ioutil.ReadAll(resp.Body) - query, err := gojq.Parse(jq) - if err != nil { - fmt.Println(err) - } - iter := query.Run(result) // or query.RunWithContext - for { - v, ok := iter.Next() - if !ok { - break - } - if err, ok := v.(error); ok { - fmt.Println(err) - } + if (raw) { + fmt.Println(string(byteResult)); + return; + } + + json.Unmarshal(byteResult, &result) - f := colorjson.NewFormatter() - f.Indent = 2 + query, err := gojq.Parse(jq) + if err != nil { + fmt.Println(err) + } + iter := query.Run(result) // or query.RunWithContext + for { + v, ok := iter.Next() + if !ok { + break + } + if err, ok := v.(error); ok { + fmt.Println(err) + } - s, _ := f.Marshal(v) - fmt.Println(string(s)) + f := colorjson.NewFormatter() + f.Indent = 2 - } + s, _ := f.Marshal(v) + fmt.Println(string(s)) + } } func handleRequest(req *http.Request, err error) {