-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvercel.go
More file actions
167 lines (144 loc) · 3.31 KB
/
vercel.go
File metadata and controls
167 lines (144 loc) · 3.31 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"bufio"
"os/exec"
"strings"
"sync"
"time"
)
// DeploymentState represents a Vercel deployment state
type DeploymentState int
const (
DeployNone DeploymentState = iota
DeployQueued
DeployBuilding
DeployReady
DeployError
DeployCanceled
)
// DeploymentEvent represents a state change
type DeploymentEvent struct {
Time time.Time
State DeploymentState
}
// VercelPoller polls Vercel CLI for deployment status
type VercelPoller struct {
mu sync.RWMutex
project string
events []DeploymentEvent
lastState DeploymentState
lastDeployID string
pollInterval time.Duration
stopCh chan struct{}
}
// NewVercelPoller creates a new Vercel poller
func NewVercelPoller(project string) *VercelPoller {
return &VercelPoller{
project: project,
pollInterval: 10 * time.Second,
stopCh: make(chan struct{}),
}
}
// Start begins polling in background
func (v *VercelPoller) Start() {
go v.pollLoop()
}
// Stop stops polling
func (v *VercelPoller) Stop() {
close(v.stopCh)
}
func (v *VercelPoller) pollLoop() {
ticker := time.NewTicker(v.pollInterval)
defer ticker.Stop()
// Poll immediately on start
v.poll()
for {
select {
case <-ticker.C:
v.poll()
case <-v.stopCh:
return
}
}
}
func (v *VercelPoller) poll() {
// Execute: vercel ls <project> -y
// Use CombinedOutput because vercel sends the table to stderr
cmd := exec.Command("vercel", "ls", v.project, "-y")
output, err := cmd.CombinedOutput()
if err != nil {
return // Silently ignore errors (optional feature)
}
v.parseOutput(string(output))
}
func (v *VercelPoller) parseOutput(output string) {
v.mu.Lock()
defer v.mu.Unlock()
scanner := bufio.NewScanner(strings.NewReader(output))
inData := false
for scanner.Scan() {
line := scanner.Text()
// Skip header lines
if strings.Contains(line, "Age") && strings.Contains(line, "Status") {
inData = true
continue
}
if !inData || strings.TrimSpace(line) == "" {
continue
}
// Parse deployment line
fields := strings.Fields(line)
if len(fields) < 4 {
continue
}
// Find status and URL
var state DeploymentState
var deployID string
for _, f := range fields {
if strings.HasPrefix(f, "https://") {
deployID = extractDeployID(f)
}
switch strings.ToUpper(f) {
case "BUILDING":
state = DeployBuilding
case "READY":
state = DeployReady
case "ERROR":
state = DeployError
case "QUEUED":
state = DeployQueued
case "CANCELED":
state = DeployCanceled
}
}
// Only track the most recent deployment (first data line)
if deployID != "" {
if deployID != v.lastDeployID || state != v.lastState {
v.events = append(v.events, DeploymentEvent{
Time: time.Now(),
State: state,
})
v.lastDeployID = deployID
v.lastState = state
}
break // Only care about the most recent
}
}
}
// GetEvents returns copy of all events
func (v *VercelPoller) GetEvents() []DeploymentEvent {
v.mu.RLock()
defer v.mu.RUnlock()
result := make([]DeploymentEvent, len(v.events))
copy(result, v.events)
return result
}
func extractDeployID(url string) string {
// Extract unique deployment ID from URL
// e.g., "https://airshelf-gbb82lz07-eugeneoteam.vercel.app"
parts := strings.Split(url, "-")
if len(parts) >= 2 {
return parts[len(parts)-2]
}
return url
}