[MM-53256] Use buildinfo instead of env variables for mmctl (#23794)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
44256326a1
Коммит
86b965a3c4
@@ -34,12 +34,6 @@ BUILD_NUMBER ?= $(BUILD_NUMBER:)
|
|||||||
BUILD_DATE = $(shell date -u)
|
BUILD_DATE = $(shell date -u)
|
||||||
BUILD_HASH = $(shell git rev-parse HEAD)
|
BUILD_HASH = $(shell git rev-parse HEAD)
|
||||||
|
|
||||||
# treestate
|
|
||||||
GIT_TREESTATE = clean
|
|
||||||
DIFF = $(shell git diff --quiet >/dev/null 2>&1; if [ $$? -eq 1 ]; then echo "1"; fi)
|
|
||||||
ifeq ($(DIFF), 1)
|
|
||||||
GIT_TREESTATE = dirty
|
|
||||||
endif
|
|
||||||
|
|
||||||
# Docker
|
# Docker
|
||||||
export COMPOSE_PROJECT_NAME=mattermost-server
|
export COMPOSE_PROJECT_NAME=mattermost-server
|
||||||
@@ -62,9 +56,8 @@ GOTESTSUM_JSONFILE ?= gotestsum.json
|
|||||||
MMCTL_BUILD_TAGS =
|
MMCTL_BUILD_TAGS =
|
||||||
MMCTL_TESTFLAGS ?= -timeout 30m
|
MMCTL_TESTFLAGS ?= -timeout 30m
|
||||||
MMCTL_PKG = github.com/mattermost/mattermost/server/v8/cmd/mmctl/commands
|
MMCTL_PKG = github.com/mattermost/mattermost/server/v8/cmd/mmctl/commands
|
||||||
LDFLAGS += -X "$(MMCTL_PKG).gitCommit=$(BUILD_HASH)"
|
MMCTL_BUILD_DATE = $(shell date -u +'%Y-%m-%dT%H:%M:%SZ')
|
||||||
LDFLAGS += -X "$(MMCTL_PKG).gitTreeState=$(GIT_TREESTATE)"
|
MMCTL_LDFLAGS += -X "$(MMCTL_PKG).buildDate=$(MMCTL_BUILD_DATE)"
|
||||||
LDFLAGS += -X "$(MMCTL_PKG).buildDate=$(BUILD_DATE)"
|
|
||||||
|
|
||||||
# Enterprise
|
# Enterprise
|
||||||
BUILD_ENTERPRISE_DIR ?= ../../enterprise
|
BUILD_ENTERPRISE_DIR ?= ../../enterprise
|
||||||
@@ -91,18 +84,6 @@ endif
|
|||||||
|
|
||||||
# Webapp
|
# Webapp
|
||||||
BUILD_WEBAPP_DIR ?= ../webapp
|
BUILD_WEBAPP_DIR ?= ../webapp
|
||||||
BUILD_CLIENT = false
|
|
||||||
BUILD_HASH_CLIENT = independent
|
|
||||||
ifneq ($(wildcard $(BUILD_WEBAPP_DIR)/.),)
|
|
||||||
ifeq ($(BUILD_CLIENT),true)
|
|
||||||
BUILD_CLIENT = true
|
|
||||||
BUILD_HASH_CLIENT = $(shell cd $(BUILD_WEBAPP_DIR) && git rev-parse HEAD)
|
|
||||||
else
|
|
||||||
BUILD_CLIENT = false
|
|
||||||
endif
|
|
||||||
else
|
|
||||||
BUILD_CLIENT = false
|
|
||||||
endif
|
|
||||||
|
|
||||||
# We need current user's UID for `run-haserver` so docker compose does not run server
|
# We need current user's UID for `run-haserver` so docker compose does not run server
|
||||||
# as root and mess up file permissions for devs. When running like this HOME will be blank
|
# as root and mess up file permissions for devs. When running like this HOME will be blank
|
||||||
@@ -813,7 +794,7 @@ ifeq ($(BUILD_ENTERPRISE_READY),true)
|
|||||||
endif
|
endif
|
||||||
|
|
||||||
mmctl-build: ## Compiles and generates the mmctl binary
|
mmctl-build: ## Compiles and generates the mmctl binary
|
||||||
go build -trimpath -ldflags '$(LDFLAGS)' -o bin/mmctl ./cmd/mmctl
|
go build -trimpath -ldflags '$(MMCTL_LDFLAGS)' -o bin/mmctl ./cmd/mmctl
|
||||||
|
|
||||||
mmctl-docs: ## Generate the mmctl docs
|
mmctl-docs: ## Generate the mmctl docs
|
||||||
rm -rf ./cmd/mmctl/docs
|
rm -rf ./cmd/mmctl/docs
|
||||||
|
|||||||
@@ -5,22 +5,19 @@ package commands
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime"
|
"runtime/debug"
|
||||||
|
|
||||||
"github.com/mattermost/mattermost/server/public/model"
|
"github.com/mattermost/mattermost/server/public/model"
|
||||||
"github.com/mattermost/mattermost/server/v8/cmd/mmctl/printer"
|
"github.com/mattermost/mattermost/server/v8/cmd/mmctl/printer"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Version = model.CurrentVersion
|
Version = model.CurrentVersion
|
||||||
// SHA1 from git, output of $(git rev-parse HEAD)
|
|
||||||
gitCommit = "dev mode"
|
|
||||||
// State of git tree, either "clean" or "dirty"
|
|
||||||
gitTreeState = "unknown"
|
|
||||||
// Build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
|
// Build date in ISO8601 format, output of $(date -u +'%Y-%m-%dT%H:%M:%SZ')
|
||||||
buildDate = "unknown"
|
buildDate = "dev"
|
||||||
)
|
)
|
||||||
|
|
||||||
var VersionCmd = &cobra.Command{
|
var VersionCmd = &cobra.Command{
|
||||||
@@ -34,31 +31,73 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func versionCmdF(cmd *cobra.Command, args []string) error {
|
func versionCmdF(cmd *cobra.Command, args []string) error {
|
||||||
v := getVersionInfo()
|
v, err := getVersionInfo()
|
||||||
printer.PrintT("mmctl:\nVersion:\t{{.Version}}\nGitCommit:\t{{.GitCommit}}"+
|
if err != nil {
|
||||||
"\nGitTreeState:\t{{.GitTreeState}}\nBuildDate:\t{{.BuildDate}}\nGoVersion:\t{{.GoVersion}}"+
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
printer.PrintT("mmctl:\nVersion:\t{{.Version}}\nBuiltDate:\t{{.BuildDate}}\nCommitDate:\t{{.CommitDate}}\nGitCommit:\t{{.GitCommit}}"+
|
||||||
|
"\nGitTreeState:\t{{.GitTreeState}}\nGoVersion:\t{{.GoVersion}}"+
|
||||||
"\nCompiler:\t{{.Compiler}}\nPlatform:\t{{.Platform}}", v)
|
"\nCompiler:\t{{.Compiler}}\nPlatform:\t{{.Platform}}", v)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type Info struct {
|
type Info struct {
|
||||||
Version string
|
Version string
|
||||||
|
BuildDate string
|
||||||
|
CommitDate string
|
||||||
GitCommit string
|
GitCommit string
|
||||||
GitTreeState string
|
GitTreeState string
|
||||||
BuildDate string
|
|
||||||
GoVersion string
|
GoVersion string
|
||||||
Compiler string
|
Compiler string
|
||||||
Platform string
|
Platform string
|
||||||
}
|
}
|
||||||
|
|
||||||
func getVersionInfo() Info {
|
func getVersionInfo() (*Info, error) {
|
||||||
return Info{
|
info, ok := debug.ReadBuildInfo()
|
||||||
Version: Version,
|
if !ok {
|
||||||
GitCommit: gitCommit,
|
return nil, errors.New("failed to get build info")
|
||||||
GitTreeState: gitTreeState,
|
|
||||||
BuildDate: buildDate,
|
|
||||||
GoVersion: runtime.Version(),
|
|
||||||
Compiler: runtime.Compiler,
|
|
||||||
Platform: fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
revision = "dev"
|
||||||
|
gitTreeState = "dev"
|
||||||
|
commitDate = "dev"
|
||||||
|
|
||||||
|
os string
|
||||||
|
arch string
|
||||||
|
compiler string
|
||||||
|
)
|
||||||
|
|
||||||
|
for _, s := range info.Settings {
|
||||||
|
switch s.Key {
|
||||||
|
case "vcs.revision":
|
||||||
|
revision = s.Value
|
||||||
|
case "vcs.time":
|
||||||
|
commitDate = s.Value
|
||||||
|
case "vcs.modified":
|
||||||
|
if s.Value == "true" {
|
||||||
|
gitTreeState = "dirty"
|
||||||
|
} else {
|
||||||
|
gitTreeState = "clean"
|
||||||
|
}
|
||||||
|
case "GOOS":
|
||||||
|
os = s.Value
|
||||||
|
case "GOARCH":
|
||||||
|
arch = s.Value
|
||||||
|
case "-compiler":
|
||||||
|
compiler = s.Value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Info{
|
||||||
|
Version: Version,
|
||||||
|
BuildDate: buildDate,
|
||||||
|
CommitDate: commitDate,
|
||||||
|
GitCommit: revision,
|
||||||
|
GitTreeState: gitTreeState,
|
||||||
|
GoVersion: info.GoVersion,
|
||||||
|
Compiler: compiler,
|
||||||
|
Platform: fmt.Sprintf("%s/%s", arch, os),
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
29
server/cmd/mmctl/commands/version_test.go
Обычный файл
29
server/cmd/mmctl/commands/version_test.go
Обычный файл
@@ -0,0 +1,29 @@
|
|||||||
|
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
||||||
|
// See LICENSE.txt for license information.
|
||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/mattermost/mattermost/server/v8/cmd/mmctl/printer"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (s *MmctlUnitTestSuite) TestVersionCmd() {
|
||||||
|
printer.Clean()
|
||||||
|
printer.SetFormat(printer.FormatPlain)
|
||||||
|
|
||||||
|
err := versionCmdF(&cobra.Command{}, []string{})
|
||||||
|
s.Require().NoError(err)
|
||||||
|
s.Require().Len(printer.GetErrorLines(), 0)
|
||||||
|
s.Require().Len(printer.GetLines(), 1)
|
||||||
|
line := printer.GetLines()[0]
|
||||||
|
s.Require().Contains(line, "mmctl:")
|
||||||
|
s.Require().Contains(line, "Version:")
|
||||||
|
s.Require().Contains(line, "BuiltDate:")
|
||||||
|
s.Require().Contains(line, "CommitDate:")
|
||||||
|
s.Require().Contains(line, "GitTreeState:")
|
||||||
|
s.Require().Contains(line, "GoVersion:")
|
||||||
|
s.Require().Contains(line, " go1.")
|
||||||
|
s.Require().Contains(line, "Compiler:")
|
||||||
|
s.Require().Contains(line, "Platform:")
|
||||||
|
}
|
||||||
Ссылка в новой задаче
Block a user