Skip to content

Commit fe5e611

Browse files
cyphareparis
authored andcommitted
doc: obey SOURCE_DATE_EPOCH with manpage generation (#735)
Previously if a cobra user didn't specify an explicit .Date header, the current time would be included in all of the generated man pages each time they were built. This causes an issue for reproducible builds, since each re-build of a package that includes the man pages will have different times listed in the man pages. To fix this, add support for SOURCE_DATE_EPOCH (which is a standardised packaging environment variable, designed to be used specifically for this purpose[1]). [1]: https://reproducible-builds.org/specs/source-date-epoch/ Signed-off-by: Aleksa Sarai <[email protected]>
1 parent f619abc commit fe5e611

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

doc/man_docs.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"os"
2121
"path/filepath"
2222
"sort"
23+
"strconv"
2324
"strings"
2425
"time"
2526

@@ -104,14 +105,16 @@ func GenMan(cmd *cobra.Command, header *GenManHeader, w io.Writer) error {
104105
if header == nil {
105106
header = &GenManHeader{}
106107
}
107-
fillHeader(header, cmd.CommandPath())
108+
if err := fillHeader(header, cmd.CommandPath()); err != nil {
109+
return err
110+
}
108111

109112
b := genMan(cmd, header)
110113
_, err := w.Write(md2man.Render(b))
111114
return err
112115
}
113116

114-
func fillHeader(header *GenManHeader, name string) {
117+
func fillHeader(header *GenManHeader, name string) error {
115118
if header.Title == "" {
116119
header.Title = strings.ToUpper(strings.Replace(name, " ", "\\-", -1))
117120
}
@@ -120,12 +123,20 @@ func fillHeader(header *GenManHeader, name string) {
120123
}
121124
if header.Date == nil {
122125
now := time.Now()
126+
if epoch := os.Getenv("SOURCE_DATE_EPOCH"); epoch != "" {
127+
unixEpoch, err := strconv.ParseInt(epoch, 10, 64)
128+
if err != nil {
129+
return fmt.Errorf("invalid SOURCE_DATE_EPOCH: %v", err)
130+
}
131+
now = time.Unix(unixEpoch, 0)
132+
}
123133
header.Date = &now
124134
}
125135
header.date = (*header.Date).Format("Jan 2006")
126136
if header.Source == "" {
127137
header.Source = "Auto generated by spf13/cobra"
128138
}
139+
return nil
129140
}
130141

131142
func manPreamble(buf *bytes.Buffer, header *GenManHeader, cmd *cobra.Command, dashedName string) {

0 commit comments

Comments
 (0)