From 42e74bc5de240d10cc9178f443cb0cf190001f62 Mon Sep 17 00:00:00 2001 From: Owen Farrell Date: Thu, 25 Jun 2020 10:55:58 -0400 Subject: [PATCH 1/5] Codify implicit defaults for start and stop Signed-off-by: Owen Farrell --- lord/time_lord.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) 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) } From eb6f3daf139f5ff5ed943e1e9ef44b49a0db30e1 Mon Sep 17 00:00:00 2001 From: Owen Farrell Date: Thu, 25 Jun 2020 11:00:34 -0400 Subject: [PATCH 2/5] Update README to outline implied defaults for start and stop Signed-off-by: Owen Farrell --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0714c48..60aff54 100644 --- a/README.md +++ b/README.md @@ -9,8 +9,8 @@ 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`. * `location`: *Optional. Default `UTC`.* The [location](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) in From 2a36ecaec0c4a82e0bfa8c27ddff67bfc7a509e1 Mon Sep 17 00:00:00 2001 From: Owen Farrell Date: Thu, 25 Jun 2020 11:05:19 -0400 Subject: [PATCH 3/5] Remove validation of interval or start/stop Signed-off-by: Owen Farrell --- check/check_test.go | 47 ++++++++++++++++++++++++++++++++++++--------- models/models.go | 4 ---- 2 files changed, 38 insertions(+), 13 deletions(-) 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/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") } From ebaf26ef7a12feae5710398e42e6c7fb13cdc1b0 Mon Sep 17 00:00:00 2001 From: Owen Farrell Date: Mon, 20 Jul 2020 15:08:56 -0400 Subject: [PATCH 4/5] Update README to reflect optional configuration Signed-off-by: Owen Farrell --- README.md | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 60aff54..3b5b23e 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,8 @@ level of precision is better left to other tools. ## Source Configuration * `interval`: *Optional.* The interval on which to report new versions. Valid - examples: `60s`, `90m`, `1h`. + 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,10 @@ 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. e.g. @@ -37,8 +38,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. From 094025f4ba5974715fbfdeca03a99cc1126637b0 Mon Sep 17 00:00:00 2001 From: Owen Farrell Date: Mon, 20 Jul 2020 18:18:55 -0400 Subject: [PATCH 5/5] Document default values for start and stop Signed-off-by: Owen Farrell --- README.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3b5b23e..d2b46f4 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,9 @@ level of precision is better left to other tools. * `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. + `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.