config+cmd/lncli: add flag to specify the base dir for lnd

In this commit, we introduce a new flag `--lnddir` that allows us to
set a different base directory, other than the OS specific application
data directory, for all related lnd files and directories.
This commit is contained in:
Wilmer Paulino committed 2018-03-12 22:23:56 -04:00
1 parent 8988a07bce
commit 44bc563396
2 files changed
+59 -18

No files matched your search

+30 -4
View File
@@ -28,9 +28,9 @@ const (
)
var (
lndHomeDir = btcutil.AppDataDir("lnd", false)
defaultTLSCertPath = filepath.Join(lndHomeDir, defaultTLSCertFilename)
defaultMacaroonPath = filepath.Join(lndHomeDir, defaultMacaroonFilename)
defaultLndDir = btcutil.AppDataDir("lnd", false)
defaultTLSCertPath = filepath.Join(defaultLndDir, defaultTLSCertFilename)
defaultMacaroonPath = filepath.Join(defaultLndDir, defaultMacaroonFilename)
)
func fatal(err error) {
@@ -59,6 +59,27 @@ func getClient(ctx *cli.Context) (lnrpc.LightningClient, func()) {
}
func getClientConn(ctx *cli.Context, skipMacaroons bool) *grpc.ClientConn {
lndDir := cleanAndExpandPath(ctx.GlobalString("lnddir"))
if lndDir != defaultLndDir {
// If a custom lnd directory was set, we'll also check if custom
// paths for the TLS cert and macaroon file were set as well. If
// not, we'll override their paths so they can be found within
// the custom lnd directory set. This allows us to set a custom
// lnd directory, along with custom paths to the TLS cert and
// macaroon file.
tlsCertPath := cleanAndExpandPath(ctx.GlobalString("tlscertpath"))
if tlsCertPath == defaultTLSCertPath {
ctx.GlobalSet("tlscertpath",
filepath.Join(lndDir, defaultTLSCertFilename))
}
macPath := cleanAndExpandPath(ctx.GlobalString("macaroonpath"))
if macPath == defaultMacaroonPath {
ctx.GlobalSet("no-macaroons",
filepath.Join(lndDir, defaultMacaroonFilename))
}
}
// Load the specified TLS certificate and build transport credentials
// with it.
tlsCertPath := cleanAndExpandPath(ctx.GlobalString("tlscertpath"))
@@ -136,6 +157,11 @@ func main() {
Value: "localhost:10009",
Usage: "host:port of ln daemon",
},
cli.StringFlag{
Name: "lnddir",
Value: defaultLndDir,
Usage: "path to lnd's base directory",
},
cli.StringFlag{
Name: "tlscertpath",
Value: defaultTLSCertPath,
@@ -210,7 +236,7 @@ func main() {
func cleanAndExpandPath(path string) string {
// Expand initial ~ to OS specific home directory.
if strings.HasPrefix(path, "~") {
homeDir := filepath.Dir(lndHomeDir)
homeDir := filepath.Dir(defaultLndDir)
path = strings.Replace(path, "~", homeDir, 1)
}