Skip to content

Commit e4fac12

Browse files
assistcontrolmholt
andauthored
core: Set version manually via CustomVersion (caddyserver#5072)
* Allow version to be set manually When Caddy is built from a release tarball (as downloaded from GitHub), `caddy version` returns an empty string. This causes confusion for downstream packagers. With this commit, VersionString can be set with eg. go build (...) -ldflags '-X (...).VersionString=v1.2.3' Then the short form version will be "v1.2.3", and the full version string will begin with "v1.2.3 ". * Prefer embedded version, then CustomVersion * Prefer "unknown" for full version over empty Co-authored-by: Matthew Holt <mholt@users.noreply.github.com>
1 parent 2153a81 commit e4fac12

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

caddy.go

+33-3
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,20 @@ func InstanceID() (uuid.UUID, error) {
824824
return uuid.ParseBytes(uuidFileBytes)
825825
}
826826

827+
// CustomVersion is an optional string that overrides Caddy's
828+
// reported version. It can be helpful when downstream packagers
829+
// need to manually set Caddy's version. If no other version
830+
// information is available, the short form version (see
831+
// Version()) will be set to CustomVersion, and the full version
832+
// will include CustomVersion at the beginning.
833+
//
834+
// Set this variable during `go build` with `-ldflags`:
835+
//
836+
// -ldflags '-X github.com/caddyserver/caddy/v2.CustomVersion=v2.6.2'
837+
//
838+
// for example.
839+
var CustomVersion string
840+
827841
// Version returns the Caddy version in a simple/short form, and
828842
// a full version string. The short form will not have spaces and
829843
// is intended for User-Agent strings and similar, but may be
@@ -833,8 +847,10 @@ func InstanceID() (uuid.UUID, error) {
833847
// build info provided by go.mod dependencies; then it tries to
834848
// get info from embedded VCS information, which requires having
835849
// built Caddy from a git repository. If no version is available,
836-
// this function returns "(devel)" becaise Go uses that, but for
837-
// the simple form we change it to "unknown".
850+
// this function returns "(devel)" because Go uses that, but for
851+
// the simple form we change it to "unknown". If still no version
852+
// is available (e.g. no VCS repo), then it will use CustomVersion;
853+
// CustomVersion is always prepended to the full version string.
838854
//
839855
// See relevant Go issues: https://github.com/golang/go/issues/29228
840856
// and https://github.com/golang/go/issues/50603.
@@ -910,8 +926,22 @@ func Version() (simple, full string) {
910926
}
911927
}
912928

929+
if full == "" {
930+
if CustomVersion != "" {
931+
full = CustomVersion
932+
} else {
933+
full = "unknown"
934+
}
935+
} else if CustomVersion != "" {
936+
full = CustomVersion + " " + full
937+
}
938+
913939
if simple == "" || simple == "(devel)" {
914-
simple = "unknown"
940+
if CustomVersion != "" {
941+
simple = CustomVersion
942+
} else {
943+
simple = "unknown"
944+
}
915945
}
916946

917947
return

0 commit comments

Comments
 (0)