From cbbfd94fbb7b160ea4c350b2075da194f8236c2c Mon Sep 17 00:00:00 2001 From: anianz Date: Wed, 11 Nov 2020 12:22:16 +0100 Subject: [PATCH] simple check option --- Dockerfile | 3 ++- check/main.go | 38 ++++++++++++++++++++++++++++++++++++++ common/common.go | 26 ++++++++++++++++++++++++++ out/main.go | 17 +---------------- 4 files changed, 67 insertions(+), 17 deletions(-) create mode 100644 check/main.go diff --git a/Dockerfile b/Dockerfile index dc357bb..842ac35 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,7 +4,8 @@ WORKDIR /tmp/nomad-resource COPY . . RUN go build -o dist/out out/main.go && \ - go build -o dist/in in/main.go + go build -o dist/in in/main.go && \ + go build -o dist/check check/main.go FROM alpine:edge diff --git a/check/main.go b/check/main.go new file mode 100644 index 0000000..b3e133f --- /dev/null +++ b/check/main.go @@ -0,0 +1,38 @@ +package main + +import ( + "encoding/json" + "os" + + resource "github.com/cioplenu/concourse-nomad-resource" + "github.com/cioplenu/concourse-nomad-resource/common" +) + +type Request struct { + Source resource.Source `json:"source"` + Version resource.Version `json:"version,omitempty"` +} + +func main() { + var request Request + err := json.NewDecoder(os.Stdin).Decode(&request) + common.Check(err, "Error parsing request") + + lastVersion := request.Version.Version + + history := common.GetHistory(request.Source) + versions := make([]resource.Version, 0) + + for _, jobVersion := range history { + if lastVersion != 0 && lastVersion > jobVersion.Version { + continue + } + versions = append(versions, resource.Version{jobVersion.Version}) + if lastVersion == 0 { + break + } + } + + json.NewEncoder(os.Stdout).Encode(versions) + +} diff --git a/common/common.go b/common/common.go index 77863a5..842e98d 100644 --- a/common/common.go +++ b/common/common.go @@ -1,8 +1,13 @@ package common import ( + "bytes" + "encoding/json" "fmt" "os" + "os/exec" + + resource "github.com/cioplenu/concourse-nomad-resource" ) func Check(err error, msg string) { @@ -11,3 +16,24 @@ func Check(err error, msg string) { os.Exit(1) } } + +func GetHistory(source resource.Source) []resource.JobVersion { + cmd := exec.Command( + "nomad", + "job", + "history", + "-json", + "-address="+source.URL, + "-token="+source.Token, + source.Name, + ) + var histResp bytes.Buffer + cmd.Stdout = &histResp + err := cmd.Run() + Check(err, "Error checking versions") + + var history []resource.JobVersion + json.Unmarshal(histResp.Bytes(), &history) + + return history +} diff --git a/out/main.go b/out/main.go index 52d8d8b..1182966 100644 --- a/out/main.go +++ b/out/main.go @@ -86,22 +86,7 @@ func main() { fmt.Fprint(os.Stderr, out.String()) - cmd = exec.Command( - "nomad", - "job", - "history", - "-json", - "-address="+config.Source.URL, - "-token="+config.Source.Token, - config.Source.Name, - ) - var histResp bytes.Buffer - cmd.Stdout = &histResp - err = cmd.Run() - common.Check(err, "Error checking versions") - - var history []resource.JobVersion - json.Unmarshal(histResp.Bytes(), &history) + history := common.GetHistory(config.Source) response := Response{ Version: resource.Version{