Files
gitea-tea/internal/version/version.go
T
bircni 83dd4f7938 feat(version): print the version like gh version
tea version and --version printed "Version: development<TAB>golang:
1.27.2". They now print "tea version 1.0.0 (2026-10-01)", the release
notes URL for tagged releases, and "built with go1.27.2" with the build
tags. Builds from go install take the module version from the build
info, and the date comes from the commit the binary was built from.

Signed-off-by: bircni <bircni@icloud.com>
2026-10-11 23:50:46 +02:00

65 lines
2.0 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
// Package version reports the version of the tea build.
package version
import (
"regexp"
"runtime"
"runtime/debug"
"strings"
"time"
)
// Version holds the current tea version.
// This is set at build time via ldflags.
// If the Version is moved to another package or name changed,
// build flags in .goreleaser.yaml or Makefile need to be updated accordingly.
var Version = "development"
// Tags holds the build tags used
var Tags = ""
// release matches the versions of tagged releases, such as 1.0.0 or 1.1.0-rc1; builds between
// releases carry a +commit suffix.
var release = regexp.MustCompile(`^\d+\.\d+\.\d+(-[0-9A-Za-z.]+)?$`)
// Format describes the build like gh version: the version and its date, the release notes of a
// release, and the Go version and build tags:
//
// tea version 1.0.0 (2026-10-01)
// https://gitea.com/gitea/tea/releases/tag/v1.0.0
// built with go1.27.2
func Format() string {
info, _ := debug.ReadBuildInfo()
return format(Version, Tags, runtime.Version(), info)
}
func format(version, tags, goVersion string, info *debug.BuildInfo) string {
date := ""
if info != nil {
// go install gitea.dev/tea@v1.0.0 stamps the module version instead of the ldflags.
if version == "development" && info.Main.Version != "" && info.Main.Version != "(devel)" {
version = strings.TrimPrefix(info.Main.Version, "v")
}
for _, setting := range info.Settings {
if setting.Key != "vcs.time" {
continue
}
if built, err := time.Parse(time.RFC3339, setting.Value); err == nil {
date = " (" + built.UTC().Format(time.DateOnly) + ")"
}
}
}
text := "tea version " + version + date + "\n"
if release.MatchString(version) {
text += "https://gitea.com/gitea/tea/releases/tag/v" + version + "\n"
}
text += "built with " + goVersion
if tags = strings.Join(strings.Fields(tags), ", "); tags != "" {
text += " (tags: " + tags + ")"
}
return text
}