From 217ac39e77152949889bbb7461635fba962ebb40 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Sat, 24 Sep 2022 14:09:08 +0100 Subject: [PATCH] Allow setting default config path and `AdminListen` at compile time By providing the following items to `LDFLAGS`: * `-X github.com/yggdrasil-network/yggdrasil-go/src/defaults.defaultConfig=/path/to/config` * '-X github.com/yggdrasil-network/yggdrasil-go/src/defaults.defaultAdminListen=unix://path/to/sock' Closes #818. --- src/defaults/defaults.go | 24 ++++++++++++++++++++---- src/defaults/defaults_darwin.go | 2 +- src/defaults/defaults_freebsd.go | 2 +- src/defaults/defaults_linux.go | 2 +- src/defaults/defaults_openbsd.go | 2 +- src/defaults/defaults_other.go | 2 +- src/defaults/defaults_windows.go | 2 +- 7 files changed, 26 insertions(+), 10 deletions(-) diff --git a/src/defaults/defaults.go b/src/defaults/defaults.go index 7912fc7..a7492de 100644 --- a/src/defaults/defaults.go +++ b/src/defaults/defaults.go @@ -4,6 +4,9 @@ import "github.com/yggdrasil-network/yggdrasil-go/src/config" type MulticastInterfaceConfig = config.MulticastInterfaceConfig +var defaultConfig = "" // LDFLAGS='-X github.com/yggdrasil-network/yggdrasil-go/src/defaults.defaultConfig=/path/to/config +var defaultAdminListen = "" // LDFLAGS='-X github.com/yggdrasil-network/yggdrasil-go/src/defaults.defaultAdminListen=unix://path/to/sock' + // Defines which parameters are expected by default for configuration on a // specific platform. These values are populated in the relevant defaults_*.go // for the platform being targeted. They must be set. @@ -23,21 +26,34 @@ type platformDefaultParameters struct { DefaultIfName string } +func GetDefaults() platformDefaultParameters { + defaults := getDefaults() + if defaultConfig != "" { + defaults.DefaultConfigFile = defaultConfig + } + if defaultAdminListen != "" { + defaults.DefaultAdminListen = defaultAdminListen + } + return defaults +} + // Generates default configuration and returns a pointer to the resulting // NodeConfig. This is used when outputting the -genconf parameter and also when // using -autoconf. func GenerateConfig() *config.NodeConfig { + // Get the defaults for the platform. + defaults := GetDefaults() // Create a node configuration and populate it. cfg := new(config.NodeConfig) cfg.NewKeys() cfg.Listen = []string{} - cfg.AdminListen = GetDefaults().DefaultAdminListen + cfg.AdminListen = defaults.DefaultAdminListen cfg.Peers = []string{} cfg.InterfacePeers = map[string][]string{} cfg.AllowedPublicKeys = []string{} - cfg.MulticastInterfaces = GetDefaults().DefaultMulticastInterfaces - cfg.IfName = GetDefaults().DefaultIfName - cfg.IfMTU = GetDefaults().DefaultIfMTU + cfg.MulticastInterfaces = defaults.DefaultMulticastInterfaces + cfg.IfName = defaults.DefaultIfName + cfg.IfMTU = defaults.DefaultIfMTU cfg.NodeInfoPrivacy = false return cfg diff --git a/src/defaults/defaults_darwin.go b/src/defaults/defaults_darwin.go index 060ce81..a848342 100644 --- a/src/defaults/defaults_darwin.go +++ b/src/defaults/defaults_darwin.go @@ -5,7 +5,7 @@ package defaults // Sane defaults for the macOS/Darwin platform. The "default" options may be // may be replaced by the running configuration. -func GetDefaults() platformDefaultParameters { +func getDefaults() platformDefaultParameters { return platformDefaultParameters{ // Admin DefaultAdminListen: "unix:///var/run/yggdrasil.sock", diff --git a/src/defaults/defaults_freebsd.go b/src/defaults/defaults_freebsd.go index 84df48a..c8f918b 100644 --- a/src/defaults/defaults_freebsd.go +++ b/src/defaults/defaults_freebsd.go @@ -5,7 +5,7 @@ package defaults // Sane defaults for the BSD platforms. The "default" options may be // may be replaced by the running configuration. -func GetDefaults() platformDefaultParameters { +func getDefaults() platformDefaultParameters { return platformDefaultParameters{ // Admin DefaultAdminListen: "unix:///var/run/yggdrasil.sock", diff --git a/src/defaults/defaults_linux.go b/src/defaults/defaults_linux.go index c7f5f11..ea646db 100644 --- a/src/defaults/defaults_linux.go +++ b/src/defaults/defaults_linux.go @@ -5,7 +5,7 @@ package defaults // Sane defaults for the Linux platform. The "default" options may be // may be replaced by the running configuration. -func GetDefaults() platformDefaultParameters { +func getDefaults() platformDefaultParameters { return platformDefaultParameters{ // Admin DefaultAdminListen: "unix:///var/run/yggdrasil.sock", diff --git a/src/defaults/defaults_openbsd.go b/src/defaults/defaults_openbsd.go index 0ec877c..6e9d174 100644 --- a/src/defaults/defaults_openbsd.go +++ b/src/defaults/defaults_openbsd.go @@ -5,7 +5,7 @@ package defaults // Sane defaults for the BSD platforms. The "default" options may be // may be replaced by the running configuration. -func GetDefaults() platformDefaultParameters { +func getDefaults() platformDefaultParameters { return platformDefaultParameters{ // Admin DefaultAdminListen: "unix:///var/run/yggdrasil.sock", diff --git a/src/defaults/defaults_other.go b/src/defaults/defaults_other.go index 3763742..f42b087 100644 --- a/src/defaults/defaults_other.go +++ b/src/defaults/defaults_other.go @@ -5,7 +5,7 @@ package defaults // Sane defaults for the other platforms. The "default" options may be // may be replaced by the running configuration. -func GetDefaults() platformDefaultParameters { +func getDefaults() platformDefaultParameters { return platformDefaultParameters{ // Admin DefaultAdminListen: "tcp://localhost:9001", diff --git a/src/defaults/defaults_windows.go b/src/defaults/defaults_windows.go index c1ea968..90f3b9e 100644 --- a/src/defaults/defaults_windows.go +++ b/src/defaults/defaults_windows.go @@ -5,7 +5,7 @@ package defaults // Sane defaults for the Windows platform. The "default" options may be // may be replaced by the running configuration. -func GetDefaults() platformDefaultParameters { +func getDefaults() platformDefaultParameters { return platformDefaultParameters{ // Admin DefaultAdminListen: "tcp://localhost:9001",