mirror of
https://github.com/caddyserver/caddy.git
synced 2025-02-27 10:05:42 +08:00
main: Use embedded version, debug.BuildInfo
This commit is contained in:
parent
13d0454f71
commit
bf6ec2bbfd
@ -25,6 +25,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"runtime/debug"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
@ -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() {
|
|||||||
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() {
|
|||||||
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() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 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,26 +277,26 @@ 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() {
|
||||||
if fromJSON {
|
if fromJSON {
|
||||||
@ -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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user