diff --git a/cmd/nebula-cert/main.go b/cmd/nebula-cert/main.go index c88626f..82ad2fe 100644 --- a/cmd/nebula-cert/main.go +++ b/cmd/nebula-cert/main.go @@ -5,10 +5,28 @@ import ( "fmt" "io" "os" + "runtime/debug" + "strings" ) +// A version string that can be set with +// +// -ldflags "-X main.Build=SOMEVERSION" +// +// at compile-time. var Build string +func init() { + if Build == "" { + info, ok := debug.ReadBuildInfo() + if !ok { + return + } + + Build = strings.TrimPrefix(info.Main.Version, "v") + } +} + type helpError struct { s string } diff --git a/cmd/nebula-service/main.go b/cmd/nebula-service/main.go index 8d0eaa1..9a17b94 100644 --- a/cmd/nebula-service/main.go +++ b/cmd/nebula-service/main.go @@ -4,6 +4,8 @@ import ( "flag" "fmt" "os" + "runtime/debug" + "strings" "github.com/sirupsen/logrus" "github.com/slackhq/nebula" @@ -18,6 +20,17 @@ import ( // at compile-time. var Build string +func init() { + if Build == "" { + info, ok := debug.ReadBuildInfo() + if !ok { + return + } + + Build = strings.TrimPrefix(info.Main.Version, "v") + } +} + func main() { serviceFlag := flag.String("service", "", "Control the system service.") configPath := flag.String("config", "", "Path to either a file or directory to load configuration from") diff --git a/cmd/nebula/main.go b/cmd/nebula/main.go index 5cf0a02..ffdc15b 100644 --- a/cmd/nebula/main.go +++ b/cmd/nebula/main.go @@ -4,6 +4,8 @@ import ( "flag" "fmt" "os" + "runtime/debug" + "strings" "github.com/sirupsen/logrus" "github.com/slackhq/nebula" @@ -18,6 +20,17 @@ import ( // at compile-time. var Build string +func init() { + if Build == "" { + info, ok := debug.ReadBuildInfo() + if !ok { + return + } + + Build = strings.TrimPrefix(info.Main.Version, "v") + } +} + func main() { configPath := flag.String("config", "", "Path to either a file or directory to load configuration from") configTest := flag.Bool("test", false, "Test the config and print the end result. Non zero exit indicates a faulty config") diff --git a/main.go b/main.go index b37df49..7b32661 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "fmt" "net" "net/netip" + "runtime/debug" + "strings" "time" "github.com/sirupsen/logrus" @@ -27,6 +29,10 @@ func Main(c *config.C, configTest bool, buildVersion string, logger *logrus.Logg } }() + if buildVersion == "" { + buildVersion = moduleVersion() + } + l := logger l.Formatter = &logrus.TextFormatter{ FullTimestamp: true, @@ -296,3 +302,18 @@ func Main(c *config.C, configTest bool, buildVersion string, logger *logrus.Logg connManager.Start, }, nil } + +func moduleVersion() string { + info, ok := debug.ReadBuildInfo() + if !ok { + return "" + } + + for _, dep := range info.Deps { + if dep.Path == "github.com/slackhq/nebula" { + return strings.TrimPrefix(dep.Version, "v") + } + } + + return "" +}