cmd/lncli: move commands and export
We want to export some of our CLI code to re-use in other projects. But in Golang you cannot import code from a `main` package. So we need to move the actual code into its own package and only have the `func main()` in the `main` package.
This commit is contained in:
43 files changed
+841
-674
No files matched your search
@@ -0,0 +1,62 @@
|
||||
//go:build dev
|
||||
// +build dev
|
||||
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/lightningnetwork/lnd/lncfg"
|
||||
"github.com/lightningnetwork/lnd/lnrpc"
|
||||
"github.com/lightningnetwork/lnd/lnrpc/devrpc"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
// devCommands will return the set of commands to enable for devrpc builds.
|
||||
func devCommands() []cli.Command {
|
||||
return []cli.Command{
|
||||
{
|
||||
Name: "importgraph",
|
||||
Category: "Development",
|
||||
Description: "Imports graph from describegraph JSON",
|
||||
Usage: "Import the network graph.",
|
||||
ArgsUsage: "graph-json-file",
|
||||
Action: actionDecorator(importGraph),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func getDevClient(ctx *cli.Context) (devrpc.DevClient, func()) {
|
||||
conn := getClientConn(ctx, false)
|
||||
cleanUp := func() {
|
||||
conn.Close()
|
||||
}
|
||||
return devrpc.NewDevClient(conn), cleanUp
|
||||
}
|
||||
|
||||
func importGraph(ctx *cli.Context) error {
|
||||
ctxc := getContext()
|
||||
client, cleanUp := getDevClient(ctx)
|
||||
defer cleanUp()
|
||||
|
||||
jsonFile := lncfg.CleanAndExpandPath(ctx.Args().First())
|
||||
jsonBytes, err := os.ReadFile(jsonFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading JSON from file %v: %v",
|
||||
jsonFile, err)
|
||||
}
|
||||
|
||||
jsonGraph := &lnrpc.ChannelGraph{}
|
||||
err = lnrpc.ProtoJSONUnmarshalOpts.Unmarshal(jsonBytes, jsonGraph)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing JSON: %w", err)
|
||||
}
|
||||
res, err := client.ImportGraph(ctxc, jsonGraph)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
printRespJSON(res)
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user