Embed sample-lbcd.conf contents at build time. Use embedded config

if the sample-lbcd.conf is not found at runtime.
This commit is contained in:
Jonathan Moody 2022-04-19 15:31:00 -04:00
parent 1bab29aa69
commit 7b40def376

View file

@ -7,6 +7,7 @@ package main
import ( import (
"bufio" "bufio"
"crypto/rand" "crypto/rand"
_ "embed"
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"errors" "errors"
@ -78,6 +79,9 @@ var (
defaultLogDir = filepath.Join(defaultHomeDir, defaultLogDirname) defaultLogDir = filepath.Join(defaultHomeDir, defaultLogDirname)
) )
//go:embed sample-lbcd.conf
var sampleConfig string
// runServiceCommand is only set to a real function on Windows. It is used // runServiceCommand is only set to a real function on Windows. It is used
// to parse and execute service commands specified via the -s flag. // to parse and execute service commands specified via the -s flag.
var runServiceCommand func(string) error var runServiceCommand func(string) error
@ -1176,11 +1180,15 @@ func createDefaultConfigFile(destinationPath string) error {
} }
generatedRPCPass := base64.StdEncoding.EncodeToString(randomBytes) generatedRPCPass := base64.StdEncoding.EncodeToString(randomBytes)
var reader *bufio.Reader
src, err := os.Open(sampleConfigPath) src, err := os.Open(sampleConfigPath)
if err != nil { if err != nil {
return err // Fall back to sample config embedded at build time.
} reader = bufio.NewReader(strings.NewReader(sampleConfig))
} else {
reader = bufio.NewReader(src)
defer src.Close() defer src.Close()
}
dest, err := os.OpenFile(destinationPath, dest, err := os.OpenFile(destinationPath,
os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
@ -1191,7 +1199,6 @@ func createDefaultConfigFile(destinationPath string) error {
// We copy every line from the sample config file to the destination, // We copy every line from the sample config file to the destination,
// only replacing the two lines for rpcuser and rpcpass // only replacing the two lines for rpcuser and rpcpass
reader := bufio.NewReader(src)
for err != io.EOF { for err != io.EOF {
var line string var line string
line, err = reader.ReadString('\n') line, err = reader.ReadString('\n')