Skip to content

Commit

Permalink
Merge pull request #52 from owenfarrell/implied_defaults
Browse files Browse the repository at this point in the history
Codify implicit defaults for start and stop
  • Loading branch information
vito authored Jul 21, 2020
2 parents a5f2232 + 094025f commit f11dfab
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 26 deletions.
30 changes: 22 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.

Expand All @@ -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.

Expand Down
47 changes: 38 additions & 9 deletions check/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down
18 changes: 13 additions & 5 deletions lord/time_lord.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
4 changes: 0 additions & 4 deletions models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down

0 comments on commit f11dfab

Please sign in to comment.