mirror of
https://github.com/spf13/cobra
synced 2025-05-06 05:17:21 +00:00
Allow to override build date
via environment variable in order to make builds easily reproducible. See https://reproducible-builds.org/ for why this is good and https://reproducible-builds.org/specs/source-date-epoch/ for the definition of this variable. Also use UTC to be independent of timezones. Without this change, docker-17.09.1 package man pages varied over time.
This commit is contained in:
parent
4dab30cb33
commit
fb660d6634
1 changed files with 11 additions and 1 deletions
|
@ -20,6 +20,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
@ -87,6 +88,7 @@ type GenManTreeOptions struct {
|
||||||
|
|
||||||
// GenManHeader is a lot like the .TH header at the start of man pages. These
|
// GenManHeader is a lot like the .TH header at the start of man pages. These
|
||||||
// include the title, section, date, source, and manual. We will use the
|
// include the title, section, date, source, and manual. We will use the
|
||||||
|
// SOURCE_DATE_EPOCH environment variable (if set) or the
|
||||||
// current time if Date if unset and will use "Auto generated by spf13/cobra"
|
// current time if Date if unset and will use "Auto generated by spf13/cobra"
|
||||||
// if the Source is unset.
|
// if the Source is unset.
|
||||||
type GenManHeader struct {
|
type GenManHeader struct {
|
||||||
|
@ -120,9 +122,17 @@ func fillHeader(header *GenManHeader, name string) {
|
||||||
}
|
}
|
||||||
if header.Date == nil {
|
if header.Date == nil {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
|
source_date_epoch := os.Getenv("SOURCE_DATE_EPOCH")
|
||||||
|
if source_date_epoch != "" {
|
||||||
|
sde, err := strconv.ParseInt(source_date_epoch, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("Invalid SOURCE_DATE_EPOCH: %s", err))
|
||||||
|
}
|
||||||
|
now = time.Unix(sde, 0)
|
||||||
|
}
|
||||||
header.Date = &now
|
header.Date = &now
|
||||||
}
|
}
|
||||||
header.date = (*header.Date).Format("Jan 2006")
|
header.date = (*header.Date).UTC().Format("Jan 2006")
|
||||||
if header.Source == "" {
|
if header.Source == "" {
|
||||||
header.Source = "Auto generated by spf13/cobra"
|
header.Source = "Auto generated by spf13/cobra"
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue