-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta.go
575 lines (482 loc) · 13.7 KB
/
meta.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// Copyright Josh Komoroske. All rights reserved.
// Use of this source code is governed by the MIT license,
// a copy of which can be found in the LICENSE.txt file.
// Package meta provides common application metadata for use with go build and
// ldflags. This package is intended to be imported, where variable values can
// be set by using -X arguments, tp the -ldflags argument, when running go
// build. See https://pkg.go.dev/cmd/go and https://pkg.go.dev/cmd/link.
//
// List of variable names:
//
// xiam.li/meta.author
// xiam.li/meta.author_url
// xiam.li/meta.copyright
// xiam.li/meta.date
// xiam.li/meta.desc
// xiam.li/meta.dev
// xiam.li/meta.docs
// xiam.li/meta.license
// xiam.li/meta.license_url
// xiam.li/meta.name
// xiam.li/meta.note
// xiam.li/meta.sha
// xiam.li/meta.src
// xiam.li/meta.title
// xiam.li/meta.url
// xiam.li/meta.version
package meta
import (
u "net/url"
"runtime"
"time"
)
// Arch is the architecture target that the application is running on.
func Arch() string {
return runtime.GOARCH
}
// author is the name of the application author. May contain their name, email
// address, or optionally both.
//
// Variable name:
//
// xiam.li/meta.author
//
// Examples:
//
// -ldflags "-X 'xiam.li/meta.author=John Doe'"
// -ldflags "-X 'xiam.li/[email protected]'"
// -ldflags "-X 'xiam.li/meta.author=Jane Doe <[email protected]>'"
var author string
var authorParsed, authorEmailParsed = mustAuthor("xiam.li/meta.author", author)
// Author is the name of the application author.
func Author() string {
return authorParsed
}
// AuthorOr is the name of the application author, or the given default value if not set.
func AuthorOr(defaultValue string) string {
if authorParsed == "" {
return defaultValue
}
return authorParsed
}
// AuthorEmail is the email address for the application author.
func AuthorEmail() string {
return authorEmailParsed
}
// AuthorEmailOr is the email address for the application author, or the given default value if not set.
func AuthorEmailOr(defaultValue string) string {
if authorEmailParsed == "" {
return defaultValue
}
return authorEmailParsed
}
// author_url is a URL for the application author. Typically links to the
// author's personal homepage or Github profile.
//
// Variable name:
//
// xiam.li/meta.author_url
//
// Examples:
//
// -ldflags "-X 'xiam.li/meta.author_url=https://example.com/profile'"
var author_url string
var authorURLParsed = mustURL("xiam.li/meta.author_url", author_url)
// AuthorURL is the homepage URL for the application author.
func AuthorURL() *u.URL {
return authorURLParsed
}
// AuthorURLOr is the homepage URL for the application author, or the given default value if not set.
func AuthorURLOr(defaultValue string) *u.URL {
if authorURLParsed == nil {
return mustURL("xiam.li/meta.author_url", defaultValue)
}
return authorURLParsed
}
// copyright is the copyright for the application. Typically the name if the
// author or organization, sometimes prefixed with a year or year range.
//
// Variable name:
//
// xiam.li/meta.copyright
//
// Examples:
//
// -ldflags "-X 'xiam.li/meta.copyright=John Doe'"
// -ldflags "-X 'xiam.li/meta.copyright=2021 Jane Doe'"
// -ldflags "-X 'xiam.li/meta.copyright=2019-2021 Jim Doe'"
var copyright string
// Copyright is the copyright for the application.
func Copyright() string {
return copyright
}
// CopyrightOr is the copyright for the application, or the given default value if not set.
func CopyrightOr(defaultValue string) string {
if copyright == "" {
return defaultValue
}
return copyright
}
// date is the time that the application was built. Supports several common
// formats.
//
// Variable name:
//
// xiam.li/meta.date
//
// Examples:
//
// -ldflags "-X 'xiam.li/meta.date=$(date -R)'"
// -ldflags "-X 'xiam.li/meta.date=Fri, 23 Aug 2019 11:00:00 -0700'"
// -ldflags "-X 'xiam.li/meta.date=$(date -Iseconds)'"
// -ldflags "-X 'xiam.li/meta.date=2019-08-23T11:00:00-07:00'"
// -ldflags "-X 'xiam.li/meta.date=$(date -u +%Y-%m-%dT%H:%M:%SZ)'"
// -ldflags "-X 'xiam.li/meta.date=2019-08-23T18:00:00Z'"
var date string
var dateParsed = mustTime("xiam.li/meta.date", date)
// Date is the time at which the application was built.
func Date() *time.Time {
return dateParsed
}
// DateOr is the time at which the application was built, or the given default value if not set.
func DateOr(defaultValue time.Time) *time.Time {
if dateParsed == nil {
return &defaultValue
}
return dateParsed
}
// DateFormat is the time at which the application was built, formatted using
// the given layout.
func DateFormat(layout string) string {
if dateParsed == nil {
return ""
}
return dateParsed.Format(layout)
}
// DateFormatOr is the time at which the application was built, formatted using
// the given layout, or the given default value if not set.
func DateFormatOr(layout string, defaultValue string) string {
if dateParsed == nil {
return defaultValue
}
return dateParsed.Format(layout)
}
// desc is a description for the application. Typically a longer statement
// describing what the application does.
//
// Variable name:
//
// xiam.li/meta.desc
//
// Examples:
//
// -ldflags "-X 'xiam.li/meta.desc=A super simple demonstration application'"
var desc string
// Description is the description of the application.
func Description() string {
return desc
}
// DescriptionOr is the description of the application, or the given default value if not set.
func DescriptionOr(defaultValue string) string {
if desc == "" {
return defaultValue
}
return desc
}
// dev is the development status for the application. An application in
// development mode may indicate that it's using experimental or untested
// features, and should be used with caution.
//
// Variable name:
//
// xiam.li/meta.dev
//
// Examples:
//
// -ldflags "-X 'xiam.li/meta.dev=true'"
var dev string
var devParsed = mustBool("xiam.li/meta.dev", dev)
// Development is the development status for the application.
func Development() bool {
return devParsed
}
// docs is a URL for application documentation. Typically links to a page where
// a user can find technical documentation.
//
// Variable name:
//
// xiam.li/meta.docs
//
// Examples:
//
// -ldflags "-X 'xiam.li/meta.docs=https://example.com/demo/README.md'"
var docs string
var docsParsed = mustURL("xiam.li/meta.docs", docs)
// Docs is the documentation URL for the application.
func Docs() *u.URL {
return docsParsed
}
// DocsOr is the documentation URL for the application, or the given default value if not set.
func DocsOr(defaultValue string) *u.URL {
if docsParsed == nil {
return mustURL("xiam.li/meta.docs", defaultValue)
}
return docsParsed
}
// Go is the version of the Go runtime that the application is running on.
func Go() string {
return runtime.Version()
}
// license is the license identifier for the application. Should not the full
// license body, but one of the identifiers from https://spdx.org/licenses, so
// that the type of license can be easily determined.
//
// Variable name:
//
// xiam.li/meta.license
//
// Examples:
//
// -ldflags "-X 'xiam.li/meta.license=Apache-2.0'"
// -ldflags "-X 'xiam.li/meta.license=MIT'"
// -ldflags "-X 'xiam.li/meta.license=WTFPL'"
var license string
// License is the license identifier for the application.
func License() string {
return license
}
// LicenseOr is the license identifier for the application, or the given default value if not set.
func LicenseOr(defaultValue string) string {
if license == "" {
return defaultValue
}
return license
}
// license_url is a URL for the application license. Typically links to a page
// where the verbatim license body is available.
//
// Variable name:
//
// xiam.li/meta.license_url
//
// Examples:
//
// -ldflags "-X 'xiam.li/meta.license_url=https://example.com/demo/LICENSE.txt'"
var license_url string
var licenseURLParsed = mustURL("xiam.li/meta.license_url", license_url)
// LicenseURL is the license URL for the application.
func LicenseURL() *u.URL {
return licenseURLParsed
}
// LicenseURLOr is the license URL for the application, or the given default value if not set.
func LicenseURLOr(defaultValue string) *u.URL {
if licenseURLParsed == nil {
return mustURL("xiam.li/meta.license_url", defaultValue)
}
return licenseURLParsed
}
// name is the name of the application. Typically named the same as the binary,
// or for display in an error or help message.
//
// Variable name:
//
// xiam.li/meta.name
//
// Examples:
//
// -ldflags "-X 'xiam.li/meta.name=demo-app'"
var name string
// Name is the name of the application.
func Name() string {
return name
}
// NameOr is the name of the application, or the given default value if not set.
func NameOr(defaultValue string) string {
if name == "" {
return defaultValue
}
return name
}
// note is an arbitrary message for the application. Can be used to store a
// message about the build environment, release, etc.
//
// Variable name:
//
// xiam.li/meta.note
//
// Examples:
//
// -ldflags "-X 'xiam.li/meta.note=Built on CI server ...'"
// -ldflags "-X 'xiam.li/meta.note=This release is dedicated to ...'"
var note string
// Note is an arbitrary message for the application.
func Note() string {
return note
}
// NoteOr is an arbitrary message for the application, or the given default value if not set.
func NoteOr(defaultValue string) string {
if note == "" {
return defaultValue
}
return note
}
// OS is the operating system target that the application is running on.
func OS() string {
return runtime.GOOS
}
// sha is the git SHA that was used to build the application. A 40 character
// "long" SHA should be provided.
//
// Variable name:
//
// xiam.li/meta.sha
//
// Examples:
//
// -ldflags "-X 'xiam.li/meta.sha=bb2fecbb4a287ea4c1f9887ca86dd0eb7ff28ec6'"
// -ldflags "-X 'xiam.li/meta.sha=$(git rev-parse HEAD)'"
var sha string
var shaParsed = mustSHA("xiam.li/meta.sha", sha)
// SHA is the git SHA used to build the application.
func SHA() string {
return shaParsed
}
// SHAOr is the git SHA used to build the application, or the given default value if not set.
func SHAOr(defaultValue string) string {
if shaParsed == "" {
return defaultValue
}
return shaParsed
}
// ShortSHA is the git "short" SHA used to build the application.
func ShortSHA() string {
if shaParsed == "" {
return ""
}
return shaParsed[:7]
}
// ShortSHAOr is the git "short" SHA used to build the application, or the given default value if not set.
func ShortSHAOr(defaultValue string) string {
return SHAOr(defaultValue)[:7]
}
// src is a URL for the application source code. Typically links to a
// repository where a user can browse or clone the source code.
//
// Variable name:
//
// xiam.li/meta.src
//
// Examples:
//
// -ldflags "-X 'xiam.li/meta.src=https://example.com/demo.git'"
var src string
var srcParsed = mustURL("xiam.li/meta.src", src)
// Source is the URL for the application source code.
func Source() *u.URL {
return srcParsed
}
// SourceOr is the URL for the application source code, or the given default value if not set.
func SourceOr(defaultValue string) *u.URL {
if srcParsed == nil {
return mustURL("xiam.li/meta.src", defaultValue)
}
return srcParsed
}
// title is the title of the application. Typically a full or non-abbreviated
// form of the application name.
//
// Variable name:
//
// xiam.li/meta.title
//
// Examples:
//
// -ldflags "-X 'xiam.li/meta.title=Demo Application'"
var title string
// Title is the title of the application.
func Title() string {
return title
}
// TitleOr is the title of the application, or the given default value if not set.
func TitleOr(defaultValue string) string {
if title == "" {
return defaultValue
}
return title
}
// url is a URL for the application homepage. Typically links to a page where a
// user can learn more about the application.
//
// Variable name:
//
// xiam.li/meta.url
//
// Examples:
//
// -ldflags "-X 'xiam.li/meta.url=https://example.com/demo'"
var url string
var urlParsed = mustURL("xiam.li/meta.url", url)
// URL is the homepage URL for the application.
func URL() *u.URL {
return urlParsed
}
// URLOr is the homepage URL for the application, or the given default value if not set.
func URLOr(defaultValue string) *u.URL {
if urlParsed == nil {
return mustURL("xiam.li/meta.src", defaultValue)
}
return urlParsed
}
// version is the version slug for the application. The value can be used to
// point back to a specific tag or release. Supports semver, see
// https://semver.org.
//
// Variable name:
//
// xiam.li/meta.version
//
// Examples:
//
// -ldflags "-X 'xiam.li/meta.version=development'"
// -ldflags "-X 'xiam.li/meta.version=v1.0.0'"
// -ldflags "-X 'xiam.li/meta.version=$(git describe)'"
var version string
// Version is the version slug for the application.
func Version() string {
return version
}
// VersionOr is the version slug for the application, or the given default value if not set.
func VersionOr(defaultValue string) string {
if version == "" {
versionMajor, versionMinor, versionPatch, versionPreRelease,
versionBuild = mustSemver("xiam.li/version", defaultValue)
return defaultValue
}
return version
}
var versionMajor, versionMinor, versionPatch, versionPreRelease, versionBuild = mustSemver("xiam.li/version", version)
// VersionMajor is the semver major version.
// See https://semver.org.
func VersionMajor() string {
return versionMajor
}
// VersionMinor is the semver minor version.
// See https://semver.org.
func VersionMinor() string {
return versionMinor
}
// VersionPatch is the semver patch version.
// See https://semver.org.
func VersionPatch() string {
return versionPatch
}
// VersionPreRelease is the semver pre-release version.
// See https://semver.org.
func VersionPreRelease() string {
return versionPreRelease
}
// VersionBuild is the semver build metadata version.
// See https://semver.org.
func VersionBuild() string {
return versionBuild
}