You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
naza/pkg/bininfo/bininfo.go

58 lines
1.7 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// Copyright 2019, Chef. All rights reserved.
// https://github.com/q191201771/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
// Package bininfo
//
// 将编译时源码的git版本信息当前tagcommit log的sha值和commit message是否有未提交的修改编译时间Go版本编译、运行平台打入程序中
// 编译时传入这些信息的方式见 naza 的编译脚本: https://github.com/q191201771/naza/blob/master/build.sh
//
package bininfo
import (
"fmt"
"runtime"
"strings"
)
var (
// 初始化为 unknown如果编译时没有传入这些值则为 unknown
GitTag = "unknown"
GitCommitLog = "unknown"
GitStatus = "unknown"
BuildTime = "unknown"
BuildGoVersion = "unknown"
)
// 返回单行格式
func StringifySingleLine() string {
return fmt.Sprintf("GitTag=%s. GitCommitLog=%s. GitStatus=%s. BuildTime=%s. GoVersion=%s. runtime=%s/%s.",
GitTag, GitCommitLog, GitStatus, BuildTime, BuildGoVersion, runtime.GOOS, runtime.GOARCH)
}
// 返回多行格式
func StringifyMultiLine() string {
return fmt.Sprintf("GitTag=%s\nGitCommitLog=%s\nGitStatus=%s\nBuildTime=%s\nGoVersion=%s\nruntime=%s/%s\n",
GitTag, GitCommitLog, GitStatus, BuildTime, BuildGoVersion, runtime.GOOS, runtime.GOARCH)
}
// 对一些值做美化处理
func beauty() {
if GitStatus == "" {
// GitStatus 为空时,说明本地源码与最近的 commit 记录一致,无修改
// 为它赋一个特殊值
GitStatus = "cleanly"
} else {
// 将多行结果合并为一行
GitStatus = strings.Replace(strings.Replace(GitStatus, "\r\n", " |", -1), "\n", " |", -1)
}
}
func init() {
beauty()
}