Skip to content

Commit ddcee48

Browse files
committed
Validate timestamps in Info
1 parent 3090d21 commit ddcee48

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

goversion.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func AugmentFromEnv(info Info) Info {
199199
}
200200

201201
func ValidateStrict(versionInfo Info) error {
202-
fmt.Println(versionInfo.ToPrettyJSON())
202+
// Check if all fields are set
203203
infoType := reflect.ValueOf(versionInfo)
204204
for i := 0; i < infoType.NumField(); i++ {
205205
fieldType := infoType.Type().Field(i)
@@ -208,11 +208,25 @@ func ValidateStrict(versionInfo Info) error {
208208
}
209209
}
210210

211+
// Ensure that the git state is clean.
211212
if versionInfo.GitTreeState == GitTreeStateDirty {
212213
return errors.New("goversion requires a clean git tree state in strict mode")
213214
}
214215

215-
// TODO validate date fields
216+
// Validate the format of GitCommitDate timestamp.
217+
if ts, err := time.Parse(time.RFC3339, versionInfo.GitCommitDate); err != nil {
218+
return fmt.Errorf("date format in gitCommitDate is not a valid RFC3339 time: %v", err)
219+
} else if !ts.UTC().Equal(ts) {
220+
return errors.New("buildDate is not a UTC time")
221+
}
222+
223+
// Validate the format of BuildDate timestamp.
224+
if ts, err := time.Parse(time.RFC3339, versionInfo.BuildDate); err != nil {
225+
return fmt.Errorf("date format in buildDate is not a a valid RFC3339 time: %v", err)
226+
} else if !ts.UTC().Equal(ts) {
227+
return errors.New("buildDate is not a UTC time")
228+
}
229+
216230
return nil
217231

218232
}

0 commit comments

Comments
 (0)