Skip to content

Commit 217ac39

Browse files
committed
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.
1 parent 0abfe78 commit 217ac39

File tree

7 files changed

+26
-10
lines changed

7 files changed

+26
-10
lines changed

src/defaults/defaults.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import "github.com/yggdrasil-network/yggdrasil-go/src/config"
44

55
type MulticastInterfaceConfig = config.MulticastInterfaceConfig
66

7+
var defaultConfig = "" // LDFLAGS='-X github.com/yggdrasil-network/yggdrasil-go/src/defaults.defaultConfig=/path/to/config
8+
var defaultAdminListen = "" // LDFLAGS='-X github.com/yggdrasil-network/yggdrasil-go/src/defaults.defaultAdminListen=unix://path/to/sock'
9+
710
// Defines which parameters are expected by default for configuration on a
811
// specific platform. These values are populated in the relevant defaults_*.go
912
// for the platform being targeted. They must be set.
@@ -23,21 +26,34 @@ type platformDefaultParameters struct {
2326
DefaultIfName string
2427
}
2528

29+
func GetDefaults() platformDefaultParameters {
30+
defaults := getDefaults()
31+
if defaultConfig != "" {
32+
defaults.DefaultConfigFile = defaultConfig
33+
}
34+
if defaultAdminListen != "" {
35+
defaults.DefaultAdminListen = defaultAdminListen
36+
}
37+
return defaults
38+
}
39+
2640
// Generates default configuration and returns a pointer to the resulting
2741
// NodeConfig. This is used when outputting the -genconf parameter and also when
2842
// using -autoconf.
2943
func GenerateConfig() *config.NodeConfig {
44+
// Get the defaults for the platform.
45+
defaults := GetDefaults()
3046
// Create a node configuration and populate it.
3147
cfg := new(config.NodeConfig)
3248
cfg.NewKeys()
3349
cfg.Listen = []string{}
34-
cfg.AdminListen = GetDefaults().DefaultAdminListen
50+
cfg.AdminListen = defaults.DefaultAdminListen
3551
cfg.Peers = []string{}
3652
cfg.InterfacePeers = map[string][]string{}
3753
cfg.AllowedPublicKeys = []string{}
38-
cfg.MulticastInterfaces = GetDefaults().DefaultMulticastInterfaces
39-
cfg.IfName = GetDefaults().DefaultIfName
40-
cfg.IfMTU = GetDefaults().DefaultIfMTU
54+
cfg.MulticastInterfaces = defaults.DefaultMulticastInterfaces
55+
cfg.IfName = defaults.DefaultIfName
56+
cfg.IfMTU = defaults.DefaultIfMTU
4157
cfg.NodeInfoPrivacy = false
4258

4359
return cfg

src/defaults/defaults_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package defaults
55

66
// Sane defaults for the macOS/Darwin platform. The "default" options may be
77
// may be replaced by the running configuration.
8-
func GetDefaults() platformDefaultParameters {
8+
func getDefaults() platformDefaultParameters {
99
return platformDefaultParameters{
1010
// Admin
1111
DefaultAdminListen: "unix:///var/run/yggdrasil.sock",

src/defaults/defaults_freebsd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package defaults
55

66
// Sane defaults for the BSD platforms. The "default" options may be
77
// may be replaced by the running configuration.
8-
func GetDefaults() platformDefaultParameters {
8+
func getDefaults() platformDefaultParameters {
99
return platformDefaultParameters{
1010
// Admin
1111
DefaultAdminListen: "unix:///var/run/yggdrasil.sock",

src/defaults/defaults_linux.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package defaults
55

66
// Sane defaults for the Linux platform. The "default" options may be
77
// may be replaced by the running configuration.
8-
func GetDefaults() platformDefaultParameters {
8+
func getDefaults() platformDefaultParameters {
99
return platformDefaultParameters{
1010
// Admin
1111
DefaultAdminListen: "unix:///var/run/yggdrasil.sock",

src/defaults/defaults_openbsd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package defaults
55

66
// Sane defaults for the BSD platforms. The "default" options may be
77
// may be replaced by the running configuration.
8-
func GetDefaults() platformDefaultParameters {
8+
func getDefaults() platformDefaultParameters {
99
return platformDefaultParameters{
1010
// Admin
1111
DefaultAdminListen: "unix:///var/run/yggdrasil.sock",

src/defaults/defaults_other.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package defaults
55

66
// Sane defaults for the other platforms. The "default" options may be
77
// may be replaced by the running configuration.
8-
func GetDefaults() platformDefaultParameters {
8+
func getDefaults() platformDefaultParameters {
99
return platformDefaultParameters{
1010
// Admin
1111
DefaultAdminListen: "tcp://localhost:9001",

src/defaults/defaults_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package defaults
55

66
// Sane defaults for the Windows platform. The "default" options may be
77
// may be replaced by the running configuration.
8-
func GetDefaults() platformDefaultParameters {
8+
func getDefaults() platformDefaultParameters {
99
return platformDefaultParameters{
1010
// Admin
1111
DefaultAdminListen: "tcp://localhost:9001",

0 commit comments

Comments
 (0)