Debian Codegraph @

internal/meta/meta.go

raw
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 // UserAgent returns the HTTP User-Agent used by Shoutrrr services.
37 //
38 // Returns:
39 // - The string "shoutrrr/" followed by the Version build variable.
40 func UserAgent() string {
41 return "shoutrrr/" + Version
42 }
43
44 // GetMetaStr returns the formatted version string, including commit info only if available.
45 func GetMetaStr() string {
46 version := GetVersion()
47 date := GetDate()
48 commit := GetCommit()
49
50 if commit == unknownValue {
51 return fmt.Sprintf("%s (Built on %s)", version, date)
52 }
53
54 return fmt.Sprintf("%s (Built on %s from Git SHA %s)", version, date, commit)
55 }
56
57 // GetVersion returns the version string, using debug.ReadBuildInfo for source builds
58 // or GoReleaser variables for release builds.
59 func GetVersion() string {
60 version := Version
61
62 // If building from source (not GoReleaser), try to get version from debug.ReadBuildInfo
63 if version == devVersion || version == "" {
64 if info, ok := debug.ReadBuildInfo(); ok {
65 // Get the module version (e.g., v1.1.4 or v1.1.4+dirty)
66 version = info.Main.Version
67 if version == "(devel)" || version == "" {
68 version = devVersion
69 }
70 // Check for dirty state
71 for _, setting := range info.Settings {
72 if setting.Key == "vcs.modified" && setting.Value == trueValue &&
73 version != unknownValue && !contains(version, "+dirty") {
74 version += "+dirty"
75 }
76 }
77 }
78 } else {
79 // GoReleaser provides a valid version without 'v' prefix, so add it
80 if version != "" && version != "v" {
81 version = "v" + version
82 }
83 }
84
85 // Fallback default if still unset or invalid
86 if version == "" || version == devVersion || version == "v" {
87 return unknownValue
88 }
89
90 return version
91 }
92
93 // GetCommit returns the commit SHA, using debug.ReadBuildInfo for source builds
94 // or GoReleaser variables for release builds.
95 func GetCommit() string {
96 // Return Commit if set by GoReleaser (non-empty and not "unknown")
97 if Commit != unknownValue && Commit != "" {
98 if len(Commit) >= commitSHALength {
99 return Commit[:commitSHALength]
100 }
101
102 return Commit
103 }
104
105 // Try to get commit from debug.ReadBuildInfo for source builds
106 if info, ok := debug.ReadBuildInfo(); ok {
107 for _, setting := range info.Settings {
108 if setting.Key == "vcs.revision" && setting.Value != "" {
109 if len(setting.Value) >= commitSHALength {
110 return setting.Value[:commitSHALength]
111 }
112
113 return setting.Value
114 }
115 }
116 }
117
118 // Fallback to unknown if no commit is found
119 return unknownValue
120 }
121
122 // GetDate returns the build or commit date, using debug.ReadBuildInfo for source builds
123 // or GoReleaser variables for release builds.
124 func GetDate() string {
125 date := Date
126
127 // If building from source (not GoReleaser), try to get date from debug.ReadBuildInfo
128 if date == unknownValue || date == "" {
129 if info, ok := debug.ReadBuildInfo(); ok {
130 for _, setting := range info.Settings {
131 if setting.Key == "vcs.time" {
132 if t, err := time.Parse(time.RFC3339, setting.Value); err == nil {
133 return t.Format("2006-01-02") // Shorten to YYYY-MM-DD
134 }
135 }
136 }
137 }
138 // Fallback to current date if no VCS time is available
139 return time.Now().UTC().Format("2006-01-02")
140 }
141
142 // Shorten date if provided by GoReleaser
143 if date != "" && date != unknownValue {
144 if t, err := time.Parse(time.RFC3339, date); err == nil {
145 return t.Format("2006-01-02") // Shorten to YYYY-MM-DD
146 }
147 }
148
149 // Fallback to current date if date is invalid
150 return time.Now().UTC().Format("2006-01-02")
151 }
152
153 // GetMetaInfo returns version information by combining GetVersion, GetCommit, and GetDate.
154 func GetMetaInfo() Info {
155 return Info{
156 Version: GetVersion(),
157 Commit: GetCommit(),
158 Date: GetDate(),
159 }
160 }
161
162 // contains checks if a string contains a substring.
163 func contains(s, substr string) bool {
164 for i := 0; i <= len(s)-len(substr); i++ {
165 if s[i:i+len(substr)] == substr {
166 return true
167 }
168 }
169
170 return false
171 }