forked from railwayapp/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogs.go
More file actions
67 lines (62 loc) · 1.81 KB
/
Copy pathlogs.go
File metadata and controls
67 lines (62 loc) · 1.81 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
package controller
import (
"context"
"fmt"
"math"
"strings"
"time"
"github.com/railwayapp/cli/entity"
)
func (c *Controller) GetActiveDeploymentLogs(ctx context.Context, numLines int32) error {
projectID, err := c.cfg.GetProject()
if err != nil {
return err
}
environmentID, err := c.cfg.GetEnvironment()
if err != nil {
return err
}
deployment, err := c.gtwy.GetLatestDeploymentForEnvironment(ctx, projectID, environmentID)
if err != nil {
return err
}
return c.LogsForDeployment(ctx, &entity.DeploymentLogsRequest{
DeploymentID: deployment.ID,
ProjectID: projectID,
NumLines: numLines,
})
}
func (c *Controller) LogsForDeployment(ctx context.Context, req *entity.DeploymentLogsRequest) error {
// LogsForDeployment will do one of two things:
// 1) If numLines is provided, perform a single request and get the last n lines
// 2) If numLines is not provided, poll for deploymentLogs while keeping a pointer for the line number
// This pointer will be used to determine what to send to stdout
// e.g We fetch 10 lines initially. Subsequent fetch returns 12. We print the last 2 lines (delta)
prevIdx := 0
for {
if prevIdx != 0 {
time.Sleep(time.Second * 2)
}
deploy, err := c.gtwy.GetDeploymentByID(ctx, req.ProjectID, req.DeploymentID)
if err != nil {
return err
}
partials := strings.Split(deploy.DeployLogs, "\n")
nextIdx := len(partials)
delimiter := prevIdx
if req.NumLines != 0 {
// If num is provided do a walkback by n lines to get latest n logs
delimiter = int(math.Max(float64(len(partials)-int(req.NumLines)), float64(prevIdx)))
}
delta := partials[delimiter:nextIdx]
if len(delta) == 0 {
continue
}
fmt.Println(strings.Join(delta, "\n"))
prevIdx = nextIdx
if req.NumLines != 0 {
// Break if numlines provided
return nil
}
}
}