Commit bf6ec2bb authored by Matthew Holt's avatar Matthew Holt

main: Use embedded version, debug.BuildInfo

parent 13d0454f
...@@ -25,6 +25,7 @@ import ( ...@@ -25,6 +25,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"runtime" "runtime"
"runtime/debug"
"strconv" "strconv"
"strings" "strings"
...@@ -43,7 +44,6 @@ import ( ...@@ -43,7 +44,6 @@ import (
func init() { func init() {
caddy.TrapSignals() caddy.TrapSignals()
setVersion()
flag.BoolVar(&certmagic.Agreed, "agree", false, "Agree to the CA's Subscriber Agreement") flag.BoolVar(&certmagic.Agreed, "agree", false, "Agree to the CA's Subscriber Agreement")
flag.StringVar(&certmagic.CA, "ca", certmagic.CA, "URL to certificate authority's ACME server directory") flag.StringVar(&certmagic.CA, "ca", certmagic.CA, "URL to certificate authority's ACME server directory")
...@@ -78,9 +78,12 @@ func init() { ...@@ -78,9 +78,12 @@ func init() {
func Run() { func Run() {
flag.Parse() flag.Parse()
module := getBuildModule()
cleanModVersion := strings.TrimPrefix(module.Version, "v")
caddy.AppName = appName caddy.AppName = appName
caddy.AppVersion = appVersion caddy.AppVersion = module.Version
certmagic.UserAgent = appName + "/" + appVersion certmagic.UserAgent = appName + "/" + cleanModVersion
// Set up process log before anything bad happens // Set up process log before anything bad happens
switch logfile { switch logfile {
...@@ -144,9 +147,11 @@ func Run() { ...@@ -144,9 +147,11 @@ func Run() {
os.Exit(0) os.Exit(0)
} }
if version { if version {
fmt.Printf("%s %s (unofficial)\n", appName, appVersion) if module.Sum != "" {
if devBuild && gitShortStat != "" { // a build with a known version will also have a checksum
fmt.Printf("%s\n%s\n", gitShortStat, gitFilesModified) fmt.Printf("Caddy %s (%s)\n", module.Version, module.Sum)
} else {
fmt.Println(module.Version)
} }
os.Exit(0) os.Exit(0)
} }
...@@ -191,7 +196,7 @@ func Run() { ...@@ -191,7 +196,7 @@ func Run() {
} }
// Begin telemetry (these are no-ops if telemetry disabled) // Begin telemetry (these are no-ops if telemetry disabled)
telemetry.Set("caddy_version", appVersion) telemetry.Set("caddy_version", module.Version)
telemetry.Set("num_listeners", len(instance.Servers())) telemetry.Set("num_listeners", len(instance.Servers()))
telemetry.Set("server_type", serverType) telemetry.Set("server_type", serverType)
telemetry.Set("os", runtime.GOOS) telemetry.Set("os", runtime.GOOS)
...@@ -272,25 +277,25 @@ func defaultLoader(serverType string) (caddy.Input, error) { ...@@ -272,25 +277,25 @@ func defaultLoader(serverType string) (caddy.Input, error) {
}, nil }, nil
} }
// setVersion figures out the version information // getBuildModule returns the build info of Caddy
// based on variables set by -ldflags. // from debug.BuildInfo (requires Go modules). If
func setVersion() { // no version information is available, a non-nil
// A development build is one that's not at a tag or has uncommitted changes // value will still be returned, but with an
devBuild = gitTag == "" || gitShortStat != "" // unknown version.
func getBuildModule() *debug.Module {
if buildDate != "" { bi, ok := debug.ReadBuildInfo()
buildDate = " " + buildDate if ok {
} // The recommended way to build Caddy involves
// creating a separate main module, which
// Only set the appVersion if -ldflags was used // preserves caddy a read-only dependency
if gitNearestTag != "" || gitTag != "" { // TODO: track related Go issue: https://github.com/golang/go/issues/29228
if devBuild && gitNearestTag != "" { for _, mod := range bi.Deps {
appVersion = fmt.Sprintf("%s (+%s%s)", if mod.Path == "github.com/mholt/caddy" {
strings.TrimPrefix(gitNearestTag, "v"), gitCommit, buildDate) return mod
} else if gitTag != "" { }
appVersion = strings.TrimPrefix(gitTag, "v")
} }
} }
return &debug.Module{Version: "unknown"}
} }
func checkJSONCaddyfile() { func checkJSONCaddyfile() {
...@@ -592,18 +597,5 @@ var ( ...@@ -592,18 +597,5 @@ var (
disabledMetrics string disabledMetrics string
) )
// Build information obtained with the help of -ldflags
var (
appVersion = "(untracked dev build)" // inferred at startup
devBuild = true // inferred at startup
buildDate string // date -u
gitTag string // git describe --exact-match HEAD 2> /dev/null
gitNearestTag string // git describe --abbrev=0 --tags HEAD
gitCommit string // git rev-parse HEAD
gitShortStat string // git diff-index --shortstat
gitFilesModified string // git diff-index --name-only HEAD
)
// EnableTelemetry defines whether telemetry is enabled in Run. // EnableTelemetry defines whether telemetry is enabled in Run.
var EnableTelemetry = true var EnableTelemetry = true
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment