-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (88 loc) · 2.69 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package main
import (
"flag"
"fmt"
"log/slog"
"os"
"time"
"github.com/gen2brain/beeep"
"github.com/gregfurman/pipescope/internal/checker"
"github.com/gregfurman/pipescope/internal/gateway"
"github.com/gregfurman/pipescope/internal/git"
)
func main() {
faccessToken := flag.String("access-token", setFromEnv("ACCESS_TOKEN", ""), "API access token where remote pipeline resides (env=ACCESS_TOKEN).")
fgitDirectoryLoc := flag.String("git-directory", ".", "Location of .git directory.")
fpollFrequency := flag.Duration("poll-frequency", 5*time.Second, "Polling frequency to pipeline.")
// Experimental
fplaySoundOnComplete := flag.Bool("play-sound", false, "Play a noise when pipeline completes (experimental).")
flag.Parse()
// Define clients
gitClient, err := git.New(*fgitDirectoryLoc)
if err != nil {
exit(err)
}
var gatewayClient gateway.Client
switch {
// If an arg is passed in, use it to determine the git provider
case flag.Arg(0) != "":
gatewayClient, err = gateway.New(*faccessToken, gateway.ProviderType(flag.Arg(0)))
// Check the access token's prefix to determine the git provider
case *faccessToken != "":
gatewayClient, err = gateway.NewFromToken(*faccessToken)
// Check the remote git URL to determine the git provider
default:
url, _ := gitClient.GetRemoteURL()
gatewayClient, err = gateway.NewFromRemoteURL(*faccessToken, url)
}
if err != nil {
exit(err)
}
// Define service
svc := checker.New(gatewayClient, gitClient)
// Run polling
if err := run(svc, *fpollFrequency); err != nil {
exit(err)
}
if *fplaySoundOnComplete {
if err := beeep.Beep(beeep.DefaultFreq, beeep.DefaultDuration); err != nil {
slog.Error("error encountered when playing sound", slog.Any("error", err))
}
}
}
func run(svc *checker.Service, pollFrequency time.Duration) error {
pipeline, err := svc.GetPipeline()
if err != nil {
return fmt.Errorf("checker Service GET pipeline failed: %w", err)
}
status := pipeline.Status
slog.Group("pipeline")
logger := slog.New(slog.Default().Handler()).With(
slog.Any("url", pipeline.URL),
slog.Any("sha", pipeline.CommitSha),
slog.Any("project_id", pipeline.ProjectID),
slog.Any("pipeline_id", pipeline.ID),
)
logger.Info(fmt.Sprintf("Polled Pipeline [status=%s]", pipeline.Status))
statusCh, _ := svc.PollPipelineStatus(pipeline.ProjectID, pipeline.ID, pollFrequency)
for s := range statusCh {
if s == "" {
continue
}
if s != status {
status = s
logger.Info(fmt.Sprintf("Polled Pipeline [status=%s]", s))
}
}
return nil
}
func setFromEnv(name, defaultValue string) string {
if v, ok := os.LookupEnv(name); ok {
return v
}
return defaultValue
}
func exit(err error) {
slog.Error(err.Error())
os.Exit(1)
}