-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): add inspect get and post commands
- Loading branch information
Showing
8 changed files
with
710 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// (c) Cartesi and individual authors (see AUTHORS) | ||
// SPDX-License-Identifier: Apache-2.0 (see LICENSE) | ||
|
||
package get | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/cartesi/rollups-node/pkg/inspectclient" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "get", | ||
Short: "Calls inspect get endpoint", | ||
Example: examples, | ||
Run: run, | ||
} | ||
|
||
const examples = `# Makes a get request with a "hi" payload: | ||
cartesi-rollups-cli inspect get --payload "hi"` | ||
|
||
var ( | ||
payload string | ||
inspectEndpoint string | ||
) | ||
|
||
func init() { | ||
Cmd.Flags().StringVar(&payload, "payload", "", | ||
"payload of the call") | ||
|
||
cobra.CheckErr(Cmd.MarkFlagRequired("payload")) | ||
|
||
Cmd.Flags().StringVar(&inspectEndpoint, "inspect-endpoint", "http://0.0.0.0:10009/inspect", | ||
"address used to connect to graphql") | ||
} | ||
|
||
func run(cmd *cobra.Command, args []string) { | ||
ctx := cmd.Context() | ||
client, err := inspectclient.NewClient(inspectEndpoint) | ||
cobra.CheckErr(err) | ||
|
||
response, err := client.Inspect(ctx, payload) | ||
cobra.CheckErr(err) | ||
|
||
respBytes, err := io.ReadAll(response.Body) | ||
cobra.CheckErr(err) | ||
|
||
var prettyJSON bytes.Buffer | ||
json.Indent(&prettyJSON, []byte(respBytes), "", " ") | ||
cobra.CheckErr(err) | ||
|
||
fmt.Print(prettyJSON.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// (c) Cartesi and individual authors (see AUTHORS) | ||
// SPDX-License-Identifier: Apache-2.0 (see LICENSE) | ||
|
||
package inspect | ||
|
||
import ( | ||
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/inspect/get" | ||
"github.com/cartesi/rollups-node/cmd/cartesi-rollups-cli/root/inspect/post" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "inspect", | ||
Short: "Makes a call to the Inspect-state HTTP API", | ||
} | ||
|
||
func init() { | ||
Cmd.AddCommand(get.Cmd) | ||
Cmd.AddCommand(post.Cmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// (c) Cartesi and individual authors (see AUTHORS) | ||
// SPDX-License-Identifier: Apache-2.0 (see LICENSE) | ||
|
||
package post | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"strings" | ||
|
||
"github.com/cartesi/rollups-node/pkg/inspectclient" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var Cmd = &cobra.Command{ | ||
Use: "post", | ||
Short: "Calls inspect post endpoint", | ||
Example: examples, | ||
Run: run, | ||
} | ||
|
||
const examples = `# Makes a post request with a "hi" payload: | ||
cartesi-rollups-cli inspect post --payload "hi"` | ||
|
||
var ( | ||
payload string | ||
inspectEndpoint string | ||
) | ||
|
||
func init() { | ||
Cmd.Flags().StringVar(&payload, "payload", "", | ||
"payload of the call") | ||
|
||
cobra.CheckErr(Cmd.MarkFlagRequired("payload")) | ||
|
||
Cmd.Flags().StringVar(&inspectEndpoint, "inspect-endpoint", "http://0.0.0.0:10009/", | ||
"address used to connect to graphql") | ||
} | ||
|
||
func run(cmd *cobra.Command, args []string) { | ||
ctx := cmd.Context() | ||
client, err := inspectclient.NewClient(inspectEndpoint) | ||
cobra.CheckErr(err) | ||
|
||
requestBody := strings.NewReader(payload) | ||
|
||
response, err := client.InspectPostWithBody(ctx, "application/octet-stream", requestBody) | ||
cobra.CheckErr(err) | ||
|
||
respBytes, err := io.ReadAll(response.Body) | ||
cobra.CheckErr(err) | ||
|
||
var prettyJSON bytes.Buffer | ||
json.Indent(&prettyJSON, []byte(respBytes), "", " ") | ||
cobra.CheckErr(err) | ||
|
||
fmt.Print(prettyJSON.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.