lncli: add state command

This commit is contained in:
Johan T. Halseth 2021-02-09 15:20:18 +01:00
parent 9ef556624e
commit b4bc6f918e
No known key found for this signature in database
GPG Key ID: 15BAADA29DA20D26
2 changed files with 56 additions and 0 deletions

45
cmd/lncli/cmd_state.go Normal file
View File

@ -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
}

View File

@ -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.