-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (65 loc) · 1.27 KB
/
main.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"fmt"
"os"
"time"
"github.com/akerl/prospectus/v3/plugin"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
)
type config struct {
Seconds int `json:"seconds"`
Branch string `json:"branch"`
}
func run(cfg config) error {
if cfg.Seconds == 0 {
return fmt.Errorf("required arg not provided: seconds")
}
branch := cfg.Branch
if branch == "" {
branch = "main"
}
repo, err := git.PlainOpen(".")
if err != nil {
return err
}
ref, err := repo.Reference(plumbing.NewBranchReferenceName(branch), true)
if err != nil {
return err
}
log, err := repo.Log(&git.LogOptions{From: ref.Hash()})
if err != nil {
return err
}
defer log.Close()
last, err := log.Next()
if err != nil {
return err
}
stamp := last.Committer.When
currentTime := time.Now()
age := int(currentTime.Sub(stamp).Seconds())
if age <= cfg.Seconds {
fmt.Println("fresh")
} else {
fmt.Println("outdated")
}
return nil
}
func main() {
cfg := config{}
meta, err := plugin.ParseConfig(&cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to load config: %s", err)
os.Exit(1)
}
if meta.Mode == plugin.Expected {
fmt.Println("fresh")
return
}
err = run(cfg)
if err != nil {
fmt.Fprintf(os.Stderr, err.Error())
os.Exit(1)
}
}