Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: debug pkg #21

Merged
merged 17 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ jobs:
env:
GOTOOLCHAIN: local+path
GOSUMDB: off
run: make check
run: make ci
46 changes: 40 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,27 @@
# Project variables.
PROJECT_NAME = 'ignite apps'

## govet: Run go vet.
## goget: Run go get for all apps.
goget:
@echo Running go mod tidy...
@for dir in $$(find $$(pwd -P) -mindepth 1 -maxdepth 4 -type d); do \
if [ -e "$$dir/go.mod" ]; then \
echo "Running go get $(REPO) in $$dir"; \
cd "$$dir" && go get $(REPO); \
fi \
done

## modtidy: Run go mod tidy for all apps.
modtidy:
@echo Running go mod tidy...
@for dir in $$(find $$(pwd -P) -mindepth 1 -maxdepth 4 -type d); do \
if [ -e "$$dir/go.mod" ]; then \
echo "Running go mod tidy in $$dir"; \
cd "$$dir" && go mod tidy; \
fi \
done

## govet: Run go vet for all apps.
govet:
@echo Running go vet...
@for dir in $$(find $$(pwd -P) -mindepth 1 -maxdepth 4 -type d); do \
Expand All @@ -13,7 +33,7 @@ govet:
fi \
done

## govulncheck: Run govulncheck
## govulncheck: Run govulncheck for all apps.
govulncheck:
@command -v govulncheck >/dev/null 2>&1 || { \
echo "Installing govulncheck..."; \
Expand All @@ -27,11 +47,25 @@ govulncheck:
done
@echo Running govulncheck...

## lint: Run Golang CI Lint.
## lint: Run Golang Lint for all apps.
lint:
@command -v golangci-lint >/dev/null 2>&1 || { \
echo "Installing golangci-lint..."; \
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.42.1; \
}
@echo Running golangci-lint...
@for dir in $$(find $$(pwd -P) -mindepth 1 -maxdepth 4 -type d); do \
if [ -e "$$dir/go.mod" ]; then \
echo "Running golangci-lint in $$dir"; \
cd "$$dir" && golangci-lint run; \
fi \
done

## lint-ci: Run Golang CI Lint for all apps.
lint-ci:
@command -v golangci-lint >/dev/null 2>&1 || { \
echo "Installing golangci-lint..."; \
curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.42.1; \
}
@echo Running golangci-lint...
@for dir in $$(find $$(pwd -P) -mindepth 1 -maxdepth 4 -type d); do \
Expand All @@ -41,10 +75,10 @@ lint:
fi \
done

## check: Run govet, govulncheck and lint
check: govet govulncheck lint
## ci: Run CI pipeline govet, govulncheck and lint
ci: govet govulncheck lint-ci

## format: Install and run goimports and gofumpt
## format: Install and run goimports and gofumpt for all apps.
format:
@echo Formatting...
@command -v gofumpt >/dev/null 2>&1 || { \
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,12 @@ This project is licensed under the [Copyright License](LICENSE) - see the [LICEN

- Join the community conversations on [Discord](https://discord.com/invite/ignite) or [X/Twitter](https://twitter.com/ignite).
- Follow the project's progress and updates.

## Developer instruction

- Clone this repo locally.
- Scaffold your app: `ignite app scaffold my-app`
- Add the folder to the `go.work`.
- Add your cobra commands into `debug/main.go` and the module replace to the `debug/go.mod` for a easy debug.
- Add the plugin: `ignite app add -g ($GOPATH)/src/github.com/ignite/apps/my-app`
- Test with Ignite.
2 changes: 1 addition & 1 deletion examples/chain-info/cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

// ExecuteInfo executes the info subcommand.
func ExecuteInfo(ctx context.Context, cmd *plugin.ExecutedCommand, c *chain.Chain) error {
func ExecuteInfo(_ context.Context, _ *plugin.ExecutedCommand, c *chain.Chain) error {
w := tabwriter.NewWriter(os.Stdout, 0, 8, 0, '\t', 0)
write := func(s string, v interface{}) {
fmt.Fprintf(w, "%s:\t%v\n", s, v)
Expand Down
4 changes: 2 additions & 2 deletions examples/flags/cmd/cowsay.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"github.com/spf13/pflag"
)

// ExecuteHello executes the cowsay subcommand.
func ExecuteCowsay(ctx context.Context, cmd *plugin.ExecutedCommand) error {
// ExecuteCowsay executes the cowsay subcommand.
func ExecuteCowsay(_ context.Context, cmd *plugin.ExecutedCommand) error {
flags, err := cmd.NewFlags()
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion examples/flags/cmd/hello.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// ExecuteHello executes the hello subcommand.
func ExecuteHello(ctx context.Context, cmd *plugin.ExecutedCommand) error {
func ExecuteHello(_ context.Context, cmd *plugin.ExecutedCommand) error {
flags, err := cmd.NewFlags()
if err != nil {
return err
Expand Down
20 changes: 13 additions & 7 deletions examples/health-monitor/cmd/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func ExecuteMonitor(ctx context.Context, cmd *plugin.ExecutedCommand, chainInfo
if err != nil {
return fmt.Errorf("failed to parse flags: %w", err)
}
jsonFlag, err := getJsonFlag(flags)
jsonFlag, err := getJSONFlag(flags)
if err != nil {
return fmt.Errorf("failed to get json flag: %w", err)
}
refreshDur, err := getRefreshDurationFlag(flags)
if err != nil {
return fmt.Errorf("failed to get refresh-duration flag: %w", err)
}
rpcAddress, err := getRpcAddressFlag(flags)
rpcAddress, err := getRPCAddressFlag(flags)
if err != nil {
return fmt.Errorf("failed to get rpc-address flag: %w", err)
}
Expand Down Expand Up @@ -56,7 +56,9 @@ func ExecuteMonitor(ctx context.Context, cmd *plugin.ExecutedCommand, chainInfo
return fmt.Errorf("failed to get status: %w", err)
}
if jsonFlag {
printJson(status)
if err := printJSON(status); err != nil {
return err
}
} else {
printUserFriendly(status)
}
Expand All @@ -66,7 +68,7 @@ func ExecuteMonitor(ctx context.Context, cmd *plugin.ExecutedCommand, chainInfo
}
}

func getJsonFlag(flags *pflag.FlagSet) (bool, error) {
func getJSONFlag(flags *pflag.FlagSet) (bool, error) {
j, err := flags.GetBool("json")
if err != nil {
return false, err
Expand All @@ -85,7 +87,7 @@ func getRefreshDurationFlag(flags *pflag.FlagSet) (time.Duration, error) {
return time.ParseDuration(r)
}

func getRpcAddressFlag(flags *pflag.FlagSet) (string, error) {
func getRPCAddressFlag(flags *pflag.FlagSet) (string, error) {
return flags.GetString("rpc-address")
}

Expand All @@ -97,16 +99,20 @@ type statusResponse struct {
LatestBlockHash string `json:"latest_block_hash"`
}

func printJson(status *ctypes.ResultStatus) {
func printJSON(status *ctypes.ResultStatus) error {
resp := statusResponse{
Time: time.Now(),
ChainID: status.NodeInfo.Network,
Version: status.NodeInfo.Version,
Height: status.SyncInfo.LatestBlockHeight,
LatestBlockHash: status.SyncInfo.LatestBlockHash.String(),
}
data, _ := json.Marshal(resp)
data, err := json.Marshal(resp)
if err != nil {
return err
}
fmt.Println(string(data))
return nil
}

func printUserFriendly(status *ctypes.ResultStatus) {
Expand Down
2 changes: 1 addition & 1 deletion examples/hello-world/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (app) Manifest(_ context.Context) (*plugin.Manifest, error) {
}, nil
}

func (app) Execute(ctx context.Context, c *plugin.ExecutedCommand, _ plugin.ClientAPI) error {
func (app) Execute(_ context.Context, _ *plugin.ExecutedCommand, _ plugin.ClientAPI) error {
fmt.Println("Hello, world!")
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion examples/hooks/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (app) Manifest(_ context.Context) (*plugin.Manifest, error) {
}, nil
}

func (app) Execute(ctx context.Context, c *plugin.ExecutedCommand, _ plugin.ClientAPI) error {
func (app) Execute(_ context.Context, _ *plugin.ExecutedCommand, _ plugin.ClientAPI) error {
fmt.Println(`To use either run "ignite chain build" or "ignite chain serve" and see the output.`)
return nil
}
Expand Down
15 changes: 0 additions & 15 deletions hermes/cmd/debug/main.go

This file was deleted.

43 changes: 43 additions & 0 deletions test/debug/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# debug

This package is a helper that you could use to debug your Ignite App by running it as a standalone binary independently of Ignite CLI.

## How to use

```shell
go run test/debug/main.go <COMMAND>
```

e.g:
```shell
go run test/debug/main.go explorer <ARGS>
```

```shell
go run test/debug/main.go hermes <ARGS>
```

## Developer instruction

- Replace the app repo for a local folder into the `test/debug/go.mod`.
```go.mod
replace (
github.com/ignite/apps/explorer => ../../explorer
github.com/ignite/apps/hermes => ../../hermes
github.com/ignite/apps/<MY-APP> => ../../<MY-APP>
)
```

- Add the command to be debugged.
```go
rootCmd.AddCommand(
explorer.NewExplorer(),
hermes.NewHermes(),
myapp.NewCommand() // <--- Add the new command
)
```

### Caveat

The app doesn't support debugging interactions with the CLI. This method allows debugging running App commands independently of the CLI, which means that Ignite doesn't know the PID of the App, so it won't be able to attach to it, and because of that, debugged Apps won't be able to communicate with Ignite to for example use the Client API.
We will soon support dynamic debugging in Ignite CLI.
Loading
Loading