Skip to content

Commit

Permalink
add -l/--local option for local full node (#9)
Browse files Browse the repository at this point in the history
* add -l/--local option for local full node

* remove apiRoot func

* fix --api url parsing
  • Loading branch information
cameroncooper authored Sep 27, 2024
1 parent 697a327 commit 0be4242
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,15 @@ require (

require (
github.com/fatih/color v1.13.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f // indirect
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/itchyny/timefmt-go v0.1.4 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
github.com/samber/mo v1.13.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/sys v0.1.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f h1:7LYC+Yfkj3CTRcShK0KOL/w6iTiKyqqBA9a41Wnggw8=
Expand All @@ -24,6 +29,8 @@ github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Ky
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
Expand All @@ -43,6 +50,7 @@ golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
5 changes: 2 additions & 3 deletions internal/cmd/coinset/root.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package cmd

import (
"net/http"
"os"

"github.com/spf13/cobra"
)

var client = &http.Client{}

var rootCmd = &cobra.Command{
Use: "coinset",
Short: "Make Chia RPC requests",
Expand All @@ -33,6 +30,7 @@ func SetVersion(v string) {
var jq string
var mainnet bool
var testnet bool
var local bool
var raw bool
var api string
var version = "dev"
Expand All @@ -41,6 +39,7 @@ func init() {
rootCmd.PersistentFlags().StringVarP(&jq, "query", "q", ".", "filter to apply using jq syntax")
rootCmd.PersistentFlags().BoolVar(&mainnet, "mainnet", false, "Use mainnet as the network")
rootCmd.PersistentFlags().BoolVarP(&testnet, "testnet", "t", false, "Use the latest testnet as the network")
rootCmd.PersistentFlags().BoolVarP(&local, "local", "l", false, "Use the local full node")
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")
Expand Down
44 changes: 25 additions & 19 deletions internal/cmd/coinset/util.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package cmd

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"log"
"net/url"
"regexp"

"github.com/TylerBrock/colorjson"
"github.com/chia-network/go-chia-libs/pkg/rpc"
"github.com/chia-network/go-chia-libs/pkg/rpcinterface"
"github.com/itchyny/gojq"
)

Expand Down Expand Up @@ -40,32 +41,37 @@ func apiRoot() string {
return "https://api.coinset.org"
}

func makeRequest(rpc string, jsonData map[string]interface{}) {
var buf io.Reader
if jsonData != nil {
jsonString, _ := json.Marshal(jsonData)
buf = bytes.NewBuffer([]byte(string(jsonString)))
func makeRequest(path string, jsonData map[string]interface{}) {
var client *rpc.Client
var err error

baseUrl, err := url.Parse(apiRoot())
if err != nil {
log.Fatal(err.Error())
}

if local {
client, err = rpc.NewClient(rpc.ConnectionModeHTTP, rpc.WithAutoConfig())
} else {
client, err = rpc.NewClient(rpc.ConnectionModePublicHTTP, rpc.WithPublicConfig(), rpc.WithBaseURL(baseUrl))
}
req, err := http.NewRequest("POST", apiRoot()+"/"+rpc, buf)

if err != nil {
fmt.Println(err)
return
log.Fatal(err.Error())
}
req.Header.Add("Content-Type", `application/json"`)

resp, err := client.Do(req)
req, err := client.FullNodeService.NewRequest(rpcinterface.Endpoint(path), jsonData)
if err != nil {
fmt.Println(err)
return
log.Fatal(err.Error())
}

jsonBytes, err := io.ReadAll(resp.Body)
jsonResponse := json.RawMessage{}
_, err = client.FullNodeService.Do(req, &jsonResponse)
if err != nil {
fmt.Println(err)
return
log.Fatal(err.Error())
}

printJson(jsonBytes)
printJson(jsonResponse)
}

func printJson(jsonBytes []byte) {
Expand Down

0 comments on commit 0be4242

Please sign in to comment.