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
73 lines (68 loc) · 1.92 KB
/
Copy pathlogs.go
File metadata and controls
73 lines (68 loc) · 1.92 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
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 {
// Fetch Initial Deployment Logs
deploy, err := c.gtwy.GetDeploymentByID(ctx, req.ProjectID, req.DeploymentID)
if err != nil {
return err
}
// Break them down line by line
logLines := strings.Split(deploy.DeployLogs, "\n")
offset := 0.0
if req.NumLines != 0 {
// If a limit is set, walk it back n steps (with a min of zero so no panics)
offset = math.Max(float64(len(logLines))-float64(req.NumLines)-1, 0.0)
}
// Output Initial Logs
fmt.Print(strings.Join(logLines[int(offset):], "\n"))
if req.NumLines == 0 {
// If no log limit is set, we stream logs
prevLogs := strings.Split(deploy.DeployLogs, "\n")
for {
time.Sleep(time.Second * 2)
deploy, err := c.gtwy.GetDeploymentByID(ctx, req.ProjectID, req.DeploymentID)
if err != nil {
return err
}
// Current Logs fetched from server
currLogs := strings.Split(deploy.DeployLogs, "\n")
// Diff logs using the line numbers as references
logDiff := currLogs[len(prevLogs)-1 : len(currLogs)-1]
// If no changes we continue
if len(logDiff) == 0 {
continue
}
// Output logs
fmt.Print(strings.Join(logDiff, "\n"))
// Set out walk pointer forward using the newest logs
prevLogs = currLogs
}
}
return nil
}