| 1 |
// Package meta provides functionality to parse and manage metadata information |
| 2 |
// for Shoutrrr using Go's debug.ReadBuildInfo and GoReleaser build flags. |
| 3 |
package meta |
| 4 |
|
| 5 |
import ( |
| 6 |
"fmt" |
| 7 |
"runtime/debug" |
| 8 |
"time" |
| 9 |
) |
| 10 |
|
| 11 |
// Info holds version information for Shoutrrr. |
| 12 |
type Info struct { |
| 13 |
Version string |
| 14 |
Commit string |
| 15 |
Date string |
| 16 |
} |
| 17 |
|
| 18 |
// Constants for repeated string values. |
| 19 |
const ( |
| 20 |
devVersion = "dev" |
| 21 |
unknownValue = "unknown" |
| 22 |
trueValue = "true" |
| 23 |
commitSHALength = 7 // Length to shorten Git commit SHA |
| 24 |
) |
| 25 |
|
| 26 |
// These values are populated by GoReleaser during release builds. |
| 27 |
var ( |
| 28 |
// Version is the Shoutrrr version (e.g., "v0.0.1"). |
| 29 |
Version = devVersion |
| 30 |
// Commit is the Git commit SHA (e.g., "abc123"). |
| 31 |
Commit = unknownValue |
| 32 |
// Date is the build or commit timestamp in RFC3339 format (e.g., "2025-05-07T00:00:00Z"). |
| 33 |
Date = unknownValue |
| 34 |
) |
| 35 |
|
| 36 |
// GetMetaStr returns the formatted version string, including commit info only if available. |
| 37 |
func GetMetaStr() string { |
| 38 |
version := GetVersion() |
| 39 |
date := GetDate() |
| 40 |
commit := GetCommit() |
| 41 |
|
| 42 |
if commit == unknownValue { |
| 43 |
return fmt.Sprintf("%s (Built on %s)", version, date) |
| 44 |
} |
| 45 |
|
| 46 |
return fmt.Sprintf("%s (Built on %s from Git SHA %s)", version, date, commit) |
| 47 |
} |
| 48 |
|
| 49 |
// GetVersion returns the version string, using debug.ReadBuildInfo for source builds |
| 50 |
// or GoReleaser variables for release builds. |
| 51 |
func GetVersion() string { |
| 52 |
version := Version |
| 53 |
|
| 54 |
// If building from source (not GoReleaser), try to get version from debug.ReadBuildInfo |
| 55 |
if version == devVersion || version == "" { |
| 56 |
if info, ok := debug.ReadBuildInfo(); ok { |
| 57 |
// Get the module version (e.g., v1.1.4 or v1.1.4+dirty) |
| 58 |
version = info.Main.Version |
| 59 |
if version == "(devel)" || version == "" { |
| 60 |
version = devVersion |
| 61 |
} |
| 62 |
// Check for dirty state |
| 63 |
for _, setting := range info.Settings { |
| 64 |
if setting.Key == "vcs.modified" && setting.Value == trueValue && |
| 65 |
version != unknownValue && !contains(version, "+dirty") { |
| 66 |
version += "+dirty" |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
} else { |
| 71 |
// GoReleaser provides a valid version without 'v' prefix, so add it |
| 72 |
if version != "" && version != "v" { |
| 73 |
version = "v" + version |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
// Fallback default if still unset or invalid |
| 78 |
if version == "" || version == devVersion || version == "v" { |
| 79 |
return unknownValue |
| 80 |
} |
| 81 |
|
| 82 |
return version |
| 83 |
} |
| 84 |
|
| 85 |
// GetCommit returns the commit SHA, using debug.ReadBuildInfo for source builds |
| 86 |
// or GoReleaser variables for release builds. |
| 87 |
func GetCommit() string { |
| 88 |
// Return Commit if set by GoReleaser (non-empty and not "unknown") |
| 89 |
if Commit != unknownValue && Commit != "" { |
| 90 |
if len(Commit) >= commitSHALength { |
| 91 |
return Commit[:commitSHALength] |
| 92 |
} |
| 93 |
|
| 94 |
return Commit |
| 95 |
} |
| 96 |
|
| 97 |
// Try to get commit from debug.ReadBuildInfo for source builds |
| 98 |
if info, ok := debug.ReadBuildInfo(); ok { |
| 99 |
for _, setting := range info.Settings { |
| 100 |
if setting.Key == "vcs.revision" && setting.Value != "" { |
| 101 |
if len(setting.Value) >= commitSHALength { |
| 102 |
return setting.Value[:commitSHALength] |
| 103 |
} |
| 104 |
|
| 105 |
return setting.Value |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
// Fallback to unknown if no commit is found |
| 111 |
return unknownValue |
| 112 |
} |
| 113 |
|
| 114 |
// GetDate returns the build or commit date, using debug.ReadBuildInfo for source builds |
| 115 |
// or GoReleaser variables for release builds. |
| 116 |
func GetDate() string { |
| 117 |
date := Date |
| 118 |
|
| 119 |
// If building from source (not GoReleaser), try to get date from debug.ReadBuildInfo |
| 120 |
if date == unknownValue || date == "" { |
| 121 |
if info, ok := debug.ReadBuildInfo(); ok { |
| 122 |
for _, setting := range info.Settings { |
| 123 |
if setting.Key == "vcs.time" { |
| 124 |
if t, err := time.Parse(time.RFC3339, setting.Value); err == nil { |
| 125 |
return t.Format("2006-01-02") // Shorten to YYYY-MM-DD |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
// Fallback to current date if no VCS time is available |
| 131 |
return time.Now().UTC().Format("2006-01-02") |
| 132 |
} |
| 133 |
|
| 134 |
// Shorten date if provided by GoReleaser |
| 135 |
if date != "" && date != unknownValue { |
| 136 |
if t, err := time.Parse(time.RFC3339, date); err == nil { |
| 137 |
return t.Format("2006-01-02") // Shorten to YYYY-MM-DD |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
// Fallback to current date if date is invalid |
| 142 |
return time.Now().UTC().Format("2006-01-02") |
| 143 |
} |
| 144 |
|
| 145 |
// GetMetaInfo returns version information by combining GetVersion, GetCommit, and GetDate. |
| 146 |
func GetMetaInfo() Info { |
| 147 |
return Info{ |
| 148 |
Version: GetVersion(), |
| 149 |
Commit: GetCommit(), |
| 150 |
Date: GetDate(), |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
// contains checks if a string contains a substring. |
| 155 |
func contains(s, substr string) bool { |
| 156 |
for i := 0; i <= len(s)-len(substr); i++ { |
| 157 |
if s[i:i+len(substr)] == substr { |
| 158 |
return true |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
return false |
| 163 |
} |