Skip to content

Commit

Permalink
Add akashctl events command (#696)
Browse files Browse the repository at this point in the history
* Add `akashctl events` command

* Fix lint issues
  • Loading branch information
akhilkumarpilli committed Jun 22, 2020
1 parent f1c6e67 commit 8140c66
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/akashctl/main.go
Expand Up @@ -18,6 +18,7 @@ import (

"github.com/ovrclk/akash/app"
"github.com/ovrclk/akash/cmd/common"
ecmd "github.com/ovrclk/akash/events/cmd"
pcmd "github.com/ovrclk/akash/provider/cmd"
"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -54,6 +55,7 @@ func main() {
lcd.ServeCommand(cdc, lcdRoutes),
keys.Commands(),
pcmd.RootCmd(cdc),
ecmd.EventCmd(cdc),
version.Cmd,
flags.NewCompletionCmd(root, true),
)
Expand Down
65 changes: 65 additions & 0 deletions events/cmd/root.go
@@ -0,0 +1,65 @@
package cmd

import (
"context"

"github.com/spf13/cobra"

ccontext "github.com/cosmos/cosmos-sdk/client/context"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/ovrclk/akash/cmd/common"
"github.com/ovrclk/akash/events"
"github.com/ovrclk/akash/pubsub"
"golang.org/x/sync/errgroup"
)

// EventCmd prints out events in real time
func EventCmd(cdc *codec.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "events",
Short: "Prints out akash events in real time",
RunE: func(cmd *cobra.Command, args []string) error {
return common.RunForever(func(ctx context.Context) error {
return getEvents(ctx, cdc, cmd, args)
})
},
}

return cmd
}

func getEvents(ctx context.Context, cdc *codec.Codec, _ *cobra.Command, _ []string) error {
cctx := ccontext.NewCLIContext().WithCodec(cdc)

if err := cctx.Client.Start(); err != nil {
return err
}

bus := pubsub.NewBus()
defer bus.Close()

group, ctx := errgroup.WithContext(ctx)

subscriber, err := bus.Subscribe()

if err != nil {
return err
}

group.Go(func() error {
return events.Publish(ctx, cctx.Client, "akash-cli", bus)
})

group.Go(func() error {
for {
select {
case <-subscriber.Done():
return nil
case ev := <-subscriber.Events():
cctx.PrintOutput(ev)
}
}
})

return group.Wait()
}

0 comments on commit 8140c66

Please sign in to comment.