From 37fc646c07042ac7a16862d7ec1e86b040ef5727 Mon Sep 17 00:00:00 2001 From: Otto Suess Date: Mon, 16 Jul 2018 09:30:09 +0200 Subject: [PATCH] fix: evaluate lnd flags, fix AdminMacPath --- config.go | 24 +++++++++++++++++++++++- zapconnect.go | 17 +++++++++-------- 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/config.go b/config.go index ac5a110..952aa90 100644 --- a/config.go +++ b/config.go @@ -12,6 +12,7 @@ import ( const ( defaultConfigFilename = "lnd.conf" + defaultDataDirname = "data" defaultTLSCertFilename = "tls.cert" defaultAdminMacFilename = "admin.macaroon" ) @@ -19,6 +20,7 @@ const ( var ( defaultLndDir = btcutil.AppDataDir("lnd", false) defaultConfigFile = filepath.Join(defaultLndDir, defaultConfigFilename) + defaultDataDir = filepath.Join(defaultLndDir, defaultDataDirname) defaultTLSCertPath = filepath.Join(defaultLndDir, defaultTLSCertFilename) defaultAdminMacPath = filepath.Join(defaultLndDir, defaultAdminMacFilename) ) @@ -28,8 +30,11 @@ var ( // See loadConfig for further details regarding the configuration // loading+parsing process. type config struct { + LocalIp bool `short:"i" long:"localip" description:"Include local ip in QRCode."` + Json bool `short:"j" long:"json" description:"Generate json instead of a QRCode."` LndDir string `long:"lnddir" description:"The base directory that contains lnd's data, logs, configuration file, etc."` ConfigFile string `long:"C" long:"configfile" description:"Path to configuration file"` + DataDir string `short:"b" long:"datadir" description:"The directory to store lnd's data within"` TLSCertPath string `long:"tlscertpath" description:"Path to write the TLS certificate for lnd's RPC and REST services"` AdminMacPath string `long:"adminmacaroonpath" description:"Path to write the admin macaroon for lnd's RPC and REST services if it doesn't exist"` } @@ -38,17 +43,34 @@ func loadConfig() (*config, error) { defaultCfg := config{ LndDir: defaultLndDir, ConfigFile: defaultConfigFile, + DataDir: defaultDataDir, TLSCertPath: defaultTLSCertPath, AdminMacPath: defaultAdminMacPath, } - cfg := defaultCfg + // Pre-parse the command line options to pick up an alternative config + // file. + preCfg := defaultCfg + if _, err := flags.Parse(&preCfg); err != nil { + return nil, err + } + + cfg := preCfg configFile := cleanAndExpandPath(defaultCfg.ConfigFile) flags.IniParse(configFile, &cfg) cfg.TLSCertPath = cleanAndExpandPath(cfg.TLSCertPath) cfg.AdminMacPath = cleanAndExpandPath(cfg.AdminMacPath) + // If a custom macaroon directory wasn't specified and the data + // directory has changed from the default path, then we'll also update + // the path for the macaroons to be generated. + if cfg.DataDir != defaultDataDir && cfg.AdminMacPath == defaultAdminMacPath { + cfg.AdminMacPath = filepath.Join( + cfg.DataDir, defaultAdminMacFilename, + ) + } + return &cfg, nil } diff --git a/zapconnect.go b/zapconnect.go index 85e85d3..176b803 100644 --- a/zapconnect.go +++ b/zapconnect.go @@ -1,7 +1,7 @@ package main import ( -"flag" +// "flag" "fmt" "net" "io/ioutil" @@ -52,26 +52,27 @@ func getPublicIP() string { } func main() { - ipPtr := flag.Bool("i", false, "Include local ip in QRCode.") - jsonPtr := flag.Bool("j", false, "Generate json instead of a QRCode.") - flag.Parse() - - loadedConfig, _ := loadConfig() + loadedConfig, err := loadConfig() + if err != nil { + return + } certBytes, err := ioutil.ReadFile(loadedConfig.TLSCertPath) if err != nil { fmt.Print(err) + return } macBytes, err := ioutil.ReadFile(loadedConfig.AdminMacPath) if err != nil { fmt.Print(err) + return } sEnc := b64.StdEncoding.EncodeToString([]byte(macBytes)) ipString := "" - if *ipPtr { + if loadedConfig.LocalIp { ipString = getLocalIP() + ":10009" } else { ipString = getPublicIP() + ":10009" @@ -84,7 +85,7 @@ func main() { certB, _ := json.Marshal(cert) - if *jsonPtr { + if loadedConfig.Json { fmt.Println(string(certB)) } else { obj := qrcodeTerminal.New()