154 lines
2.8 KiB
Go
154 lines
2.8 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"os/exec"
|
||
|
"strings"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
func GetCommitHash(hashfmt string) (string, error) {
|
||
|
gitCmd := exec.Command("git", "log", "-n", "1", "--pretty=format:"+hashfmt)
|
||
|
gitCmd.Dir = ModulePath
|
||
|
|
||
|
stdout, err := gitCmd.StdoutPipe()
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
if err := gitCmd.Start(); err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
hashData, err := io.ReadAll(stdout)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
if err := gitCmd.Wait(); err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
trimmedHash := strings.TrimSpace(string(hashData))
|
||
|
if trimmedHash == "" {
|
||
|
return "", errors.New("no valid hash found")
|
||
|
}
|
||
|
|
||
|
return trimmedHash, nil
|
||
|
}
|
||
|
|
||
|
func GetVersionTag() (string, error) {
|
||
|
gitCmd := exec.Command("git", "log", "-n", "1", "--pretty=format:%(describe:tags)")
|
||
|
gitCmd.Dir = ModulePath
|
||
|
|
||
|
stdout, err := gitCmd.StdoutPipe()
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
if err := gitCmd.Start(); err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
tagData, err := io.ReadAll(stdout)
|
||
|
if err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
if err := gitCmd.Wait(); err != nil {
|
||
|
return "", err
|
||
|
}
|
||
|
|
||
|
trimmedTag := strings.TrimSpace(string(tagData))
|
||
|
if trimmedTag == "" {
|
||
|
return "", errors.New("no valid version tag found")
|
||
|
}
|
||
|
|
||
|
return trimmedTag, nil
|
||
|
}
|
||
|
|
||
|
func WorkTreeChanged() bool {
|
||
|
gitCmd := exec.Command("git", "status", "--porcelain")
|
||
|
gitCmd.Dir = ModulePath
|
||
|
|
||
|
stdout, err := gitCmd.StdoutPipe()
|
||
|
if err != nil {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
if err := gitCmd.Start(); err != nil {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
data, err := io.ReadAll(stdout)
|
||
|
if err != nil {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
if err := gitCmd.Wait(); err != nil {
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
return len(data) != 0
|
||
|
}
|
||
|
|
||
|
func GatherMetaData() {
|
||
|
if !WorkTreeChanged() {
|
||
|
VersionTag, _ = GetVersionTag()
|
||
|
CommitHash, _ = GetCommitHash("%H")
|
||
|
} else {
|
||
|
hash, _ := GetCommitHash("%h")
|
||
|
VersionTag = fmt.Sprintf("(devel-%s)", hash)
|
||
|
}
|
||
|
|
||
|
BuildTime = time.Now().Format(time.RFC3339)
|
||
|
}
|
||
|
|
||
|
func BuildLdFlags(os, arch string) string {
|
||
|
b := &strings.Builder{}
|
||
|
|
||
|
b.WriteString("-ldflags=")
|
||
|
|
||
|
if !*KeepDebugFlags {
|
||
|
b.WriteString("-s -w")
|
||
|
}
|
||
|
|
||
|
if CommitHash != "" {
|
||
|
b.WriteString(" -X ")
|
||
|
b.WriteString("git.milar.in/milarin/buildinfo.Commit")
|
||
|
b.WriteString("=")
|
||
|
b.WriteString(CommitHash)
|
||
|
}
|
||
|
|
||
|
if VersionTag != "" {
|
||
|
b.WriteString(" -X ")
|
||
|
b.WriteString("git.milar.in/milarin/buildinfo.Version")
|
||
|
b.WriteString("=")
|
||
|
b.WriteString(VersionTag)
|
||
|
}
|
||
|
|
||
|
b.WriteString(" -X ")
|
||
|
b.WriteString("git.milar.in/milarin/buildinfo.Name")
|
||
|
b.WriteString("=")
|
||
|
b.WriteString(ProjectName)
|
||
|
|
||
|
b.WriteString(" -X ")
|
||
|
b.WriteString("git.milar.in/milarin/buildinfo.BuildTime")
|
||
|
b.WriteString("=")
|
||
|
b.WriteString(BuildTime)
|
||
|
|
||
|
b.WriteString(" -X ")
|
||
|
b.WriteString("git.milar.in/milarin/buildinfo.OS")
|
||
|
b.WriteString("=")
|
||
|
b.WriteString(os)
|
||
|
|
||
|
b.WriteString(" -X ")
|
||
|
b.WriteString("git.milar.in/milarin/buildinfo.Arch")
|
||
|
b.WriteString("=")
|
||
|
b.WriteString(arch)
|
||
|
|
||
|
return b.String()
|
||
|
}
|