From f83f579dea43727c217904f400f8b066b1b12939 Mon Sep 17 00:00:00 2001 From: 6543 <6543@obermui.de> Date: Sat, 18 Jun 2022 22:34:18 +0800 Subject: [PATCH] Show more version info (#486) Reviewed-on: https://gitea.com/gitea/tea/pulls/486 Reviewed-by: Lunny Xiao Reviewed-by: Wim <42wim@noreply.gitea.io> Co-authored-by: 6543 <6543@obermui.de> Co-committed-by: 6543 <6543@obermui.de> --- Makefile | 3 ++- main.go | 22 +++++++++++++++++----- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 38246fd..f616587 100644 --- a/Makefile +++ b/Makefile @@ -24,7 +24,8 @@ endif TEA_VERSION_TAG ?= $(shell sed 's/+/_/' <<< $(TEA_VERSION)) TAGS ?= -LDFLAGS := -X "main.Version=$(TEA_VERSION)" -X "main.Tags=$(TAGS)" -s -w +SDK ?= $(shell $(GO) list -f '{{.Version}}' -m code.gitea.io/sdk/gitea) +LDFLAGS := -X "main.Version=$(TEA_VERSION)" -X "main.Tags=$(TAGS)" -X "main.SDK=$(SDK)" -s -w # override to allow passing additional goflags via make CLI override GOFLAGS := $(GOFLAGS) -mod=vendor -tags '$(TAGS)' -ldflags '$(LDFLAGS)' diff --git a/main.go b/main.go index fe7a65a..1fc60bb 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ package main // import "code.gitea.io/tea" import ( "fmt" "os" + "runtime" "strings" "code.gitea.io/tea/cmd" @@ -21,6 +22,9 @@ var Version = "development" // Tags holds the build tags used var Tags = "" +// SDK holds the sdk version from go.mod +var SDK = "" + func main() { // make parsing tea --version easier, by printing /just/ the version string cli.VersionPrinter = func(c *cli.Context) { fmt.Fprintln(c.App.Writer, c.App.Version) } @@ -30,7 +34,7 @@ func main() { app.Usage = "command line tool to interact with Gitea" app.Description = appDescription app.CustomAppHelpTemplate = helpTemplate - app.Version = Version + formatBuiltWith(Tags) + app.Version = formatVersion() app.Commands = []*cli.Command{ &cmd.CmdLogin, &cmd.CmdLogout, @@ -61,12 +65,20 @@ func main() { } } -func formatBuiltWith(Tags string) string { - if len(Tags) == 0 { - return "" +func formatVersion() string { + version := fmt.Sprintf("Version: %s\tgolang: %s", + bold(Version), + strings.ReplaceAll(runtime.Version(), "go", "")) + + if len(Tags) != 0 { + version += fmt.Sprintf("\tbuilt with: %s", strings.Replace(Tags, " ", ", ", -1)) } - return " built with: " + strings.Replace(Tags, " ", ", ", -1) + if len(SDK) != 0 { + version += fmt.Sprintf("\tgo-sdk: %s", SDK) + } + + return version } var appDescription = `tea is a productivity helper for Gitea. It can be used to manage most entities on