Skip to content

Commit

Permalink
Merge pull request #55 from owenfarrell/command
Browse files Browse the repository at this point in the history
Extract business logic to base package
  • Loading branch information
vito authored Aug 31, 2020
2 parents 2bfa77c + cfb6160 commit a099ab8
Show file tree
Hide file tree
Showing 14 changed files with 351 additions and 402 deletions.
32 changes: 0 additions & 32 deletions check/check_suite_test.go

This file was deleted.

36 changes: 5 additions & 31 deletions check/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"encoding/json"
"fmt"
"os"
"time"

"github.com/concourse/time-resource/lord"
resource "github.com/concourse/time-resource"
"github.com/concourse/time-resource/models"
)

Expand All @@ -19,38 +18,13 @@ func main() {
os.Exit(1)
}

err = request.Source.Validate()
command := resource.CheckCommand{}

versions, err := command.Run(request)
if err != nil {
fmt.Fprintln(os.Stderr, "invalid configuration:", err)
fmt.Fprintln(os.Stderr, "running command:", err.Error())
os.Exit(1)
}

previousTime := request.Version.Time
currentTime := time.Now().UTC()

specifiedLocation := request.Source.Location
if specifiedLocation != nil {
currentTime = currentTime.In((*time.Location)(specifiedLocation))
}

tl := lord.TimeLord{
PreviousTime: previousTime,
Location: specifiedLocation,
Start: request.Source.Start,
Stop: request.Source.Stop,
Interval: request.Source.Interval,
Days: request.Source.Days,
}

versions := []models.Version{}

if !previousTime.IsZero() {
versions = append(versions, models.Version{Time: previousTime})
}

if tl.Check(currentTime) {
versions = append(versions, models.Version{Time: currentTime})
}

json.NewEncoder(os.Stdout).Encode(versions)
}
47 changes: 47 additions & 0 deletions check_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package resource

import (
"time"

"github.com/concourse/time-resource/lord"
"github.com/concourse/time-resource/models"
)

type CheckCommand struct {
}

func (*CheckCommand) Run(request models.CheckRequest) ([]models.Version, error) {
err := request.Source.Validate()
if err != nil {
return nil, err
}

previousTime := request.Version.Time
currentTime := time.Now().UTC()

specifiedLocation := request.Source.Location
if specifiedLocation != nil {
currentTime = currentTime.In((*time.Location)(specifiedLocation))
}

tl := lord.TimeLord{
PreviousTime: previousTime,
Location: specifiedLocation,
Start: request.Source.Start,
Stop: request.Source.Stop,
Interval: request.Source.Interval,
Days: request.Source.Days,
}

versions := []models.Version{}

if !previousTime.IsZero() {
versions = append(versions, models.Version{Time: previousTime})
}

if tl.Check(currentTime) {
versions = append(versions, models.Version{Time: currentTime})
}

return versions, nil
}
Loading

0 comments on commit a099ab8

Please sign in to comment.