Skip to content

Commit

Permalink
added --raw output option
Browse files Browse the repository at this point in the history
  • Loading branch information
cameroncooper committed Feb 8, 2024
1 parent 34bf81a commit 2446c61
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
2 changes: 2 additions & 0 deletions internal/cmd/coinset/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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")
}
51 changes: 28 additions & 23 deletions internal/cmd/coinset/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,44 +42,49 @@ 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 {
fmt.Println(err)
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) {
Expand Down

0 comments on commit 2446c61

Please sign in to comment.