Skip to content

Commit

Permalink
fix(build): improve handling of build info such as commits or time
Browse files Browse the repository at this point in the history
  • Loading branch information
pcfreak30 committed Jan 12, 2025
1 parent 5bd7fd6 commit 8121583
Showing 1 changed file with 29 additions and 5 deletions.
34 changes: 29 additions & 5 deletions build/build_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,25 @@ func (i *Info) IsRelease() bool {
}

func (i Info) String() string {
commit := i.GetCommit()
shortCommit := commit
if len(commit) > 8 {
shortCommit = commit[:8]
}

return fmt.Sprintf("%s-%s (%s)",
i.GetVersion(),
i.GetCommit()[:8],
shortCommit,
i.GetBuildTime().Format(time.RFC3339))
}

func (i *Info) Short() string {
commit := i.GetCommit()
shortCommit := commit
if len(commit) > 8 {
commit = commit[:8]
shortCommit = commit[:8]
}
return fmt.Sprintf("%s-%s", i.GetVersion(), commit)
return fmt.Sprintf("%s-%s", i.GetVersion(), shortCommit)
}

func (i *Info) JSON() (string, error) {
Expand Down Expand Up @@ -129,7 +136,24 @@ func IsRelease() bool {
func New(version, commit, branch, buildTime, goVersion, platform, architecture string) BuildInfo {
var bTime time.Time
if buildTime != "" {
bTime, _ = time.Parse(time.RFC3339, buildTime)
parsed, err := time.Parse(time.RFC3339, buildTime)
if err == nil {
bTime = parsed
} else {
bTime = time.Now()
}
} else {
bTime = time.Now()
}

if commit == "" {
commit = "unknown"
}
if branch == "" {
branch = "unknown"
}
if version == "" {
version = "develop"
}

return &Info{
Expand All @@ -141,4 +165,4 @@ func New(version, commit, branch, buildTime, goVersion, platform, architecture s
Platform: platform,
Architecture: architecture,
}
}
}

0 comments on commit 8121583

Please sign in to comment.