diff --git a/README.md b/README.md index 0714c48..d2b46f4 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,9 @@ level of precision is better left to other tools. ## Source Configuration -* `interval`: *Optional. Required unless `start` and `stop` are defined.* The - interval on which to report new versions. Valid examples: `60s`, `90m`, `1h`. +* `interval`: *Optional.* The interval on which to report new versions. Valid + examples: `60s`, `90m`, `1h`. If not specified, this resource will generate + exactly 1 new version per calendar day on each of the valid `days`. * `location`: *Optional. Default `UTC`.* The [location](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) in @@ -22,10 +23,12 @@ level of precision is better left to other tools. location: Africa/Abidjan ``` -* `start` and `stop`: *Optional.* Only create new time versions between this - time range. The supported formats for the times are: `3:04 PM`, `3PM`, `3 - PM`, `15:04`, and `1504`. If a `start` is specified, a `stop` must also be - specified, and vice versa. +* `start` and `stop`: *Optional.* Limit the creation of new versions to times + on/after `start` and before `stop`. The supported formats for the times are: + `3:04 PM`, `3PM`, `3PM`, `15:04`, and `1504`. If a `start` is specified, a + `stop` must also be specified, and vice versa. If neither value is specified, + both values will default to `00:00` and this resource can generate a new + version (based on `interval`) at any time of day. e.g. @@ -37,8 +40,19 @@ level of precision is better left to other tools. **Deprecation: an offset may be appended, e.g. `+0700` or `-0400`, but you should use `location` instead.** -* `days`: *Optional.* Run only on these day(s). Supported days are: `Sunday`, - `Monday`, `Tuesday`, `Wednesday`, `Thursday`, `Friday` and `Saturday`. + To explicitly represent a full calendar day, set `start` and `stop` to + the same value. + + e.g. + + ``` + start: 6:00 AM + stop: 6:00 AM + ``` + +* `days`: *Optional.* Limit the creation of new time versions to the specified + day(s). Supported days are: `Sunday`, `Monday`, `Tuesday`, `Wednesday`, + `Thursday`, `Friday` and `Saturday`. e.g. diff --git a/check/check_test.go b/check/check_test.go index b374520..378df50 100644 --- a/check/check_test.go +++ b/check/check_test.go @@ -60,6 +60,44 @@ var _ = Describe("Check", func() { Expect(err).NotTo(HaveOccurred()) }) + Context("when nothing is specified", func() { + Context("when no version is given", func() { + It("outputs a version containing the current time", func() { + Expect(response).To(HaveLen(1)) + Expect(response[0].Time.Unix()).To(BeNumerically("~", time.Now().Unix(), 1)) + }) + }) + + Context("when a version is given", func() { + var prev time.Time + + Context("when the resource has already triggered on the current day", func() { + BeforeEach(func() { + prev = time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, now.Second(), now.Nanosecond(), now.Location()) + version = &models.Version{Time: prev} + }) + + It("outputs a supplied version", func() { + Expect(response).To(HaveLen(1)) + Expect(response[0].Time.Unix()).To(BeNumerically("~", prev.Unix(), 1)) + }) + }) + + Context("when the resource was triggered yesterday", func() { + BeforeEach(func() { + prev = now.Add(-24 * time.Hour) + version = &models.Version{Time: prev} + }) + + It("outputs a version containing the current time and supplied version", func() { + Expect(response).To(HaveLen(2)) + Expect(response[0].Time.Unix()).To(BeNumerically("~", prev.Unix(), 1)) + Expect(response[1].Time.Unix()).To(BeNumerically("~", time.Now().Unix(), 1)) + }) + }) + }) + }) + Context("when a time range is specified", func() { Context("when we are in the specified time range", func() { BeforeEach(func() { @@ -533,15 +571,6 @@ var _ = Describe("Check", func() { Expect(err).NotTo(HaveOccurred()) }) - Context("with a missing everything", func() { - It("returns an error", func() { - <-session.Exited - - Expect(session.Err).To(gbytes.Say("must configure either 'interval' or 'start' and 'stop'")) - Expect(session.ExitCode()).To(Equal(1)) - }) - }) - Context("with a missing stop", func() { BeforeEach(func() { source["start"] = tod(3, 4, -7) diff --git a/lord/time_lord.go b/lord/time_lord.go index fbe1086..e6890f7 100644 --- a/lord/time_lord.go +++ b/lord/time_lord.go @@ -2,9 +2,12 @@ package lord import ( "time" + "github.com/concourse/time-resource/models" ) +var DEFAULT_TIME_OF_DAY = models.TimeOfDay(time.Duration(0)) + type TimeLord struct { PreviousTime time.Time Location *models.Location @@ -59,23 +62,28 @@ func (tl TimeLord) daysMatch(now time.Time) bool { func (tl TimeLord) latestRangeBefore(reference time.Time) (time.Time, time.Time) { - if tl.Start == nil || tl.Stop == nil { - return time.Time{}, time.Time{} + tlStart := DEFAULT_TIME_OF_DAY + if tl.Start != nil { + tlStart = *tl.Start + } + tlStop := DEFAULT_TIME_OF_DAY + if tl.Stop != nil { + tlStop = *tl.Stop } refInLoc := reference.In(tl.loc()) start := time.Date(refInLoc.Year(), refInLoc.Month(), refInLoc.Day(), - tl.Start.Hour(), tl.Start.Minute(), 0, 0, tl.loc()) + tlStart.Hour(), tlStart.Minute(), 0, 0, tl.loc()) if start.After(refInLoc) { start = start.AddDate(0, 0, -1) } stop := time.Date(start.Year(), start.Month(), start.Day(), - tl.Stop.Hour(), tl.Stop.Minute(), 0, 0, tl.loc()) + tlStop.Hour(), tlStop.Minute(), 0, 0, tl.loc()) - if stop.Before(start) { + if !stop.After(start) { stop = stop.AddDate(0, 0, 1) } diff --git a/models/models.go b/models/models.go index 100ed42..6a97694 100644 --- a/models/models.go +++ b/models/models.go @@ -47,10 +47,6 @@ type Source struct { } func (source Source) Validate() error { - if source.Interval == nil && source.Start == nil && source.Stop == nil { - return errors.New("must configure either 'interval' or 'start' and 'stop'") - } - if source.Start != nil && source.Stop == nil { return errors.New("must configure 'stop' if 'start' is set") }