-
Notifications
You must be signed in to change notification settings - Fork 0
/
out.go
54 lines (45 loc) · 1.05 KB
/
out.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package resource
import (
"errors"
"os"
"path/filepath"
"time"
)
func NewOut() out {
return out{}
}
type out struct{}
func (c *out) Run(request OutRequest, artifactsPath string) (OutResponse, error) {
if len(request.Source.List) == 0 {
return OutResponse{}, errors.New("empty list provided in resouce's Source. At least one item required")
}
if request.Params.Previous == "" {
return OutResponse{}, errors.New("no file provided with previous version")
}
prevFile := filepath.Join(artifactsPath, request.Params.Previous)
contents, err := os.ReadFile(prevFile)
if err != nil {
return OutResponse{}, err
}
prevVersion := string(contents)
var nextVersion string
if prevVersion == "" {
nextVersion = request.Source.List[0]
}
for i, v := range request.Source.List {
if v == prevVersion {
if i == len(request.Source.List)-1 {
nextVersion = request.Source.List[0]
break
}
nextVersion = request.Source.List[i+1]
break
}
}
return OutResponse{
Version: Version{
Item: nextVersion,
Date: time.Now(),
},
}, nil
}