From b4bc6f918e361a536417a80b0d46eebaf3464ed3 Mon Sep 17 00:00:00 2001 From: "Johan T. Halseth" Date: Tue, 9 Feb 2021 15:20:18 +0100 Subject: [PATCH] lncli: add state command --- cmd/lncli/cmd_state.go | 45 ++++++++++++++++++++++++++++++++++++++++++ cmd/lncli/main.go | 11 +++++++++++ 2 files changed, 56 insertions(+) create mode 100644 cmd/lncli/cmd_state.go diff --git a/cmd/lncli/cmd_state.go b/cmd/lncli/cmd_state.go new file mode 100644 index 00000000..d561c8d0 --- /dev/null +++ b/cmd/lncli/cmd_state.go @@ -0,0 +1,45 @@ +package main + +import ( + "context" + + "github.com/lightningnetwork/lnd/lnrpc" + "github.com/urfave/cli" +) + +var getStateCommand = cli.Command{ + Name: "state", + Category: "Startup", + Usage: "Get the current state of the wallet and RPC", + Description: ` + Get the current state of the wallet. The possible states are: + - NON_EXISTING: wallet has not yet been initialized. + - LOCKED: wallet is locked. + - UNLOCKED: wallet has been unlocked successfully, but the full RPC is + not yet ready. + - RPC_READY: the daemon has started and the RPC is fully available. + `, + Flags: []cli.Flag{}, + Action: actionDecorator(getState), +} + +func getState(ctx *cli.Context) error { + ctxb := context.Background() + client, cleanUp := getStateServiceClient(ctx) + defer cleanUp() + + req := &lnrpc.SubscribeStateRequest{} + stream, err := client.SubscribeState(ctxb, req) + if err != nil { + return err + } + + // Get a single state, then exit. + resp, err := stream.Recv() + if err != nil { + return err + } + + printRespJSON(resp) + return nil +} diff --git a/cmd/lncli/main.go b/cmd/lncli/main.go index 0ed5d8d0..0350238b 100644 --- a/cmd/lncli/main.go +++ b/cmd/lncli/main.go @@ -57,6 +57,16 @@ func getWalletUnlockerClient(ctx *cli.Context) (lnrpc.WalletUnlockerClient, func return lnrpc.NewWalletUnlockerClient(conn), cleanUp } +func getStateServiceClient(ctx *cli.Context) (lnrpc.StateClient, func()) { + conn := getClientConn(ctx, true) + + cleanUp := func() { + conn.Close() + } + + return lnrpc.NewStateClient(conn), cleanUp +} + func getClient(ctx *cli.Context) (lnrpc.LightningClient, func()) { conn := getClientConn(ctx, false) @@ -351,6 +361,7 @@ func main() { trackPaymentCommand, versionCommand, profileSubCommand, + getStateCommand, } // Add any extra commands determined by build flags.