@marcel91200 / caddy Mirror · read-only / commits / e4fac129
Fork 0

Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS

Mirrored from https://github.com/Slazee/caddy.git

Synced Nov 13, 09:15 UTC

core: Set version manually via CustomVersion (#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>

Adam Weinberger committed Oct 5, 2022 at 10:59 UTC e4fac1294f3fcedea5bc94668d56ec64e651bc98
1 file changed +33 -3
caddy.go
+33 -3
@@ -824,6 +824,20 @@ func InstanceID() (uuid.UUID, error) {
824 return uuid.ParseBytes(uuidFileBytes)
825 }
826
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 +
841 // Version returns the Caddy version in a simple/short form, and
842 // a full version string. The short form will not have spaces and
843 // is intended for User-Agent strings and similar, but may be
@@ -833,8 +847,10 @@ func InstanceID() (uuid.UUID, error) {
847 // build info provided by go.mod dependencies; then it tries to
848 // get info from embedded VCS information, which requires having
849 // 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.
854 //
855 // See relevant Go issues: https://github.com/golang/go/issues/29228
856 // and https://github.com/golang/go/issues/50603.
@@ -910,8 +926,22 @@ func Version() (simple, full string) {
926 }
927 }
928
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 +
939 if simple == "" || simple == "(devel)" {
914 - simple = "unknown"
940 + if CustomVersion != "" {
941 + simple = CustomVersion
942 + } else {
943 + simple = "unknown"
944 + }
945 }
946
947 return